TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


The player ended as no.~\test{1}{} and her friend came ind \test{2}{nd}.




How can I change the \test command so that I'm abel to switch between one or two arguments?

(I would like to be abel to write \test{1} instead of \test{1}{}.)

share|improve this question
Optional arguments for LaTeX2e commands are given in square brackets! – Joseph Wright Jan 17 at 10:53
@JosephWright Sorry. I mixed things up; I'll rephrase my question. – Svend Tveskæg Jan 17 at 10:54
@SvendTveskæg You might want to be able to use \test{1} and \test{2}{nd}, but I'm recommending not to. – egreg Jan 17 at 11:03
@SvendTveskæg Joseph already told you: if an argument is optional, treat it as such. – egreg Jan 17 at 11:05
While it is possible to do this it is a really bad idea to do it in a latex2e context. LaTeX2e commands never take a variable number of {} arguments. A major aim of the system is to give a consistent interface to arguments. If an argument is optional it should be in square brackets. – David Carlisle Jan 17 at 11:12
up vote 8 down vote accepted

With xparse the feature can be done easily with the g optional argumen t specifier, but optional arguments should be done with [...], i.e. use o rather, in my point of view!


The player ended as no.~\test{1} and her friend came as \test{2}{nd}.
share|improve this answer
I would like to be abel to write \test{1} and \test{2}{nd} (or \test{5} and \test{8}{th}, ect.). – Svend Tveskæg Jan 17 at 11:00
@SvendTveskæg: That's easy: Use g instead of o as optional argument specifier! – Christian Hupfer Jan 17 at 11:02
I actually like Werner approach better (even though I asked for the solution you have given). – Svend Tveskæg Jan 17 at 17:37
@SvendTveskæg: You asked for this and I provided this -- now you unaccept. I am sure you know that is not kosher at all. – Christian Hupfer Jan 17 at 19:14
I didn't knew that but we can't have that you are upset about it. – Svend Tveskæg Jan 17 at 19:41

I'd suggest an alternative interface using * (say):

enter image description here





\test{1} \test*{1}

\test{2} \test*{2}

\test{11} \test{12} \test{21}

The above command definition is also possible using xparse:

\NewDocumentCommand{\test}{s m}
share|improve this answer
Great! What if I want the starred version to give the ordinal number? – Svend Tveskæg Jan 17 at 17:50
@SvendTveskæg: Then use \newcommand{\test}{\@ifstar\@@test\@test} or \NewDocumentCommand{\test}{s m}{\IfBooleanTF{#1}{\ordinalnum{#2}}{#2}}. – Werner Jan 17 at 17:55
I Guess I have to accept Christian's answer anyway... – Svend Tveskæg Jan 17 at 19:40
@SvendTveskæg: You can accept any answer that helped you the most. – Werner Jan 17 at 19:57
Okay. I think that Christian is rather childish so I'll let him have the 15 points reward but I'll remember your advise for next time. – Svend Tveskæg Jan 18 at 14:37
The player ended as no.~\test{1} and her friend came ind \test[nd]{2}.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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