如何让Julia的PrettyTables.jl调用booktabs包生成LaTeX表格?
如何让PrettyTables.jl生成booktabs风格的LaTeX表格?
问题背景
我正在使用Julia,希望将名为portfolios的字典形式数据导出为LaTeX表格,该字典内容如下:
Dict{String, Vector} with 5 entries: "Sharpe Ratio" => [1.08321, 0.89012, 1.23641, 1.08433] "Portfolio" => ["Equal weights", "Minimum volatility", "Tangency", "Risk parity"] "Return" => [0.092, 0.0579556, 0.106783, 0.0810661] "Gini index" => [0.0, 0.568016, 0.533732, 0.183507] "Volatility" => [0.0849329, 0.0651099, 0.0863653, 0.0747617]
当前使用的代码如下:
using PrettyTables as PT PT.pretty_table( io, portfolios; backend = Val(:latex), show_subheader = false, alignment=[:l, :c, :c, :c, :c], formatters = PT.ft_printf("%1.2f") )
该代码生成的LaTeX表格使用\hline命令:
\begin{tabular}{lcccc} \hline \textbf{Portfolio} & \textbf{Return} & \textbf{Volatility} & \textbf{Sharpe Ratio} & \textbf{Gini index} \\ \hline Equal weights & 0.09 & 0.08 & 1.08 & 0.00 \\ Minimum volatility & 0.06 & 0.07 & 0.89 & 0.57 \\ Tangency & 0.11 & 0.09 & 1.24 & 0.53 \\ Risk parity & 0.08 & 0.07 & 1.08 & 0.18 \\ \hline \end{tabular}
如何让PrettyTables.jl使用booktabs包的\toprule、\midrule等命令生成表格?
解决方案
要生成符合booktabs包风格的LaTeX表格,只需在pretty_table的参数列表里添加latex_table_type = Val(:booktabs)即可。
修改后的完整代码如下:
using PrettyTables as PT PT.pretty_table( io, portfolios; backend = Val(:latex), show_subheader = false, alignment=[:l, :c, :c, :c, :c], formatters = PT.ft_printf("%1.2f"), latex_table_type = Val(:booktabs) )
生成的LaTeX代码会自动将原有的\hline替换为booktabs专用的规则命令,输出示例如下:
\begin{tabular}{lcccc} \toprule \textbf{Portfolio} & \textbf{Return} & \textbf{Volatility} & \textbf{Sharpe Ratio} & \textbf{Gini index} \\ \midrule Equal weights & 0.09 & 0.08 & 1.08 & 0.00 \\ Minimum volatility & 0.06 & 0.07 & 0.89 & 0.57 \\ Tangency & 0.11 & 0.09 & 1.24 & 0.53 \\ Risk parity & 0.08 & 0.07 & 1.08 & 0.18 \\ \bottomrule \end{tabular}
注意:使用该表格的LaTeX文档必须导入booktabs包才能正常编译,需在文档开头添加:
\usepackage{booktabs}
内容的提问来源于stack exchange,提问作者Raul Guarini Riva
相关产品推荐
相关产品推荐

