如何在 Julia 中将 Dataframe 保存为 pdf 文件?

问题描述 投票:0回答:1

我在将数据框转换为 pdf 文件时遇到了困难。我尝试先转换为 latex,然后再转换为 pdf,但问题是我使用 latexify 包将数据帧转换为 latex 格式,但 latexify() 函数的输出不正确,因为当转换为 pdf 时,它会给出扭曲的表格。那么,你能帮我吗?这是示例代码

using DataFrames
using PrettyTables
using LaTeXStrings
using Latexify
# Create a DataFrame
df = DataFrame(A = [1, 2, 3], B = [4, 5, 6], C = [7, 8, 9])
# Convert the DataFrame to a LaTeX table
latex_table = latexify(df,latex=false)
# Print the LaTeX table
println(latex_table)
# Define the LaTeX document as a string
latex_doc = “”"
\\documentclass{article}
\\usepackage{booktabs}
\\begin{document}
$latex_table
\\end{document}
“”"
# Save the LaTeX document to a file
write(“mydoc.tex”, latex_doc)
# Define the name of the LaTeX file
latex_file = “mydoc.tex”
# Run the pdflatex command to generate the PDF file
run(`pdflatex $latex_file`)

我尝试了

Latexify
PrettyTables
包,具有
latexify()
功能我得到了一个输出,当保存到 PDF 文件时,它给出了扭曲的表格。我交叉检查了在线乳胶到 pdf 转换器,我发现
latexify()
输出不正确。使用
PrettyTables
包我试图创建数据框的乳胶输出但未能这样做。

所以,请建议我应该用来将数据框保存到 PDF 文件的包、函数或标志。

julia pdflatex
1个回答
1
投票

这是将数据帧输出到 LaTeX 的方法:

julia> using DataFrames

julia> df = DataFrame(A = [1, 2, 3], B = [4, 5, 6], C = [7, 8, 9])
3×3 DataFrame
 Row │ A      B      C
     │ Int64  Int64  Int64
─────┼─────────────────────
   1 │     1      4      7
   2 │     2      5      8
   3 │     3      6      9

julia> show(stdout, MIME("text/latex"), df)
\begin{tabular}{r|ccc}
        & A & B & C\\
        \hline
        & Int64 & Int64 & Int64\\
        \hline
        1 & 1 & 4 & 7 \\
        2 & 2 & 5 & 8 \\
        3 & 3 & 6 & 9 \\
\end{tabular}

这里,我用

stdout
输出来展示结果。但是你可以使用任何流来代替
stdout
来写入表内容。

如果您需要更多解释,请发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.