Julia:将结果导出到表:示例代码

时间:2017-06-03 08:20:43

标签: latex julia stargazer xelatex

请指出有关将一些数据和模拟结果导出到一个整洁,可读的表格的示例代码和文档,这些表格可以轻松地复制粘贴或导入到文档中,尤其适用于LaTeX的后期处理。

我目前的做法是:

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  │

将上述内容转换为LaTeX表需要做一些工作。任何正确方向的步骤将不胜感激。

我找到了一个名为LaTeX.jl的有前途的软件包,似乎没有被维护和/或被取代。我的梦想是为R提供类似stargazer的内容。

我在我的代码中使用了unicode Greeks,例如α,可以使用XeLaTeX进行编译,但是α转换为\alpha的解决方案也会受到欢迎。另一种方法是手动替换我的便利函数show_table中的名称,这不是那么糟糕。

3 个答案:

答案 0 :(得分:7)

通常导出乳胶表可能有也可能没有。我在上面的评论中说的是,根据我的经验,对于任何严重的文档,您的乳胶表将是复杂的并且最好单独处理(例如,有些可能有多列,有些可能不是,其他人可能需要自定义间距等)。因此,可能值得让julia脚本生成这些表,即每个表一个脚本。这使您可以在编译最终的乳胶文档时创建makefile的这一部分。

,例如,使用您提供的示例数据框:

%% 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);

在你的朱莉娅REPL:

julia> using DataFrames
julia> # function show_table defined as above ...
julia> T = show_table(1.01,2.02);
julia> include("table1.jl")

导致以下table1.tex文件:

\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}

编译生成的main.tex文件给出:

现在,我想清楚我在这里说的是什么。我不是说这是最通用的自动化朱莉娅方法。恰恰相反。这非常适合您的文件。我所说的是保留一个简单的julia脚本模板来生成适合您项目的表格,并根据您需要的每个特定乳胶表进行调整,可能会更多在撰写论文或类似文档的背景下直截了当,从长远来看,您可能需要对表进行大量微调和控制。

因此,在您编写了第一个表并将其作为模板后,这种方法对于后续表来说很快,因为您只是在这里和那里进行奇怪的调整,同时仍然允许您使用新数据更新表当它们出现时,允许您在更广泛的Latex Makefile编译序列中自动编译julia生成的表。

这是我自己的论文所遵循的方法,它为我节省了很多时间。

答案 1 :(得分:3)

请注意,LaTX导出存在于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"

可以很容易地将其移植到DataFrames。

答案 2 :(得分:1)

这是一个包:

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

我同意OP的观点,即快速解决输出回归结果很有用。我可能想花一个小时在一个项目的某个阶段建立一个自定义表,但对于初学者和快速分享结果,Julia的stargazer(这是该包的目标)是理想的。