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.


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


Can \LaTeX\ select a random quote from a file?


2 Answers 2


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

enter image description here

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

% http://www.brainyquote.com/slideshow/topics/top_10_success_quotes.html
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

     \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
  }}% Left
  {}% Centre
  {}% Right

  Can \LaTeX{} select a random quote from a file?
  • 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. Commented Jun 28, 2014 at 1:54

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



% Create an array to store the quotes

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



  Can \LaTeX\ select a random quote from a file?

    Yes, of course.


The Output

enter image description here

  • I think the OP wants his quotes in a separate file. Commented 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
    Commented 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. Commented 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 .