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

Code

\documentclass{article}
\newcommand*\test[2]{#1\textsuperscript{#2}}
\begin{document}
The player ended as no.~\test{1}{} and her friend came ind \test{2}{nd}.
\end{document}

Output

output

Question

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
1  
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
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 at 11:03
2  
@SvendTveskæg Joseph already told you: if an argument is optional, treat it as such. – egreg Jan 17 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 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!

\documentclass{article}
\usepackage{xparse}


\NewDocumentCommand{\test}{mg}{#1\IfValueT{#2}{\textsuperscript{#2}}}
\begin{document}
The player ended as no.~\test{1} and her friend came as \test{2}{nd}.
\end{document}
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
1  
@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

\documentclass{article}

\usepackage{fmtcount}

\makeatletter
\newcommand{\@test}[1]{#1}
\newcommand{\@@test}[1]{\ordinalnum{#1}}
\newcommand{\test}{\@ifstar\@test\@@test}
\makeatother

\begin{document}

\test{1} \test*{1}

\test{2} \test*{2}

\test{11} \test{12} \test{21}
\end{document}

The above command definition is also possible using xparse:

\usepackage{xparse}
\NewDocumentCommand{\test}{s m}
  {\IfBooleanTF{#1}{#2}{\ordinalnum{#2}}}
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
1  
@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
1  
@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
\documentclass[a4paper]{article}
\newcommand\test[2][]{#2\ifx\relax#1\relax\else\textsuperscript{#1}\fi}
\begin{document}
The player ended as no.~\test{1} and her friend came ind \test[nd]{2}.
\end{document}
share|improve this answer

Your Answer

 
discard

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.