0

I made a command with DeclareDocumentCommand from the packet xparse, and I have had troubles. My problem is that the \ifmmode (if mathmode) and the else don't work correctly, because when I type $$ \num{}[] $$, it shows the things that are in \ifmmode and \else.

imagen_1

Here is part of the code

\DeclareDocumentCommand{\num}{m o o}{
    \ifmmode
        \text{mathmode}
        \IfValueT{#1}{\text{#1}}
        \IfValueT{#2}{
            \IfNoValueTF{#3}{
                \,\text{#2}
            }
        }
    \else
        \text{textmode}
        \IfValueT{#1}{#1}
        \IfValueT{#2}{
            \IfValueT{#3}{#2/#3}
            %\, % Espacio
            \IfNoValueTF{#3}{\ \text{#2}}
        }
    \fi
}
  • 2
    Don't use \DeclareDocumentCommand; it would silently override existing commands; in particular \num is defined by siunitx. – egreg May 23 at 10:26
  • And what command should I use? – El borito May 24 at 23:10
  • 1
    \NewDocumentCommand is the right choice. – egreg May 24 at 23:10
4

You used \IfNoValueTF but only provided one branch where two were expected. You probably meant to use \IfNoValueT.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\NewDocumentCommand{\num}{m o o}{%
    \ifmmode
        \text{mathmode}%
        \IfValueT{#1}{\text{#1}}%
        \IfValueT{#2}{%
            \IfNoValueT{#3}{% <<< T instead of TF
                \,\text{#2}%
            }%
        }%
    \else
        \text{textmode}%
        \IfValueT{#1}{#1}%
        \IfValueT{#2}{%
            \IfValueT{#3}{#2/#3}%
            %\, % Espacio
            \IfNoValueT{#3}{\ \text{#2}}% <<< T instead of TF
        }%
    \fi
}
\begin{document}
\[ \num{}[] \]
\end{document}
| improve this answer | |
  • Thank you!! you are so great – El borito May 23 at 4:34
  • 1
    The last two tests can be incorporated in a single \IfNoValueTF. Besides, it makes no sense to do \IfValueT{#1}, because it's a mandatory argument. – egreg May 23 at 10:38
  • @egreg you are right – El borito May 24 at 22:48

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.