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

I want to create a loop including n images.The number n is the only content of a help file (created by the application that generates the images).
Is it possible to read the contents of the help file and use that as a number variable to set the loop?

The following code works:


Output is the number (i.e. the content of the file).

The following code doesn't work:


The error indicates that the input isn't a number ("Missing number, treated as zero").

So how can I convert the input file to a LaTeX number?

share|improve this question
Welcome to TeX.SX. – Claudio Fiandrino Feb 8 '13 at 11:10
Hi back :-) As I'm still quite new to LaTeX, I'll be only receiving for some time. But in the past I usually turned to answering questions too at some point ;-) – uli_1973 Feb 10 '13 at 12:56
up vote 15 down vote accepted

One way is to use \read which reads a file a line at a time




\read \myread to \zz



assuming numberfile.tex looks like

share|improve this answer
Thanks, this works perfectly! – uli_1973 Feb 8 '13 at 11:19

Without a \read:


\newcommand{\setcounterfromfile}[2]{% #1 = counter, #2 = file name
  \CatchFileDef{\scfftemp}{#2}{\endlinechar=-1 }%




If numberfile.tex contains the only line 12345 (a very difficult number to guess, as it's well known1) we get

> 12345.

1 The reference is, of course, to Mel Brooks' Space Balls.

share|improve this answer
Correction to my comment on the other solution: My real task expects the file name to be passed as an argument, and the file name may contain special characters and does contain an additional dot. The \read solution doesn't work with that but the 'catchfile' approach does handle such filename without problems. – uli_1973 Feb 10 '13 at 13:20

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.