| Hosted by CoCalc | Download
1
\documentclass{article}
2
\usepackage{amsmath}
3
\usepackage{sagetex}
4
\usepackage{booktabs}
5
6
\begin{document}
7
8
Why is the table showing up here, above the sageblock?
9
10
11
\begin{sageblock}
12
t = r"""
13
\begin{tabular}{ll}
14
col1 & col2 \\
15
1 & 2 \\
16
3 & 4 via sagestr
17
\end{tabular}
18
"""
19
\end{sageblock}
20
21
You just want to include \verb|t| without applying latex(), so use
22
\verb|\sagestr|, not \verb|\sage|.
23
24
\sagestr{"foo"}
25
26
\sagestr{t}
27
28
\section{t2: join(list of str)}
29
30
\begin{sageblock}
31
t2 = []
32
t2.append(r"\begin{tabular}{ll}")
33
t2.append(r"col1 & col2 \\")
34
t2.append(r"1 & 2 \\")
35
t2.append(r"3 & 4")
36
t2.append(r"\end{tabular}")
37
t2 = "\n".join(t2)
38
\end{sageblock}
39
40
\sagestr{t2}
41
42
\section{pandas: DataFrame.to\_html()}
43
44
\begin{sageblock}
45
import statsmodels.api as sm
46
import pandas as pd
47
ds = sm.datasets.grunfeld.load_pandas()
48
pt = ds.data[ds.data.year > 1950]\
49
.pivot_table(\
50
values=["invest"],\
51
index=["firm"],\
52
columns=["year"])
53
\end{sageblock}
54
55
%\sage{ds.data.head().to_latex()}
56
57
Dan: The following uses toprule, midrule, and bottomrule, which require the booktabs package.
58
59
HSY: Ah, thanks, that's really good. I want to demo this to a company who is interested in SMC. Showing them that creating tables is easy, is my goal.
60
61
\sagestr{pt.to_latex()}
62
63
x
64
65
x
66
67
x
68
69
x
70
71
x
72
73
x
74
75
x
76
77
x
78
79
x
80
81
x
82
83
x
84
85
x
86
87
x
88
89
x
90
91
x
92
93
x
94
95
x
96
97
x
98
99
x
100
101
x
102
103
x
104
105
x
106
107
x
108
109
x
110
111
x
112
113
x
114
115
x
116
117
x
118
119
x
120
121
x
122
123
x
124
125
x
126
127
x
128
129
x
130
131
x
132
133
134
135
\end{document}
136
137