如何从文本文件制作表格?

如何从文本文件制作表格?

我有一个文本文件,其中每一行都是一个问题。我想用这个文件制作一份问卷,所以我需要一个循环,循环取出每一行,并用它来制作一个表格行,以及一个 True/False 单元格,直到文件结束。我找到了这两个解决方案,一个不使用任何包,另一个使用pgffor

解决方案 1:

% read input file line by line
\newread\file
\openin\file="media/questions.txt"
    \loop\unless\ifeof\file
        \read\file to \fileline
        % write line
        \fileline
    \repeat
\closein\file
% end loop

解决方案 2:

\foreach \question in {\input{media/questions.txt}}
{
    \question
}

只要我将文本打印到文档中,此文件就可以正常工作。当然,这样做会导致问题没有格式,也不在表格中,因此我尝试将循环插入到tabular

解决方案 1:

\begin{tabular}{|l|c|c|}\hline
% read input file line by line
\newread\file
\openin\file="media/questions.txt"
    \loop\unless\ifeof\file
        \read\file to \question
        % write line
        \question  & True & False \\\hline
    \repeat
\closein\file
% end loop
\end{tabular}

解决方案 2:

\begin{tabular}{|l|c|c|}\hline
\foreach \question in {\input{media/questions.txt}}
{
    \question & True & False \\\hline
}
\end{tabular}

但是,这两个代码都不起作用:错误如下

tex/test-2.tex:15: Argument of \loop has an extra }.
tex/test-2.tex:15: leading text:         \question &
tex/test-2.tex:15: Paragraph ended before \loop was complete.
tex/test-2.tex:15: leading text:         \question &
tex/test-2.tex:16: Extra \fi.
tex/test-2.tex:16: leading text:     \repeat
tex/test-2.tex:28: Missing control sequence inserted.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Missing } inserted.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Extra }, or forgotten \endgroup.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Missing } inserted.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Extra }, or forgotten \endgroup.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Missing } inserted.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Extra }, or forgotten \endgroup.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Missing } inserted.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Extra }, or forgotten \endgroup.
tex/test-2.tex:28: leading text: }
tex/test-2.tex:28: Missing } inserted.
tex/test-2.tex:28: leading text: }

&和似乎都\\使循环在转到下一行之前停止。如何在循环内构建表格行?

答案1

这是一个有效的例子:(用你的文件 ofc 替换 test.txt)

\documentclass{scrreprt}%
\usepackage{xparse}%

\makeatletter%
\NewDocumentCommand{\preparetable}{m}{%
    \bgroup%
    \gdef\usetable{}%
    \let\hline=\relax%
    \newread\file%
    \openin\file=#1\relax%
    {\endlinechar=-1% else you will have empty spaces/lines at the end... 
        \loop\unless\ifeof\file%
        \read\file to\tmp%
        \ifx\tmp\@empty\else%
            \xdef\usetable{\usetable\tmp&True&False\\\hline}%
        \fi%
        \repeat%
    }%
    \closein\file%
    \egroup%
}
\makeatother%

\begin{document}%
    \preparetable{test.txt}%
    \begin{tabular}{|l|c|c|}%
        \usetable%
    \end{tabular}%
\end{document}%

答案2

此解决方案基于datatool包。.txt将包含问题的文件上传到数据库,为答案添加一列,然后将所有行插入到表中。

(从互联网网站上选取的琐事问题。)

A

\documentclass{article}  

\usepackage{datatool}% <<<<<<<<<<<<<<<<<<<<<<<<<<   

\usepackage{filecontents}
\begin{filecontents*}[overwrite]{LotOfQuestions.txt}
%%https://www.opinionstage.com/blog/true-or-false-questions/    
The Big Apple is a nickname given to Washington D.C in 1971.    
Peanuts are not nuts!
People may sneeze or cough while sleeping deeply.
Emus can’t fly.
Electrons move faster than the speed of light.
Light travels in a straight line.
The Mona Liza was stolen from the Louvre in 1911.
You make decisions based on logic.
There’s nothing like reading a good book.
You’re doing exactly what you dreamed of doing as a child.
You’re planning to embark upon an academic career.
There are three rivers in Saudi Arabia.
The Great Wall of China is visible from space. 
Australia is wider than the moon.
Venus is the hottest planet in the solar system.
Cows sleep standing up.
Japan has square watermelons.
I think, carefully, before I say something.
Buying a used car will help you minimize depreciation.
\end{filecontents*} 

