Please point to example code and documentation on approaches to export some data and simulation results to a tidy, readable table that could easily be copy-pasted or imported into documents, particularly for post-processing with LaTeX
.
My current practice is along the lines of:
using DataFrames
function show_table(mp::ModelParameters, ms::ModelSolution)
α = mp.α; δ = ms.δ;
d = DataFrame(Name = @data(["α"]),
Description = @data(["alpha"]),
Value = @data([α])
)
push!(d, @data(["δ", "delta", δ]))
return(d)
end
2×3 DataFrames.DataFrame
│ Row │ Name │ Description │ Value │
├─────┼──────┼─────────────┼───────┤
│ 1 │ "α" │ "alpha" │ 1.01 │
│ 2 │ "δ" │ "delta" │ 2.02 │
Turning the above into a LaTeX
table will require some work. Any step in the right direction will be appreciated.
I have found a promising package named LaTeX.jl that does not seem to be maintained and/or has been superseded. The dream would be to have something like stargazer for R
.
I have used unicode Greeks in my code, e.g. α, which can be compiled with XeLaTeX
, but a solution where α
were converted to \alpha
would be welcome too. An alternative would be to manually replace the names in my convenience function show_table
, which is not so bad.
There may or may not be a nice package that generally exports latex tables. What I'm saying in my comment above is that, in my experience, for any serious document, your latex tables will be complicated and best looked after individually (some may have multicolumns for instance, others might not, others might need custom spacing etc). So it may be worth having julia scripts generating those tables instead, i.e. one script per table. This enables you to then make this part of your makefile when compiling your final latex document.
E.g., using your provided example dataframe:
%% main.tex -- example bare-bones document illustrating how
% to import externally generated tables, without
% affecting the structure of your main document
% when those external tables get updated / replaced
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[htbp]
\centering
\input{./table1} % I.e. simply dump julia-generated table here
\caption{This table was generated in Julia}
\label{tbl:table1}
\end{table}
\end{document}
### table1.jl script
T[:,:Name] = "\$\\" * T[:,:Description] * "\$"; # replace symbol with latex math
# Generate table header
Table = "\\begin{tabular}{| " * "c |" ^ (ncol(T)+1) * "}\n";
Table *= " \\hline\n";
Table *= " % Table header\n";
Table *= " \\rowcolor[gray]{0.9}\n";
Table *= " Row"; for i in 1:ncol(T); Table *= " & " * string(names(T)[i]); end; Table *= " \\\\\n";
# Generate table body (with nice alternating row colours)
toggleRowColour(x) = x == "0.8" ? "0.7" : "0.8";
rowcolour = toggleRowColour(0.7);
Table *= " % Table body\n";
for row in 1 : nrow(T)
Table *= " \\rowcolor[gray]{" * (rowcolour = toggleRowColour(rowcolour); rowcolour) * "}\n";
Table *= " " * string(row); for col in 1 : ncol(T) Table *= " & " * string(T[row,col]); end; Table *= " \\\\\n";
Table *= " \\hline\n";
end
Table *= "\\end{tabular}\n";
# Export result to .tex file
write("table1.tex", Table);
In your julia REPL:
julia> using DataFrames
julia> # function show_table defined as above ...
julia> T = show_table(1.01,2.02);
julia> include("table1.jl")
Resulting in the following table1.tex
file:
\begin{tabular}{| c |c |c |c |}
\hline
% Table header
\rowcolor[gray]{0.9}
Row & Name & Description & Value \\
% Table body
\rowcolor[gray]{0.7}
1 & $\alpha$ & alpha & 1.01 \\
\hline
\rowcolor[gray]{0.8}
2 & $\delta$ & delta & 2.02 \\
\hline
\end{tabular}
Compiling the resulting main.tex
file gives:
Now, I want to be clear what I'm saying here. I'm not saying this is the most generic, automated julia approach. Quite the opposite. This is highly specific to your file. what I am saying is that keeping a simple julia script template for generating tables like this which is suitable for your project, and adapting it for each specific latex table you require, is probably going to be a lot more straightforward in the context of writing a thesis or similar document, where you'll probably need a lot of fine-tuning and control over your tables in the long term.
So, after you've written your first table and have it as a template, this approach is fast for the subsequent tables because you just make the odd tweak here and there, while still enabling you to update your tables with new data as they arise, and allowing you to make compilation of your julia-generated tables automatic within a wider Latex Makefile compilation sequence.
This was the approach I followed with my own thesis and it saved me a lot of time.