LaTeX 可以从另一个文件中随机选择一行吗?

LaTeX 可以从另一个文件中随机选择一行吗?

LaTeX 能否从一个文件中选择一行并将其包含在主文件中?我使用课程exam进行课程考试。我喜欢在最后一页考试的页脚中包含与课程相关的简短引言。目前,我每次都输入一条新引言。我想组装一个单独的引言文件,然后在编写考试时从文件中随机抽取一条引言并将其放入页脚中。引言可能占用两行页脚,但会出现在引言文件中的一行中。

\documentclass{exam}

\footer{\iflastpage{\footnotesize{``Random pithy quote goes here.'' --- Pithy quote speaker.}}}{}{}

\begin{document}
\begin{questions}

\question[15] 
Can \LaTeX\ select a random quote from a file?

\end{questions}
\end{document}

答案1

您可以在 CSV 文件中维护您的报价列表,然后使用datatool提取随机引文:

在此处输入图片描述

\documentclass{exam}
\usepackage[paperheight=20\baselineskip,margin=1cm,includeheadfoot]{geometry}% Just for this example

\usepackage{filecontents,datatool}
% http://www.brainyquote.com/slideshow/topics/top_10_success_quotes.html
\begin{filecontents*}{quotes.txt}
Number,Quote,Author
1,"Success is not final, failure is not fatal: it is the courage to continue that counts.", Winston Churchill
2,"Coming together is a beginning; keeping together is progress; working together is success.", Henry Ford
3,"The price of success is hard work, dedication to the job at hand, and the determination that whether we win or lose, we have applied the best of ourselves to the task at hand.", Vince Lombardi
4,"Success consists of going from failure to failure without loss of enthusiasm.", Winston Churchill
5,"A successful man is one who can lay a firm foundation with the bricks others have thrown at him.", David Brinkley
6,"Always be yourself, express yourself, have faith in yourself, do not go out and look for a successful personality and duplicate it.", Bruce Lee
7,"To succeed in life, you need two things: ignorance and confidence.", Mark Twain
8,"Try not to become a man of success, but rather try to become a man of value.", Albert Einstein
9,"The starting point of all achievement is desire.", Napoleon Hill
10,"Whosoever desires constant success must change his conduct with the times.", Niccolo Machiavelli
\end{filecontents*}

\footer
  {\iflastpage{%
     \footnotesize%
     \DTLloadrawdb[keys={Number,Quote,Author}]{quotes}{quotes.txt}% Load quotes
     \edef\RandomQuote{\number\numexpr1+\pdfuniformdeviate\DTLrowcount{quotes}}% Identify random quote
     \dtlgetrow{quotes}{\RandomQuote}% Retrieve random quote
     \dtlgetentryfromcurrentrow{\Number}{1}\dtlgetentryfromcurrentrow{\Quote}{2}\dtlgetentryfromcurrentrow{\Author}{3}%
     \Number:~\Quote~---~\Author
  }}% Left
  {}% Centre
  {}% Right

\begin{document}
\begin{questions}
  \question[15] 
  Can \LaTeX{} select a random quote from a file?
\end{questions}
\end{document}

答案2

解决方案构想

使用以下方式将引文存储在文本数组中arrayjob 包,通过使用生成随机数来选择一个随机报价 \pdfuniformdeviate

\pdfuniformdeviate <num>生成范围内均匀分布的随机整数[0, num)。请参阅此链接更多细节。


解决方案

\documentclass{exam}
\printanswers

\usepackage{arrayjob}
\usepackage{lipsum}

% Create an array to store the quotes
\newarray\pithyquotes
\readarray{pithyquotes}{\lipsum[1]&\lipsum[2]&\lipsum[3]&\lipsum[4]}

% Get random quote index
\newcounter{pqn}
\setcounter{pqn}{\pdfuniformdeviate 4}% The last number is the total number of quotes
\stepcounter{pqn}% In case 0 has been selected

\footer{\iflastpage{\footnotesize{\pithyquotes(\value{pqn})}}}{}{}

\begin{document}
\begin{questions}

  \question[15] 
  Can \LaTeX\ select a random quote from a file?

  \begin{solution}
    Yes, of course.
  \end{solution}

\end{questions}
\end{document}

输出

在此处输入图片描述

相关内容