从文本文件导入表格单元格

从文本文件导入表格单元格

假设我想建立一个如下所示的表:

\begin{tabular}{ccc}  
 1.23 & 2.98 & Apple \\  
-3.78 & 4.01 & Banana \\  
\end{tabular}

但是,表中的数字是在单独的文本文件中给出的,例如“file.txt”,内容为

1.23 & 2.98
-3.78 & 4.01

有没有一种简单的方法可以将“file.txt”中的单元格输入到更大的表中?我试过了

\begin{tabular}{ccc}
\input{file.txt} & \multirow{2}{*}{Apple \\ Banana} \\
\end{tabular}

但这失败了(不出所料)。我还尝试使用 minipages - 这在上面的玩具示例中有效,但在更实际的示例中失败了(最重要的是,我无法对齐来自不同文件的输入)。

欢迎提出任何意见/建议!

编辑这是 MWE。文件“file.txt”如上所述。“file2.txt”具有以下内容:

1.23 & 2.98 \\
-3.78 & 4.01 \\

(显然,“file.txt”和“file2.txt”都应该保存在tex文件的目录中)。MWE的代码:

\documentclass{article}
\usepackage{multirow}

\begin{document}

% Approach using minipage (works, but looks ugly)
\begin{minipage}{0.4\textwidth}
\begin{tabular}{cc}
\input{file2.txt} 
\end{tabular}
\end{minipage}
\begin{minipage}{0.2\textwidth}
\begin{tabular}{c}
Apple \\ Banana
\end{tabular}
\end{minipage}

% Approach mentioned above (fails)
\begin{tabular}{ccc}
\input{file.txt} & \multirow{2}{*}{Apple \\ Banana} \\
\end{tabular}

\end{document}

答案1

您可以定义\preparetable为表准备数据的宏:

\preparetable {file.txt} {Apple, Banana}

然后在表环境中使用它:

\begin{tabular}{ccc} \usetable \end{tabular}

整个示例如下:

\documentclass{article}      

\newread\infile
\def\preparetable#1#2{\bgroup \openin\infile=#1
   \let\\=\relax \gdef\usetable{}\preparetableA #2,,}
\def\preparetableA #1,{\if,#1,\egroup \closein\infile \else \read\infile to\tmp
  \xdef\usetable{\usetable \tmp & #1 \\}\expandafter\preparetableA\fi}

\begin{document}

\preparetable {file.txt} {Apple, Banana}
\begin{tabular}{ccc} \usetable \end{tabular}

\end{document}

答案2

类似于 wipet 的解决方案,但使用expl3。较长,但一旦经过练习,就相当易读。

\begin{filecontents*}{\jobname.dat}
 1.23 & 2.98
-3.78 & 4.01
\end{filecontents*}

\documentclass{article}
\usepackage{xparse}
\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}

\ExplSyntaxOn
\NewDocumentCommand{\addcolumn}{mm}
 {
  % #1 is the file to read, #2 the list of items to add, separated by \\
  \fabian_addcolumn:nn { #1 } { #2 }
 }

% some variables
\seq_new:N \l__fabian_additions_seq
\tl_new:N \l__fabian_table_body_tl
\tl_new:N \l__fabian_item_tl
\ior_new:N \g_fabian_import_stream

\cs_new_protected:Npn \fabian_addcolumn:nn #1 #2
 {
  % clear the variable containing the table body
  \tl_clear:N \l__fabian_table_body_tl
  % create a sequence from the second argument
  \seq_set_split:Nnn \l__fabian_additions_seq { \\ } { #2 }
  % start reading the file
  \ior_open:Nn \g_fabian_import_stream { #1 }
  % at each line ...
  \ior_map_inline:Nn \g_fabian_import_stream
   {
    % 1. detach the leftmost item from the sequence
    \seq_pop_left:NN \l__fabian_additions_seq \l__fabian_item_tl
    % 2. add the line and a trailing &
    \tl_put_right:Nn \l__fabian_table_body_tl { ##1 & }
    % 3. add the current item
    \tl_put_right:NV \l__fabian_table_body_tl \l__fabian_item_tl
    % 4. add the trailing \\
    \tl_put_right:Nn \l__fabian_table_body_tl { \\ }
   }
  % deliver the result
  \tl_use:N \l__fabian_table_body_tl
 }

\ExplSyntaxOff

\begin{document}

\begin{tabular}{CCl}
\addcolumn{\jobname.dat}{Apple\\Banana}
\end{tabular}

\end{document}

在此处输入图片描述

这是数据文件每行都有尾随的情况的版本\\;我们不是直接添加读取的行,而是先将其存储到标记列表中并替换\\&

\begin{filecontents*}{\jobname.dat}
 1.23 & 2.98 \\
-3.78 & 4.01 \\
\end{filecontents*}

\documentclass{article}
\usepackage{xparse}
\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}

\ExplSyntaxOn
\NewDocumentCommand{\addcolumn}{mm}
 {
  \fabian_addcolumn:nn { #1 } { #2 }
 }

\seq_new:N \l__fabian_additions_seq
\tl_new:N \l__fabian_table_body_tl
\tl_new:N \l__fabian_item_tl
\tl_new:N \l__fabian_temp_tl
\ior_new:N \g_fabian_import_stream

\cs_new_protected:Npn \fabian_addcolumn:nn #1 #2
 {
  \tl_clear:N \l__fabian_table_body_tl
  \seq_set_split:Nnn \l__fabian_additions_seq { \\ } { #2 }
  \ior_open:Nn \g_fabian_import_stream { #1 }
  \ior_map_inline:Nn \g_fabian_import_stream
   {
    \seq_pop_left:NN \l__fabian_additions_seq \l__fabian_item_tl
    \tl_set:Nn \l__fabian_temp_tl { ##1 }
    \tl_replace_once:Nnn \l__fabian_temp_tl { \\ } { & }
    \tl_put_right:NV \l__fabian_table_body_tl \l__fabian_temp_tl
    \tl_put_right:NV \l__fabian_table_body_tl \l__fabian_item_tl
    \tl_put_right:Nn \l__fabian_table_body_tl { \\ }
   }
  \tl_use:N \l__fabian_table_body_tl
 }

\ExplSyntaxOff

\begin{document}

\begin{tabular}{CCl}
\addcolumn{\jobname.dat}{Apple\\Banana}
\end{tabular}

\end{document}

相关内容