\DTLsetseparator{|}

\newcommand{\MarkTrueFalse}{\bigcirc\hspace*{2em}\bigcirc}% <<<<<<<<<<<<<MARKS for true/false

\begin{document}

\DTLloaddb[noheader,keys={Questions}]{questionsdb}{LotOfQuestions.txt}  

\DTLforeach{questionsdb}{% to add a new column
\Questionx=Questions}{%
\DTLappendtorow{TRUEFALSE}{\MarkTrueFalse}}     
    
\begin{table}[htbp]
    \renewcommand{\arraystretch}{1.4}% expand the rows
    \caption{Mark TRUE or FALSE}\bigskip
    \centering
    \begin{tabular}{lc}
        \bfseries Question &
        \bfseries True/False        
        \DTLforeach{questionsdb}{% loop over the rows
            \Questionx=Questions,\TrueFalse=TRUEFALSE}{%
            \\% start new row
            \Questionx & \TrueFalse }
    \end{tabular}
\end{table}
    
\end{document}

更加精致的表格:水平线、垂直扩展和问题编号。方格以基线为中心放置答案。

X

\documentclass[11pt]{article}  

\usepackage{datatool}% <<<<<<<<<<<<<<<<<<<<<<<<<<
\usepackage{booktabs} % lines
\usepackage{amssymb}% more symbols
\usepackage{graphicx}% scale

\usepackage{filecontents}
\begin{filecontents*}[overwrite]{LotOfQuestions.txt}
%%https://www.opinionstage.com/blog/true-or-false-questions/    
The Big Apple is a nickname given to Washington D.C in 1971.    
Peanuts are not nuts!
People may sneeze or cough while sleeping deeply.
Emus can’t fly.
Electrons move faster than the speed of light.
Light travels in a straight line.
The Mona Liza was stolen from the Louvre in 1911.
You make decisions based on logic.
There’s nothing like reading a good book.
You’re doing exactly what you dreamed of doing as a child.
You’re planning to embark upon an academic career.
There are three rivers in Saudi Arabia.
The Great Wall of China is visible from space. 
Australia is wider than the moon.
Venus is the hottest planet in the solar system.
Cows sleep standing up.
Japan has square watermelons.
I think, carefully, before I say something.
Buying a used car will help you minimize depreciation.
\end{filecontents*} 

\DTLsetseparator{|} 
\newcounter{qnumber}
    
\newcommand{\MarkTrueFalse}{\bigcirc\hspace*{2em}\bigcirc}

\renewcommand{\MarkTrueFalse}{% large centered squares
 \raisebox{-1ex}{\scalebox{2}{\square}}\hspace*{2em}\raisebox{-1ex}{\scalebox{2}{\square}}
}
    
\begin{document}

\DTLloaddb[noheader,keys={Questions}]{questionsdb}{LotOfQuestions.txt}  

\DTLforeach{questionsdb}{% to add two new columns
\Questionx=Questions}{%
\DTLappendtorow{TRUEFALSE}{\MarkTrueFalse}
\stepcounter{qnumber}
\DTLappendtorow{NUMBER}{ \theqnumber.}
}

\begin{table}[htbp]
    \renewcommand{\arraystretch}{2}% expand the rows
    \caption{Mark True \textbf{OR} False}\bigskip
    \centering
    \begin{tabular}{clc}
        \toprule
        &\multicolumn{1}{c}{\bfseries \large Question} & \bfseries \large True/False    
        \DTLforeach{questionsdb}{% loop over the rows           
        \Qnumber=NUMBER,\Questionx=Questions,\TrueFalse=TRUEFALSE}{%
        \DTLiffirstrow{\\\midrule}{\\}%         
        \Qnumber&\Questionx & \TrueFalse }% end of loop     
        \\ \bottomrule
    \end{tabular}
\end{table}
    
\end{document}

相关内容