7

The idea is define a command as

\newcommand{\mycommand}{This is the official long name}

and I use it as \mycommand several times in the document. But sometimes I would like a short name, something like \mycommand[short name] where short name replace This is the official long name. Is this possible in a simple way?

6

Simpler than Peter Grill's, but using the same idea.

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand{\definealtcommand}{mmm}{%
  \NewDocumentCommand{#1}{s}{\IfBooleanTF{##1}{#3}{#2}}%
}

\definealtcommand{\mycommand}{This is the official long name}{Short name}

\begin{document}

\mycommand\ (Long version)

\mycommand* (Short version)

\end{document}

Remember to add {} or \ after \mycommand without the * if you want a space to appear in the output.

enter image description here

  • The s is for the starred version right? – azetina Jan 7 '15 at 23:11
  • @azetina Yes, and \IfBooleanTF{#1} returns true if the * is found; one needs ##1 because it's a definition inside a definition. – egreg Jan 7 '15 at 23:15
7

Here is how I would do it. Use \SetMyTitle{<sort title>}{<long title>} to define both the long and short titles. Then using \MyTitle you get the long title and \MyTitle* you get the short title:

enter image description here


The other option is to define \SetMyTitle{<long title>} and access this via \MyTitle. If you wish to provide a short title you can provide the optional paramter \MyTitle{<short title>}.

Code: Recommended

\documentclass{article}
\usepackage{xparse}

\makeatletter
\newcommand*{\@MyLongTitle}{}
\newcommand*{\@MyShortTitle}{}
\newcommand*{\SetMyTitle}[2]{%
    \def\@MyLongTitle{#2}%
    \def\@MyShortTitle{#1}%
}
\NewDocumentCommand{\MyTitle}{s}{%
    {\bfseries\IfBooleanTF{#1}{\@MyShortTitle}{\@MyLongTitle}}%
}%
\makeatother

\begin{document}

\SetMyTitle{ShortTitle}{This is the official long name}

Long title is: \MyTitle

Short title is: \MyTitle*

\end{document}

Code: Alternate

\documentclass{article}
\usepackage{xparse}

\makeatletter
\newcommand*{\@MyTitle}{}
\newcommand*{\SetMyTitle}[1]{%
    \def\@MyTitle{#1}%
}
\NewDocumentCommand{\MyTitle}{o}{%
    {\bfseries\IfNoValueTF{#1}{\@MyTitle}{#1}}%
}%
\makeatother

\begin{document}

\SetMyTitle{This is the official long name}

Long title is: \MyTitle

Short title is: \MyTitle[Short Title]

\end{document}
  • Would you also have a solution without the use of xparse? Thanks – pluton Jan 10 '17 at 15:34
  • @pluton: I don't, but you could post a new question. Or copy the code from xparse.sty into your .tex file :-). – Peter Grill Jan 12 '17 at 17:20
6

Also the powerful package acronym can be used for this purpose:

\documentclass{article}
\usepackage{acronym}
\newacro{my}[short name]{This is the official long name}

\begin{document}
``\acl{my}'' is the long version.

The short version is ``\acs{my}''.
\end{document}

Result

Of course, the package acronym has a lot of more features, see the package documentation.

5

And one simple version:

\documentclass{article}
\newcommand{\mycommand}[1][This is the official long name]{#1}    

\begin{document}
    \mycommand

    \mycommand[Short name]
\end{document}

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.