定义缩写命令

定义缩写命令

我想要发出一个命令,例如:

\newcommand{abbre}{...}

\def\abbre{Modular Response}

\abbre应显示MR.

谢谢

答案1

根据我的回答修改仅排版一组中的第一个字母至:1)删除字母之间的空格;2)仅显示短语的大写单词的字母;3)创建缩写名称的宏以调用原始短语(请参阅\MRT我的 MWE 中的示例)。

这样您就可以双向工作。例如,我可以说Modular Response of Tissue"并将其缩写为\abbre{Modular Response of Tissue}MRT另一方面,在进行过一次缩写之后,我可以使用 来制定我的文档,MRT\MRT得到短语Modular Response of Tissue

可以使用 重新访问最近定义的缩写词\theabbre。如果您不希望在调用 时打印出缩写词,请将“quiet”的\abbre{}可选参数传递给它,如 中所示。如果它是最近定义的缩写词,您可以稍后使用 访问它。[q]\abbre[q]{Modular Response of Tissue}\theabbre

缩写的长度不受限制。

\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}

在此处输入图片描述

答案2

\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}

在此处输入图片描述

答案3

我可以提供一个更短的宏(只有两行 \abbre 宏):

\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.

结果是:STH 或 MR。该宏\abbre是可扩展的,因此可以定义为:

\edef\abbremacro{\abbre\marco} % Now the meaning of \abbremacro is MR. 

相关内容