3

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.

PatrickT
  • 10,037
  • 9
  • 76
  • 111
  • I'm obviously on a ``Julia/LaTeX`` roll: ``https://stackoverflow.com/questions/44120903/julia-docstrings-and-latex`` – PatrickT Jun 03 '17 at 08:25
  • in my (admittedly very personal) experience, if you have a very specific document in mind, rather than go for a generic solution you might as well generate the exact `.tex` you want yourself by writing to a file. Then simply `input` that file into your main `.tex` document at the right position. I've done this throughout my thesis (using octave) and it worked very well. Not to mention you can then put such julia / octave scripts in your latex makefile to have them automatically recompiled (e.g. in the case of new data) and automatically re-entered into your document. – Tasos Papastylianou Jun 03 '17 at 11:37
  • @TasosPapastylianou, yes that's what I'd like to do. Do you have example code on how to export, say, the results of a regression in ``Julia``.? Or an example to output my table above? Thanks. – PatrickT Jun 03 '17 at 12:19
  • 1
    The output of `show` on a DataFrame is an html table - that format with the `|` is just the julia repl's way of showing those. Jupyter deals with it another way. I think stargazer just creates html tables, right? – Michael K. Borregaard Jun 05 '17 at 18:12
  • 1
    The sensible way to do this is to export a CSV, and then use [PGFPlotsTable](https://www.ctan.org/pkg/pgfplotstable) – Frames Catherine White Jun 06 '17 at 03:19

3 Answers3

7

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.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
5

Note that LaTeX export exists in DataTables.jl:

julia> using DataTables

julia> dt = DataTable(Fish = ["Suzy", "Amir"], Mass = [1.5, 2])
2×2 DataTables.DataTable
│ Row │ Fish │ Mass │
├─────┼──────┼──────┤
│ 1   │ Suzy │ 1.5  │
│ 2   │ Amir │ 2.0  │

julia> reprmime("text/latex", dt)
"\\begin{tabular}{r|cc}\n\t& Fish & Mass\\\\\n\t\\hline\n\t1 & Suzy &     1.5 \\\\\n\t2 & Amir & 2.0 \\\\\n\\end{tabular}\n"

It could easily be backported to DataFrames.

2

Here is a package:

https://github.com/jmboehm/RegressionTables.jl

I agree with the OP that It’s useful to have a quick solution to output regression results. I may want to spend an hour building a custom table at some stage of a project, but for starters and to share results quickly, stargazer for Julia (which is what that package aims for) is ideal.

Florian Oswald
  • 5,054
  • 5
  • 30
  • 38