The player ended as no.~\test{1}{} and her friend came in \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}{}.)

  • 1
    Optional arguments for LaTeX2e commands are given in square brackets! – Joseph Wright Jan 17 '16 at 10:53
  • @JosephWright Sorry. I mixed things up; I'll rephrase my question. – Svend Tveskæg Jan 17 '16 at 10:54
  • 1
    @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 '16 at 11:03
  • 2
    @SvendTveskæg Joseph already told you: if an argument is optional, treat it as such. – egreg Jan 17 '16 at 11:05
  • 4
    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 '16 at 11:12

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}.
| 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 '16 at 11:00
  • 1
    @SvendTveskæg: That's easy: Use g instead of o as optional argument specifier! – user31729 Jan 17 '16 at 11:02
  • I actually like Werner approach better (even though I asked for the solution you have given). – Svend Tveskæg Jan 17 '16 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. – user31729 Jan 17 '16 at 19:14
  • I didn't knew that but we can't have that you are upset about it. – Svend Tveskæg Jan 17 '16 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}
| improve this answer | |
  • Great! What if I want the starred version to give the ordinal number? – Svend Tveskæg Jan 17 '16 at 17:50
  • 1
    @SvendTveskæg: Then use \newcommand{\test}{\@ifstar\@@test\@test} or \NewDocumentCommand{\test}{s m}{\IfBooleanTF{#1}{\ordinalnum{#2}}{#2}}. – Werner Jan 17 '16 at 17:55
  • 1
    @SvendTveskæg: You can accept any answer that helped you the most. – Werner Jan 17 '16 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 '16 at 14:37
  • @SvendTveskæg: :) – Werner Jan 18 '16 at 14:49
The player ended as no.~\test{1} and her friend came ind \test[nd]{2}.
| improve this answer | |

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.