將資料從 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); ETC

如何將「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}            

相關內容