I want to make a command that I say for example:
\newcommand{abbre}{...}
\def\abbre{Modular Response}
And \abbre
should display MR.
Thanks
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityModified from my answer at Typeset just the first letter in a group to: 1) remove the space between letters; 2) present only the letters of the capitalized words of the phrase, and to 3) create a macro of the abbreviated name to recall the original phrase (see the example of \MRT
in my MWE.).
This allows you to work in both directions. For example, I can speak of Modular Response of Tissue"
and abbreviate it as \abbre{Modular Response of Tissue}
to give MRT
. On the other hand, having done the abbreviation once, I can formulate my document with MRT
for the acronym and \MRT
to get the phrase Modular Response of Tissue
.
The most recently defined acronym may be re-accessed with \theabbre
. If you don't want the acronym to print out with the invocation of \abbre{}
, pass it the optional argument [q]
for "quiet" as in \abbre[q]{Modular Response of Tissue}
. You can later access it the acronym with \theabbre
, if it is the most recently defined acronym.
Abbreviations are not limited in their length.
\documentclass[]{article}
\newcommand\abbre[2][v]{\def\theabbre{}%
\expandafter\justfirstCAPS#2 \relax\relax\if q#1\else\theabbre\fi%
\expandafter\def\csname\theabbre\endcsname{#2}}
\makeatletter
\def\justfirstCAPS#1#2 #3\relax{\ifnum`#1>`@\ifnum`#1<`[%
\protected@edef\theabbre{\theabbre#1}\fi\fi%
\if\relax#3\else\justfirstCAPS#3\relax\fi}
\makeatother
\textwidth 5.5in
\def\x{Laughing My A\$\$ Off Rolling On The Floor, Biting The Carpet, Scaring
The Cat, Nearly Dying By Falling Out Of The Window In Front Of A Guy Who
Looks Like Bill Gates, Who Then Horrified, Runs Out On The Street And Is
Accidentally Killed By A Yellow Bulldozer}
\parskip 1em
\begin{document}
This prints \abbre{Modular Response of Tissue} as the abbreviation to Modular
Response of Tissue, which can thereafter be recalled with the
shorthand \verb|\MRT| as \MRT. The most recently defined acronym is \theabbre.
Abbreviations are not limited to two, three, or even nine characters:
One of the longest internet acronyms, according to
\texttt{www.urbandictionary.com}, is\\
\abbre{\x},\\ which stands for ``\x''.
\end{document}
\abbre[q]{Computer Aided Tomography}\CAT{} (\theabbre)
. If you would prefer that \abbre
just prints out the actual words (not the acronym), saving the acronym in \theabbre
, I could make that change.
May 20, 2014 at 12:31
\documentclass{article}
\makeatletter
\newcommand*\firstL[1]{\@car #1\@empty\@nil}
\newcommand*\firstLetter[1]{\expandafter\@firstLetter#1 \@nil}
\def\@firstLetter#1 #2\@nil{%
\firstL{#1}\ifx\\#2\\\else\@ReturnAfterFi{\@firstLetter#2\@nil}\fi}
\long\def\@ReturnAfterFi#1\fi{\fi#1}
\makeatother
\newcommand\abbre{Modular Response}
\begin{document}
\abbre~is the long version and \firstLetter{\abbre} the
short one. And \firstLetter{Modular Response} also. And
\firstLetter{This Is Also Returned With The first Letter}
\end{document}
I can offer a more short macro (only two lines of the \abbre macro):
\def\abbre#1{\expandafter\abbreA#1 \relax/ }
\def\abbreA#1#2 {\ifx#1\relax \else\ifnum\uccode`#1=`#1#1\fi\expandafter\abbreA\fi}
\def\macro{Modular and Response}
\abbre{Something Text is Here} or \abbre\macro.
Result is: STH or MR. The macro \abbre
is expandable thus it can be edefed:
\edef\abbremacro{\abbre\marco} % Now the meaning of \abbremacro is MR.
\newcommand
must be a macro, eg\newcommand\abbre{...}
. In your case it makes no difference in using\def
or not.\newacronym{MR}{mr}{Module Response}
command and then use\gls{MR}
in your text.glossaries
there's alsoacronym
andacro
. I suggest you take a look at them.