将数据从 CSV 文件导入到 Latex 表

将数据从 CSV 文件导入到 Latex 表

我在 Windows 7 Pro 上运行 WinEdt 9.0 和 MikTex 2.9。

我正在构建标准格式的 PDF 报告,并且必须遵循该格式(即表格采用特定格式,包含颜色和字体等)。需要明确的是,表格布局很复杂且已预定义。我所要做的就是添加数据点。

该格式要求用浮点数据点填充大型表格,这些数据点保存在我的 PC 上的 CSV 文件中。似乎有很多方法可以做到这一点,但我不确定哪种方法最简单。理想情况下,我希望能够“索引”myFile.txt,例如 x= load(myFile.txt), val1 = x(3,17); 等等

如何将“myFile.txt”中的值分配给我的变量 val1、val2、val3、val4?

我的 MWE 如下。谢谢!

            \documentclass{article}
            \usepackage[T1]{fontenc}
            \usepackage{caption}
            \usepackage{float}
            \usepackage{xspace}
            \usepackage{verbatim}
        \usepackage{array}
        \usepackage{multirow}
        \usepackage{hhline}
        \usepackage{ifthen}
        %http://www.tug.org/texmf-dist/doc/latex/pgfplots/pgfplotstable.pdf

        \newread\file
        \openin\file=A:/myReports/latestResults/myFile.txt
        \loop\unless\ifeof\file
            \read\file to\fileline % Reads a line of the file into \fileline
            % Do something with \fileline
        \repeat
        \closein\file

        \begin{tabular}{|c|c|c|}
          \hline
          t1 & t2 & t3 \\
          y1 & val1 & val4 \\
          y2 & val2 & val3 \\
          \hline
        \end{tabular}
        \end{document}

答案1

一个简单的选择是使用knitr

姆韦

\documentclass[twocolumn]{article}
\usepackage{booktabs}
\parskip1em\parindent0pt
\begin{document}
The CSV file: 
<<echo=F,comment=NA>>=
Val <- read.csv("values.csv",header=T)
val <- Val$Values ## just to simplify the \Sexpr 
Val
@
The \LaTeX\ table:\par
\begin{tabular}{ccc}\toprule
t1 & t2 & t3 \\\midrule
y1 & \Sexpr{val[1]} & \Sexpr{val[4]} \\
y2 & \Sexpr{val[2]} & \Sexpr{val[3]} \\
\bottomrule
\end{tabular}
\end{document}            

相关内容