4

I'm writing a text containing several chapters. In addition, I use about 50 acronyms. Now I would like to expand the acronym in each chapter, but only at its first occurrence. For this, I'm thinking of a macro like

\newcommand{\acronym}[2]{%
   % if first occurence in chapter
   \emph{#1} (#2}
   % else
   #2
}

The macro describes the command I'm planning to use in my text, e.g., "this is a \acronym{very simple}{VS} example". All acronyms do neither contain white spaces or special characters. In contrast, acronyms are only built of letters.

So I think it is possible with counters, but I have no clue how to start. A counter for each acronym? Or check the sectioning counter?

EDIT: since my package list is already quite long, I would prefer a solution without an additional package. If this is not possible, ok, but normally, there is a way without a package, e.g., use simple tabular and calculate column widths manually.

14
  • 3
    Why don't you use one of the package meant for this? E.g. acro or (larger) glossaries? May 2, 2014 at 8:46
  • @UlrikeFischer I extended my question
    – strauberry
    May 2, 2014 at 8:47
  • A basic thing is that the first ocurrence of the command redefines itself (something like \gdef\…{#2}) and then the rest of them only the second part would show up. By the way, is \acronym the command you are gonna use in your document, or is it the generator of those commands? In that case you would need another argument which is the command itself.
    – Manuel
    May 2, 2014 at 8:57
  • You will need a dedicated acronym counter which is reset at \chapter: the section counter is not reliable as it is possible that you use your acronym only in the third section. And you additionally need for each acronym some test if it has been used in this chapter. Check the code of acro or acronym to find out how they do this. May 2, 2014 at 9:00
  • Manuel and Ulrike, I would provide a 50 points bounty for a complete code example :-)
    – strauberry
    May 2, 2014 at 9:15

1 Answer 1

4
+50
\documentclass{book}

\usepackage{etoolbox}
\usepackage{blindtext}

\newrobustcmd{\ProvideAcronymCounter}[1]{%
\ifltxcounter{#1}{%
}{%
\newcounter{#1}[chapter]
}}

\newcommand{\MyAcronym}[2]{%
  \ProvideAcronymCounter{#2Counter}
  \ifnumequal{\number\value{#2Counter}}{0}{
   % if first occurence in chapter
   \emph{#2} {#1}
   \refstepcounter{#2Counter}
 }{   % else
   #1}
}

\begin{document}

\chapter{First one}
\blindtext[10]

\MyAcronym{Shakespeare}{An author}

\MyAcronym{Shakespeare}{An author}

\MyAcronym{Shakespeare}{An author}


\chapter{Second chapter}
\blindtext[5]

\MyAcronym{Shakespeare}{An author}



\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .