Take the 2-minute tour ×
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.

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:

\def\cnt{\input{numberfile}}
\cnt

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

The following code doesn't work:

\newcounter{cnt}
\setcounter{cnt}{\input{numberfile}}
\arabic{cnt}

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
add comment

2 Answers

up vote 14 down vote accepted

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

\documentclass{article}


\newcounter{cnt}
\newread\myread 
\openin\myread=numberfile



\begin{document}

\read \myread to \zz

\setcounter{cnt}{\zz}
\arabic{cnt}

\end{document}

assuming numberfile.tex looks like

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

Without a \read:

\documentclass{article}
\usepackage{catchfile}

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

\newcounter{cnt}

\setcounterfromfile{cnt}{numberfile}

\showthe\value{cnt}

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
add comment

Your Answer

 
discard

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.