6

I accidentally stumbled upon the following

Problem:

If I use the \num command of the siunitx package and leave the argument empty like \num{}, I get a value in my pdf output and it seems to correspond to the value of the option table-format

Example:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[round-mode=places, round-integer-to-decimal, round-precision=2,
    table-format = 1.4, 
    table-number-alignment=center,
    round-integer-to-decimal,
    output-decimal-marker={,},
    abbreviations=false
    ]{siunitx}
\begin{document}

empty num: \num{}
or $\num[round-mode=figures, round-precision=4]{}$


$\num[round-mode=figures, round-precision=4]{1.5}$
\end{document}

Result in pdflatex:

pdflatex result

Is this a bug?

In the manual I only read that the argument _ number_ of the \num command is mandatory, but it seems that there is no error or warning, if it is used like that and this leads to faulty output (which might be easily overlooked, as there is a prettily formatted number although there is no input).

  • Did I do something wrong (except forgetting the mandatory argument)?
  • Could this error be somehow "caught" by LaTeX or the package?

for a pragmatic solution

The problem occurred in this macro \ABeschr{}{}{}{}:

\newcommand{\Variable}[1]{{\normalsize{#1}}}
\newcommand{\Einheit}[1]{{\footnotesize #1}}
\newcommand{\ABeschr}[4]{\begin{minipage}{2cm}\Variable{#1}\newline \Einheit{#2} \newline \tiny $\beta_{0}\!=\!\num[round-mode=figures, round-precision=4]{#3}$ \newline $\kappa=\!\pm\num[round-mode=figures, round-precision=3]{#4}$ %\rule{\linewidth}{1pt}
\end{minipage}}
  • Could anyone suggest how to avoid this error by letting the macro check if the arguments #3 or #4 are empty and then giving no output there?

Remark: here is the solution that currently works or me: https://tex.stackexchange.com/a/234434/4009

2 Answers2

7

The behaviour here is formally 'undefined': you've not supplied a value so anything could happen. I currently don't test for an empty value, only some internal tests to avoid out-and-out disaster, for example infinite loops. However, that should not be relied on one way or the other.

As to where the value comes form: table-format is (currently) parsed by the same code as used for general number parsing.


All that said, I will probably arrange that there is no output at all in this case. An error might be risky as part of a minor update (so has to wait for me to finish v3).

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • hm, would it be possible to throw an error in that case (in the same way as the table-format magically is the output) or is it too complicated? – MostlyHarmless Mar 21 '15 at 16:44
  • 1
    @Martin Of course it would be possible: the question is is it necessary. I'd say no: the input is clearly nonsense so you should pick it up on a cursory read of your source or output. (If I arrange for no output at all, something like ab \num{} cd will end up with additional whitespace.) – Joseph Wright Mar 21 '15 at 16:51
  • 1
    @Martin I will probably check that there is no output in this case for the next release. – Joseph Wright Mar 21 '15 at 16:57
  • Thanks, no output would definitely be better than this random output. :-) – MostlyHarmless Mar 22 '15 at 02:31
0

Solution for the macro

just in case someone else should have this problem, I also want to post my solution to avoid this error in my macro \ABeschr:

Sources

How to check if a macro value is empty or will not create text with plain TeX conditionals?

Code

\newcommand{\ABeschr}[4]{\begin{minipage}{2cm}
\Variable{#1}\newline
\Einheit{#2}
\tiny 
\if\relax\detokenize{#3}\relax
\else
\newline  $\beta_{0}\!=\!\num[round-mode=figures, round-precision=4]{#3}$ 
\fi
\if\relax\detokenize{#4}\relax
\else
\newline $\kappa=\!\pm\num[round-mode=figures, round-precision=3]{#4}$ %\rule{\linewidth}{1pt}
\fi
\end{minipage}}