# NewDocumentCommand : problem on reading arguments

I have made this test latex doc to learn how \NewDocumentCommand works

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\test}{o}{\IfNoValueTF{#1}{test}{#1}}
\begin{document}
\test{gino}
\end{document}


But something is wrong in fact it keeps giving me as output "testgino" but it should be giving me only "gino"

• Do you want an optional argument (can be omitted entirely) or to test if a mandatory argument is empty? The two are very different things. – Joseph Wright Sep 25 '17 at 16:53
• Can you please choose a more meaningful title? – user36296 Sep 25 '17 at 16:53
• While you wait for an answer to this question, can you go back to your previous ones and look if the answers solve your problems and accept them, if they do? – user36296 Sep 25 '17 at 16:54
• @Joseph, would this question be considered too localized? There are several resources for learning about the xparse package and its commands, for example here or here. There seems to be little that the community can gain from this question that wouldn't be found elsewhere. Am I wrong about this? – Sandy G Sep 25 '17 at 17:38
• @SandyG i had an example of how the command works, but it was completely wrong so i asked here, in that example i had the optional argument to be specified with "g" and not with "o" – Daniel Sep 25 '17 at 17:58

You have to give to your command the number of mandatory arguments that you will use like :

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\test{m}{\IfNoValueTF{#1}{test}{#1}}
\begin{document}
\test{gino}
\end{document}


This {m} makes the difference.

Output:

gino

If you really want the argument to be optional try like this:

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\test{o}{\IfNoValueTF{#1}{test}{#1}}
\begin{document}
\test[gino]
\end{document}


Optional arguments needs '[]' to be read.

Also please change your title to "can't read argument from NewDocumentCommand" or something that might be found from users with similar problems.

• With an m argument type, \IfNoValueTF{#1} will always return false – egreg Sep 25 '17 at 17:09
• @egreg I just tried to explain the m and o that realized was the OP's problem. I just used the sane code. But I think your comment is useful in this particular example. – koleygr Sep 25 '17 at 17:44
• My problem in the original file was that i put the optional argument into {}, so it wasn't printing the document as supposed to! now i fixed it and it's all fine – Daniel Sep 25 '17 at 17:56

The o means an optional argument is expected; if present it should be enclosed in square brackets, so

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\test}{o}{\IfNoValueTF{#1}{test}{#1}}
\begin{document}

\test

\test[gino]

\end{document}


will print

test
gino

Note \test[gino], not \test{gino}.

Such a command can be defined more briefly

\NewDocumentCommand{\test}{O{test}}{#1}


that is, passing to the O specifier the code to execute when the optional argument is not present.

By the way, if #1 stands for a mandatory argument (defined with the specifier m), \IfNoValueTF{#1} will always return false. It returns true when the argument is optional (typically o, but not only) and is not present in the input for the macro call.

• Ok, then the optional parameter should go into [] and not into {}, right? Ok, finally i got it! – Daniel Sep 25 '17 at 17:36