1

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
}
3
  • 2
    Don't use \DeclareDocumentCommand; it would silently override existing commands; in particular \num is defined by siunitx.
    – egreg
    Commented May 23, 2020 at 10:26
  • And what command should I use? Commented May 24, 2020 at 23:10
  • 1
    \NewDocumentCommand is the right choice.
    – egreg
    Commented May 24, 2020 at 23:10

1 Answer 1

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}
3
  • Thank you!! you are so great Commented May 23, 2020 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
    Commented May 23, 2020 at 10:38
  • @egreg you are right Commented May 24, 2020 at 22:48

You must log in to answer this question.

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