2

The following example fails with ! Argument of \@iinput has an extra }. The problem does not occur when not using \input or not using \num. How can I avoid the error?

\documentclass{minimal}
\usepackage{siunitx}
\usepackage{filecontents}

\begin{filecontents*}{nline}
12345.6789
\end{filecontents*}

% Command to load the number from the simulated file
\newcommand{\rload}[1]{\input{#1}}

% Command to format the number loaded from the simulated file
\newcommand{\rnum}[1]{\num{\rload{#1}}}

\begin{document}
The formatted number is: \rnum{nline}
\end{document}
2
  • similar question tex.stackexchange.com/questions/108087/… , but things might have changed with the new siunitx version. Commented Aug 11 at 18:37
  • 1
    basically what you want is \num{12345.6789} and what you have is \num{\IFFileExists{file}{\def\thisfile{file}123456.6789\space\par\endinput}{\LaTeXError{...}}} you can probably make it work but if you have control over the input file it's much simpler to write \num{12345.6789} in the file then simply \input it Commented Aug 11 at 19:05

1 Answer 1

4

I'd load the file contents before passing it to \num.

\begin{filecontents*}[overwrite]{\jobname.num}
12345.6789
\end{filecontents*}

\documentclass{article}
\usepackage{siunitx}

% Command to load the number from the simulated file
\ExplSyntaxOn
\tl_new:N \l_diomidis_rload_tl

\NewDocumentCommand{\rnum}{m}
 {% #1 = file name
  \file_get:nnNTF {#1} {} \l_diomidis_rload_tl
   {% the file exists
    \num{\l_diomidis_rload_tl}
   }
   {% missing file
    \mbox{MISSING}
   }
 }
\ExplSyntaxOff


\begin{document}

The formatted number is: \rnum{\jobname.num}

The formatted number is: \rnum{xyzzzzxxx}

\end{document}

Please, be aware that minimal is not meant for examples. I changed the file name just not to risk clobbering my files; any file name (the .tex extension would be implicit if missing) is good.

enter image description here

3
  • Excellent, thank you! A slight problem is that if a file is missing, LaTeX just hangs, without issuing an error. Commented Aug 11 at 22:04
  • I addressed missing files using \IfFileExists to set \l_diomidis_rload_tl to an obviously wrong value if a file doesn't exist. Commented Aug 12 at 7:16
  • @DiomidisSpinellis Edited accordingly. You might turn the “missing file” part into an error message, if you prefer.
    – egreg
    Commented Aug 12 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .