Read number from file

I have a file called _runs.ini. Depending on its content, the document is compiled differently. I tried to read the content and executed a \ifthenelse-command without success.

Here's my MWE:

\documentclass{book}
\usepackage{ifthen}

\newcommand\getrun{%
\openin\tmp=_runs.ini
\def\ppruns{\numexpr \pruns\relax}
\closein\tmp
}

\newcommand\startrun{%
\ifthenelse{\equal{\ppruns}{0}}
{true}{false}
}

\begin{document}

\getrun
\startrun

\end{document}


The content of _runs.ini is 0 but obvisously the \equal-command does not recognize it as 0.

What am I doing wrong?

And an answer with the original \ifthenelse

\documentclass{book}
\usepackage{ifthen}

\newcommand\getrun{%
\openin\tmp=_runs.ini
\def\ppruns{\numexpr \pruns\relax}
\closein\tmp
}

\newcommand\startrun{%
\ifthenelse{\equal{\pruns}{0 }}%<-- space!
{true}{false}
}

\newcommand\startrunb{%
\ifthenelse{\ppruns=0}
{true}{false}
}

\begin{document}

\getrun
\startrun

\startrunb
\end{document}


If you're sure the file contains a number, I recommend a simpler strategy.

\documentclass{book}

\newcommand\getrun{%
\openin\tmp=_runs.ini
\closein\tmp
}

\newcommand\startrun{%
\ifnum\pruns=0
true
\else
false
\fi
}

\begin{document}

\getrun
\startrun

\end{document}


Or, with catchfile:

\documentclass{book}
\usepackage{catchfile}

\newcommand{\run}{%
\CatchFileDef{\pruns}{_runs.ini}{}%
\ifnum\pruns=0
true
\else
false
\fi
}


You can also use ifthen, if you like:

\documentclass{book}
\usepackage{ifthen}

\newcommand\getrun{%
\openin\tmp=_runs.ini
\closein\tmp
}

\newcommand\startrun{%
\ifthenelse{\pruns=0}{true}{false}
}

\begin{document}

\getrun
\startrun

\end{document}


This answer, using readarray package, is set up to discern whether the lone character of the file is a "0" or not. If you need a more intricate test (i.e., if the file can be more than a single character), let me know the specifics.

The \readdef command places the file contents into a \def, which I then can compare directly with an \if (here I assume the file is a single character).

\documentclass{article}
\newcommand\startrun{\if0\mydata true\else false\fi}
\begin{document}
\getrun
\startrun
\end{document}


Your MWE is a composite of various mistakes.

First. Numbers can be compared by num=num. The \equal from ifthen.sty package compares strings (after expansion), no numbers.

Second. OK, you are using \equal and you have the problem that the \pruns macro includes "0 " (zero space) and you compare it with "0" (zero). These two strings are not equal. The space is here from the end of line in the external file, i.e from \endlinechar. Maybe this is a reason why you added new complication to your code: the new macro \ppruns.

Third. Macro \ppruns is able to remove this trailing space (using eTeXs command \numexpr) but you forgot the \the before \numexpr. Your definition of \ppruns expands to \numexpr 0 \relax this is not the same as 0. If you add the \the before \numexpr then \ppruns expands to 0 which is desired. But it is clear that your attempt is very unclear and the superfluous functionality of eTeX is used here.

Simple solution is: compare numbers no strings, don't use superfluous \ppruns:

\ifnum\pruns=0 true\else false\fi


Forgot \ppruns:

\usepackage{etoolbox}

\newcommand\startrun{%
\ifnumcomp{\pruns}{=}{0}{true}{false}
}


Or better (plain TeX!):

\newcommand\startrun{%
\ifnum\pruns=0
true
\else
false
\fi
}

• @egreg answered with plain TeX version while I was editing, sorry. – Astrinus Oct 1 '14 at 11:16
• This is not exactly plain TeX. The \newcommand is unknown in plain TeX. – wipet Oct 1 '14 at 12:40