8

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{%
    \newread\tmp
    \openin\tmp=_runs.ini
    \read\tmp to \pruns
    \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?

Moldevort
  • 1,295

5 Answers5

6

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

\documentclass{book}

\newread\tmp
\newcommand\getrun{%
  \openin\tmp=_runs.ini
  \read\tmp to \pruns
  \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}

\newread\tmp
\newcommand\getrun{%
  \openin\tmp=_runs.ini
  \read\tmp to \pruns
  \closein\tmp
}

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

\begin{document}

\getrun
\startrun

\end{document}
egreg
  • 1,121,712
5

And an answer with the original \ifthenelse

\documentclass{book}
\usepackage{ifthen}

\newcommand\getrun{%
    \newread\tmp
    \openin\tmp=_runs.ini
    \read\tmp to \pruns
    \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}
Ulrike Fischer
  • 327,261
2

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}
\usepackage{readarray}
\newcommand\getrun{\readdef{_runs.ini}{\mydata}}
\newcommand\startrun{\if0\mydata true\else false\fi}
\begin{document}
\getrun
\startrun
\end{document}
2

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
wipet
  • 74,238
0

Forgot \ppruns:

\usepackage{etoolbox}

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

Or better (plain TeX!):

\newcommand\startrun{%
  \ifnum\pruns=0
    true
  \else
    false
  \fi
}
Astrinus
  • 1,809
  • 10
  • 28