Reputation: 25
I am trying to convert a DataFrame in Julia to a LaTeX table, similar to how it is done in Python.
I have examples of both the Python and Julia code. In Python, I use pandas to convert the DataFrame to a LaTeX table with specific formatting options. Here's the Python code:
import pandas as pd
df = pd.DataFrame(
dict(name=["Raphael", "Donatello"], age=[26, 45], height=[181.23, 177.65])
)
print(
df.to_latex(
index=False,
formatters={"name": str.upper},
float_format="{:.1f}".format,
)
)
The output LaTeX table from the above Python code looks like this:
\begin{tabular}{lrr}
\toprule
name & age & height \\
\midrule
Raphael & 26 & 181.23 \\
Donatello & 45 & 177.65 \\
\bottomrule
\end{tabular}
https://i.sstatic.net/KnBA0iBG.png
In Julia, I have found the following code:
using DataFrames
df = DataFrame(name=["Raphael", "Donatello"], age=[26, 45], height=[181.23, 177.65])
show(stdout, MIME("text/latex"), df)
This code generates a basic LaTeX table that looks like this:
\begin{tabular}{r|ccc}
& name & age & height\\
\hline
& String & Int64 & Float64\\
\hline
1 & Raphael & 26 & 181.23 \\
2 & Donatello & 45 & 177.65 \\
\end{tabular}
https://i.sstatic.net/J7pnoV2C.png
But, I would like to know how to apply similar formatting as in the Python example.
Upvotes: 2
Views: 346
Reputation: 183
There are a couple of options. For example:
using Pkg
pkg"activate --temp"
pkg"add DataFrames, Latexify"
using DataFrames, Latexify
df = DataFrame(name = ["Raphael", "Donatello"], age = [26, 45], height = [181.23, 177.65])
# Don't forget to read the docs, including
# https://korsbo.github.io/Latexify.jl/stable/arguments/#Tabular
latexify(df; env = :table, booktabs = true, latex = false) |> print
which will give you
\begin{tabular}{ccc}
\toprule
name & age & height\\
\midrule
Raphael & 26 & 181.23\\
Donatello & 45 & 177.65\\
\bottomrule
\end{tabular}
you can further manipulate the resulting string (e.g. to change the position specifier to lrr
)
l = latexify(df; env = :table, booktabs = true, latex = false)
using LaTeXStrings
latexstring(replace(l, "{ccc}" => "{lrr}"))
(some functions operating on String
s are also defined to work with LaTeXStrings
- see this)
Or, if you want more control over the output to match the Python version:
pkg"add LaTeXTabulars, LaTeXStrings"
using LaTeXTabulars
using LaTeXStrings
# https://github.com/tpapp/LaTeXTabulars.jl
latex_tabular(
"mytable.tex",
Tabular("lrr"),
[Rule(:top), names(df), Rule(:mid), df[1, :], df[2, :], Rule(:bottom)],
)
— which you can also do programmatically — to get:
\begin{tabular}{lrr}
\toprule
name & age & height \\
\midrule
Raphael & 26 & 181.23 \\
Donatello & 45 & 177.65 \\
\bottomrule
\end{tabular}
EDIT: you can further configure the latexify
call, just like you did df.to_latex
. You can specify the formatter, add it as default, etc.
Upvotes: 1