27

Can LaTeX select a line from one file and include it in the main file? I use the exam class to make course exams. I like to include a brief quote related to the course in the footer of the last exam page. Currently, I type a new quote each time. I would like to assemble a separate file of quotes, then have one quote drawn at random from the file and placed into the footer when I compile the exam. Quotes may occupy two footer lines but would appear on a single line in the quote file.

\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}

2 Answers 2

28

You can maintain your list of quotes in a CSV file and then use datatool to extract the random quote:

enter image description here

\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}
1
  • This is exactly what I was looking for. Thanks for introducing me to the datatool package. It may be helpful to me in other ways. Jun 28, 2014 at 1:54
10

Solution Idea

Store the quotes in a text array using the arrayjob package, select one random quote by generating a random number using \pdfuniformdeviate.

\pdfuniformdeviate <num> generates a uniformly distributed random integer in the range [0, num). See this link for more details.


The Solution

\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}

The Output

enter image description here

3
  • I think the OP wants his quotes in a separate file. Jun 28, 2014 at 0:52
  • @EthanBolker Yes, the \newarray and \readarray part can be moved to another file. But, IMHO, I like the solution from Werner more than mine.
    – Masroor
    Jun 28, 2014 at 1:10
  • @Masroor, your code would be helpful if I decide to associate a few quotes with one specific exam. I will save your snippet for future reference. Jun 28, 2014 at 1:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .