TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

My objective is to create a table or some form of environment as shown in the figure below.

What I want

The numbers should be randomly generated every time you compile and have the font shown below.

Number with specific font

Is this possible?

share|improve this question
I'm quite certain that the digits you show are from the "OCR-A" font. (To be entirely certain, you'd have to show the shape of the digits 5 and 8.) See, e.g., en.wikipedia.org/wiki/File:OCR-A_char_digits.svg, for more details. – Mico Jan 21 '12 at 19:32
Is its(the OCR-A font) implementation in latex possible? – azetina Jan 21 '12 at 19:36
Yes, with the ocr font package. Type texdoc ocr on a command line to get more information about this package. – Mico Jan 21 '12 at 19:52
up vote 23 down vote accepted

You could use TikZ nodes and pgf made random numbers, for example:

\begin{tikzpicture}[start chain=1 going right,node distance=-0.4pt]
    \foreach \x in {1,2,...,15} {
        \node (\x) [draw,on chain=1] {\pgfmathparse{random(0,9)}\ocr{\pgfmathresult}};} 
    \node [above of=1,anchor=south west,yshift=2ex,xshift=-2.4ex]
        {Do not write in this area};
    \node [below of=1,anchor=north west,yshift=-2ex,xshift=-2.4ex]
        {\scshape Each document must have a unique serial number};

The output, using ocr-a numbers, as Mico advised in his comment below:

random numbers on chain

To get different numbers for each compilation, you could initialize the random number generator yourself by


By default, it's the value of \time*\year. So it would't change during each compilation, so I used the datetime package to use also minutes and seconds for calculating the seed. So the random values should change each second. You could also use values or counters in your .aux file instead.

You can get the ocr-a font from CTAN: http://ctan.org/pkg/ocr-a. If you don't manage to install it, a quick workaround: you could load the mf and tfm files (for example from here), put them into your document directory and run (with fontenc and ocr as above) - it worked for me.

share|improve this answer
Can I use the current time to generate a different random number with each compilation? – Uwe Ziegenhagen Jan 21 '12 at 19:46
You'll also want to add the instructions \usepackage[T1]{fontenc} and \usepackage[ocr-a]{ocr} in the preamble, and then encase the command that outputs the digits in \ocr{...}, to get the displayed numbers to show up in OCR-A. – Mico Jan 21 '12 at 19:48
@UweZiegenhagen: yes, I extended my answer. – Stefan Kottwitz Jan 21 '12 at 20:18
Hi @StefanKottwitz, I finally got it to work. For some reason or the other, neither of TexWorks or Texmaker were generating the font properly but when I tried to compile with WinEdt, it worked just fine. Don't know what is the problem but hey it worked. – azetina Jan 23 '12 at 13:24
After a lot of trial and error, finaly it worked (using Texmaker here under Kubuntu 12.04). Key actions: (1) put all ocr-related font files in the same directory as the tex source and (2) comment out the \pgfmathparse and \pgfmathsetseed commands in the preamble! Don't really understand why. – Nikos Alexandris Oct 4 '12 at 0:07

On the off-chance that you're unable to manage the fonts, you can download the OCR-A numeral SVG and convert it to PDF (say) using Inkscape. Then, include each numeral via the traditional \includegraphics (from graphicx) with some appropriate trim-and-clip:

enter image description here

\usepackage{tikz}% http://ctan.org/pkg/pgf
%\usepackage{graphicx}% http://ctan.org/pkg/graphicx (implicitly loaded by tikz)

% Extract numerals from image
\expandafter\def\csname ocr-a-0\endcsname{\includegraphics[width=2ex,trim=0 0 4990 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-1\endcsname{\includegraphics[width=2ex,trim=554 0 4438 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-2\endcsname{\includegraphics[width=2ex,trim=1109 0 3882 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-3\endcsname{\includegraphics[width=2ex,trim=1664 0 3327 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-4\endcsname{\includegraphics[width=2ex,trim=2218 0 2773 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-5\endcsname{\includegraphics[width=2ex,trim=2773 0 2218 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-6\endcsname{\includegraphics[width=2ex,trim=3327 0 1664 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-7\endcsname{\includegraphics[width=2ex,trim=3883 0 1109 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-8\endcsname{\includegraphics[width=2ex,trim=4437 0 555 0,clip]{ocr-a}}%
\expandafter\def\csname ocr-a-9\endcsname{\includegraphics[width=2ex,trim=4991 0 0 0,clip]{ocr-a}}%

\begin{tikzpicture}[start chain=1 going right,node distance=-0.4pt]
  \foreach \x in {1,2,...,15} {
    \node [draw,on chain=1] {\pgfmathparse{random(9)}\csname ocr-a-\pgfmathresult\endcsname};} 


In the above example there is no need for an explicit loading of graphicx, since tikz already loads it.

The following steps were following in Inkscape:

  1. Open the document:

    Inkscape - open SVG

  2. Save as PDF:

    Inkscape - save as PDF

share|improve this answer
I will give it a try. Is the above image the original svg file or the 800*86 pixels png file? – azetina Jan 21 '12 at 20:17
@azetina: It is the original SVG (not the PNG). Inkscape handles SVG without problem. My coordinates for trim match the original downloaded SVG. If you download the lower resolution PNG, you would need different coordinates. – Werner Jan 21 '12 at 20:27
@WernerThanks for the quick reply will do the conversion of the image file in a few as soon as Inkscape finishes installing. – azetina Jan 21 '12 at 20:31
@azetina Can this be considered a "clean" solution? Why is it (so) hard to get ocr-a to work? – Nikos Alexandris Sep 13 '12 at 10:03

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.