# Are context-sensitive species or abbreviation macros possible?

Thanks to this, automated abbreviation of species and some technical terms became understandable for me as a LaTeX beginner :-) My PhD supervisors (not LaTeX users) however, criticised that I used the abbreviations too often, reducing text comprehension.

I found a Basically: No. answer about a similar math question, but also the resetting options for the glossaries package. The latter however seem to require consciously setting the reset commands, which would not save much time over simply writing the terms out manually.

Therefore, I wonder about a way to globally redefine abbreviation macros to take the context of its use into account. In order for example to spell out a term in a rule-based or context sensitive way, like in every heading, upon every 1st use within a (sub)section, in every figure caption, etc., while continuing to use the abbreviation in normal paragraphs. Is there a package capable of that? If yes, how can it be done? Or if not, do you know of any software that can do it?

• Context - aware means that TeX must look what is coming up (possible) but rarely possible with looking into past, what has been typeset to far. You could of course parse the input before it is really processed, but I am not sure if this really easy with TeX/LaTeX at all – user31729 Sep 17 '15 at 12:22
• I think I understand what you're getting at though it could be clearer. Are you saying you want for example Parus major initally and then P. major for subsequent calls to a macro? That could probably be done with acronym (not as full-featured as glossaries but it's what I've always used and so I can tweak it. What you do need to do though, is define where you want to reset to the full form. You could easily have that happen at every chapter for example. – Chris H Sep 17 '15 at 12:24
• @ChristianHupfer that's true, but I wonder if in this case a history-aware approach would do: If the full version hasn't been given for n pages/paragraphs/sections... then expand, otherwise abbreviate. – Chris H Sep 17 '15 at 12:26
• @ChrisH: Perhaps it would do, agreed, but this depends on the real document. If you have some ideas, go ahead please! – user31729 Sep 17 '15 at 12:37
• @ChrisH: You can count sections with (my) package assoccnt. It will give you the full number of sections etc. throughout the document, even if the section counters are reset. It does not work for TeX paragraphs (i.e. \par like stuff – user31729 Sep 17 '15 at 12:43

Here's something which might do the trick, though it doesn't implement some of my more advanced ideas. It's based on an old related answer of mine and an even older trick from Marco Daniel (\ifenv).

The code:

\usepackage{acronym}
\usepackage{etoolbox}

\makeatletter
\def\ifenv#1{
\def\@tempa{#1}%
\ifx\@tempa\@currenvir
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\newcommand*{\acn}{\AC@starredfalse\protect\@acn}%
\WithSuffix\newcommand\acn*{\AC@starredtrue\protect\@acn}%
\newcommand{\@acn}[1]{%
\ifAC@dua
\ifAC@starred\acl*{#1}\else\acl{#1}\fi%
\else
\ifenv{figure}{%
\ifAC@starred\acl*{#1}\else\aclu{#1}\fi%
}{%
\expandafter\ifx\csname AC@#1\endcsname\AC@used%
\ifAC@starred\acs*{#1}\else\acs{#1}\fi%
\else
\ifAC@starred\acl*{#1}\else\aclu{#1}\fi%
\fi
\fi}}
\makeatother
\preto\section{\acresetall}


\acn is a clone of acronym's \ac but instead of the full form of the name including the short form in brackets, it just prints the long form on first use. New compared to my old answer is a test of whether we're in a figure environment, in which case the long form is always used.

I've also told all acronyms to resest at the start of every section (the last line above). You could easily replace \section with \chapter or \subsection.

Usage:

\begin{document}
\acrodef{ecoli}[\emph{E. coli}]{\emph{Escherichia Coli}}

\section{Intro}
Here were are going to write about \acn{ecoli}.  We are interested in \acn{ecoli}.

\section{Background}
Some more about \acn{ecoli}.  And a few more \acn{ecoli} facts.
\begin{figure}[h]
\centering\bfseries{This is a figure}
\caption{A picture of \acn{ecoli}}
\end{figure}
\end{document}


(just paste this immediately after the other block and compile twice) to get: