2

I have produced the below macros for generating the custom acronym.

\documentclass{book}

\usepackage{ifthen}

\makeatletter
\def\Option#1=#2\relax{\bgroup
  \ifthenelse{\equal{#1}{longplural}}{#2}{}
        \egroup}
\def\myacronym#1{
  \@ifnextchar[%]
    {\@myacronym{#1}}%
    {\@myacronyma{#1}}%
}
\newcommand\@myacronyma[3]{%
\newcounter{#1}%
\expandafter\edef\csname#2long\endcsname{#3 (#2)}%
\expandafter\edef\csname#2longprl\endcsname{#3s (#2s)}%
}
\newcommand\@myacronym[4][]{%
\newcounter{#2}%
\expandafter\expandafter\expandafter\Option{#1}\relax
\expandafter\edef\csname#2short\endcsname{#2}%
\expandafter\edef\csname#2shortprl\endcsname{#2s}%
\expandafter\edef\csname#2long\endcsname{#3 (#2)}%
\expandafter\edef\csname#2longprl\endcsname{#3s (#2s)}%
}%

\def\anpl#1{
\stepcounter{#1}%
\ifnum\csname the#1\endcsname>1%
\csname#1shortprl\endcsname%
\else%
\csname#1longprl\endcsname%
\fi}%

\makeatother

\myacronym[longplural=Hindustan Libraries]{hl}{HLB}{Hindustan Library}
\myacronym{flp}{floppy}{floppy desk}

\begin{document}

\chapter{Sample Title}%\label{ch01}


\anpl{hl} \anpl{flp}

\end{document}

Requirement: When we call the \anpl{hl} the output needs to be rendered as "Hindustan Libraries" and \anpl{flp} then the default plural s to be added at the end (i.e.) "floppy desks" needed in the output. i'm getting the below error message.

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.39 \myacronym[lon
                   gplural=Hindustan Libraries]{hl}{HLB}{Hindustan Library}

Could you please advice. how to handle such cases. Thanks for your help in advance.

Marijn
  • 37,699
Saravanan.O
  • 595
  • 2
  • 5
  • 15

1 Answers1

2

Note that there are easier and more robust ways to handle acronyms in general, also with custom plurals for short and long forms, for example using the glossaries package (see, e.g., custom plural for the short form of an acronym using glossaries). Also, if you choose to use your own implementation, there are packages to make it easier to use key-value options.

However, the provided code can be modified to work when some syntax issues are resolved. Most of the modifications involve simplifying the code.

First the check if an the optional argument is provided with \def. The argument itself does not need to be provided during the check, the command name will be substituted according to \ifnextchar and any arguments are appended to the substitution.

Next the \edef commands (expanded def): the definitions do not need to be expanded but they do need to be global, i.e., \gdef.

Finally the implementation of the longplural argument processing. The argument must be delimited in the pattern to \def so LaTeX knows what is part of the argument and what isn't (in the code below | is used as delimiter). The group is not needed. For simplicity I have implemented it such that the regular plural is always defined, and if there is a longplural argument supplied then the plural is redefined with the supplied argument.

In the code the remaining definitions for regular plurals are also added. I have changed the example of floppy disk to compact disc because floppy itself has an irregular plural (floppies).

Code:

\documentclass{book}

\usepackage{ifthen}

\makeatletter
\def\Option|#1=#2|#3|#4|{\ifthenelse{\equal{#1}{longplural}}%
{\expandafter\gdef\csname#3longprl\endcsname{#2 (#4s)}}{}%
}
\def\myacronym{
  \@ifnextchar[
    \@myacronym%
    \@myacronyma%
}
\newcommand\@myacronyma[3]{%
\newcounter{#1}%
\expandafter\gdef\csname#1long\endcsname{#3 (#2)}%
\expandafter\gdef\csname#1longprl\endcsname{#3s (#2s)}%
\expandafter\gdef\csname#2short\endcsname{#3}%
\expandafter\gdef\csname#2shortprl\endcsname{#2s}%
}
\newcommand\@myacronym[4][]{%
\newcounter{#2}%
\expandafter\gdef\csname#2longprl\endcsname{#4s (#3s)}%
\Option|#1|#2|#3|%
\expandafter\gdef\csname#2short\endcsname{#3}%
\expandafter\gdef\csname#2shortprl\endcsname{#3s}%
\expandafter\gdef\csname#2long\endcsname{#4 (#3)}%
}%

\def\anpl#1{
\stepcounter{#1}%
\ifnum\csname the#1\endcsname>1%
\csname#1shortprl\endcsname%
\else%
\csname#1longprl\endcsname%
\fi}%

\makeatother

\begin{document}

\myacronym[longplural=Hindustan Libraries]{hl}{HLB}{Hindustan Library}
\myacronym{cd}{cd}{compact disc}

\chapter{Sample Title}

\anpl{hl}\\
\anpl{cd}\\
\anpl{hl}\\
\anpl{cd}\\

\end{document}

Result:

enter image description here

Marijn
  • 37,699