1

I have a macro for writing SI units that take numbers as optional arguments, but when these macros are used for ToC entries, the .toc file does not write closing brackets for optional arguments, messing up said .toc file during re-compile. Is this a fixable problem?

MWE :

\documentclass[10pt,a4paper,twoside]{article}
\usepackage{siunitx,xstring}
\usepackage[hidelinks]{hyperref}

\ExplSyntaxOn % Used for many other macros, so keeping this would be very nice
\NewDocumentCommand \meter {o o} {
    \IfValueTF{#1} {
        \IfValueTF{#2} { \ensuremath{\SI{#2}{#1\meter}} }
        { \IfDecimal{#1} { \ensuremath{\SI{#1}{\meter}} } { \si{#1\meter} } }
    } { \si{\meter} }
}
\ExplSyntaxOff

\begin{document}
\tableofcontents
\section[\meter[0.7]]{\meter[0.8]}
\end{document}

.toc Entries :

\contentsline {section}{\numberline {1}\meter [0.7}{1}{section.1}% 

1 Answer 1

1

Just change

\section[\meter[0.7]]{\meter[0.8]}

to

\section[{\meter[0.7]}]{\meter[0.8]}

i.e., enclose the expression \meter[0.7] located in the optional argument of \section in a pair of curly braces. That way, LaTeX gets the help it needs to parse the arguments of \section correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.