I'm trying to define a macro returning a calculated length, but making the parameter optional breaks the macro (error Missing number, treated as zero.
):
\documentclass{article}
\usepackage{xfp}
\usepackage{numprint}
\newcommand{\testa}[1]{\fpeval{#1*0.03}\linewidth}
\newcommand{\testb}[1][1]{\fpeval{#1*0.03}\linewidth}
\begin{document}
\lenprint[pt]{\testa{1}}% prints 10,34958 pt
\lenprint[pt]{\testa{2}}% prints 20,69915 pt
\lenprint[pt]{\testb}% gives ERROR and prints 0.030 pt
\lenprint[pt]{\testb[2]}% gives ERROR and prints 0.060 pt
\end{document}
Can you help understand why?
Is there a way to make the parameter optional without breaking the working macro?
N.B. The above MWE uses \lenprint
, but I'm asking for a solution working in general or at least with \addtolength
, since my real world need is to use the macro as argument of \addtolength
.
\lenprint
.