TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 26 down vote accepted

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?
share|improve this answer
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. – Mike Taylor Jun 28 '14 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

share|improve this answer
I think the OP wants his quotes in a separate file. – Ethan Bolker Jun 28 '14 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 '14 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. – Mike Taylor Jun 28 '14 at 1:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.