# Automatic species names in latex / command that does something differently the second time it is run

In academic publications it is a rule of thumb that the first time you give a species name, you write the genus and species in full...and the second time you can abbreviate the genus.

First time:

Escherichia coli

second time:

E. coli

I wanted a latex way to do this automatically. After some searching, I eventually came up with this, and I thought I'd share it..

%% Meta-Command for defining new species macros
#2{\itshape #4}\xspace \else\newcommand{#2}{}{\itshape
#3}\xspace \fi}
}

%% Defining new species
% The first argument is the name of the macro you will call in the document.
% The second argument is the name of a flag that is used to keep track of if this is the first time the macro is being called.
% The third argument is what is written the first time the macro is called
% The fourth argument is what is written every subsequent time the macro is called.

\species{\ecoli}{\ecolihbd}{Escherichia coli}{E.\;coli}
\species{\rsphaeroides}{\rspaheroideshbd}{Rhodobacter
sphaeroides}{R.\;sphaeroides}
\species{\abrasilense}{\abrasilensehbd}{Azospirillum
brasilense}{A.\;brasilense}
\species{\celegans}{\celeganshbd}{Caenorhabditis elegans}{C\;elegans}

%%

%%% Then later on, in the document:
\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli.


Output:

Escherichia coli is an example of a model species. People study E. coli because people have studied E. coli.

-

You don't need the flag: just define the macro to print the complete name and then globally redefining itself to print the abbreviated name. The global definition is needed because the first appearance might be in a group (an environment, for instance).

\documentclass{article}
\usepackage{xspace}

%% Meta-Command for defining new species macros
\newcommand{\species}[3]{%
\newcommand{#1}{\gdef#1{\textit{#3}\xspace}\textit{#2}\xspace}}

%% Defining new species
% The first argument is the name of the macro you will call in the document.
% The second argument is what is written the first time the macro is called
% The third argument is what is written every subsequent time the macro is called.

\species{\ecoli}{Escherichia coli}{E.~coli}
\species{\rsphaeroides}{Rhodobacter sphaeroides}{R.~sphaeroides}
\species{\abrasilense}{Azospirillum brasilense}{A.~brasilense}
\species{\celegans}{Caenorhabditis elegans}{C.~elegans}

%%

\begin{document}
\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli.

\end{document}

-
Clever! One should leave out \xspace, though. \textit will never find an exception from \nocorrlist and wrongly add \/ in \ecoli. for example –  clemens Sep 11 '12 at 11:57
@cgnieder That's in the nature of things: either use \xspace and forget about italic correction or use \ecoli{}. –  egreg Sep 11 '12 at 11:59
This seems to choke if I use it in a title (TeX capacity exceeded); there a workaround? –  Nick T Nov 14 '14 at 22:45
@NickT Try \protect\ecoli in a title. –  egreg Nov 14 '14 at 22:50
Renders, but also ignores my \spacedallcaps –  Nick T Nov 14 '14 at 22:56

I would use glossaries for this.

\documentclass{article}
\usepackage[acronym]{glossaries}
\newacronym[first={Escherichia coli}]{ecoli}{E. coli}{Escherichia coli}

\begin{document}
\Gls{ecoli} is an example of a model species.  People study \gls{ecoli} because
people have studied \gls{ecoli}.
\end{document}


Creating a \species macro to create the \newacronym should be pretty straight forward. The glossaries package takes care of the first/subsequent usage automatically and also handles capitalization and pluralization. It also has nice features for creating lists of acronyms.

-

Starting from egreg's answer, this example uses a different syntax to address the problem with xspace and italics correction. The species are defined using \newspecies and \species sets the species name.

\documentclass{article}

\newcommand*{\speciesformat}[1]{\textit{#1}}
\newcommand{\newspecies}[3]{%
\expandafter\newcommand\csname species@#1\endcsname{%
\expandafter\gdef\csname species@#1\endcsname{#3}%
#2%
}%
}
\makeatletter
\newcommand*{\species}[1]{%
\speciesformat{%
\@ifundefined{species@#1}{%
\latex@error{Species #1' is undefined}\@ehc
[#1]%
}{%
\csname species@#1\endcsname
}%
}%
}
\makeatother

\newspecies{ecoli}{Escherichia coli}{E.~coli}
\newspecies{rsphaeroides}{Rhodobacter sphaeroides}{R.~sphaeroides}
\newspecies{abrasilense}{Azospirillum brasilense}{A.~brasilense}
\newspecies{celegans}{Caenorhabditis elegans}{C.~elegans}

\begin{document}
\textbf{\species{ecoli} is an example} of a model species.
People study \species{ecoli} because people have
studied \species{ecoli}.

\end{document}


Some remarks:

• The example uses \textbf for the first \species call to show, why the global redefinition is important here.

• The syntax makes package xspace obsolete. The suppressed italics correction can be seen in the last call of \species before the period.

The next example calculates the abbreviated form. If the long form consists of more than two word, the first word is abbreviated.

\documentclass{article}

\newcommand*{\speciesformat}[1]{\textit{#1}}
\makeatletter
\newcommand{\newspecies}[2]{%
\expandafter\newcommand\csname species@#1\endcsname{%
\expandafter\speciesMakeShort\csname species@#1\endcsname#2 \@nil{#2}%
#2%
}%
}
\makeatletter
% Assumption: the first word consists of more than one letter.
\def\speciesMakeShort#1#2 #3\@nil#4{%
\begingroup
\def\x{#3}%
\ifx\x\@empty
% one word, no spaces
\gdef#1{#4}%
\else
\species@MakeShort#1#2 #3\@nil
\fi
\endgroup
}
\def\species@MakeShort#1#2#3 #4 \@nil{%
\gdef#1{#2.~#4}%
}
\newcommand*{\species}[1]{%
\speciesformat{%
\@ifundefined{species@#1}{%
\latex@error{Species #1' is undefined}\@ehc
[#1]%
}{%
\csname species@#1\endcsname
}%
}%
}
\makeatother

\newspecies{ecoli}{Escherichia coli}
\newspecies{rsphaeroides}{Rhodobacter sphaeroides}
\newspecies{abrasilense}{Azospirillum brasilense}
\newspecies{celegans}{Caenorhabditis elegans}

\begin{document}
\textbf{\species{ecoli} is an example} of a model species.
People study \species{ecoli} because people have
studied \species{ecoli}.

\begin{center}
\begin{tabular}{ll}
\species{rsphaeroides} & \species{rsphaeroides} \\
\species{abrasilense} & \species{abrasilense} \\
\species{celegans} & \species{celegans} \\
\end{tabular}
\end{center}
\end{document}


-

Here is a way. The command \species defines a boolean flag which will be set to true the first time a species is typeset. Depending on the status of the boolean flag either the long or the short version is typeset:

\documentclass{article}
\usepackage{etoolbox,xspace}

\makeatletter
\def\stripbs{\expandafter\@gobble\string}
\newcommand*\species[3]{%
\newbool{species@\stripbs#1@used}%
\csdef{species@\stripbs#1}{%
\ifbool{species@\stripbs#1@used}%
{% subsequent times:
{\itshape#3}\xspace
}%
{% first time:
\booltrue{species@\stripbs#1@used}%
{\itshape#2}\xspace
}%
}%
\def#1{\csuse{species@\stripbs#1}}%
}
\makeatother

\species{\ecoli}{Escherichia coli}{E.\,coli}
\species{\rsphaeroides}{Rhodobacter sphaeroides}{R.\,sphaeroides}
\species{\abrasilense}{Azospirillum brasilense}{A.\,brasilense}
\species{\celegans}{Caenorhabditis elegans}{C\,elegans}

%%

\begin{document}

\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli.

\celegans is an example of a model species.  People study \celegans because
people have studied \celegans.

\end{document}


-
Note that this version does not add italic correction (\ecoli!). IMHO it would be better to leave out \xspace and use \textit{...} in the definition. –  clemens Sep 11 '12 at 11:49

I have previously used something like this for acronyms and stuff.

Here is a small snippet that can be used:

\newcommand\species[3][]{%
\def\speciesname{\expandafter\@gobble\detokenize\expandafter{\string#2}}%
% Create the full command
\expandafter\newcommand\csname\speciesname full\endcsname{#3}%
% This testing should be performed in other manners, however, it works...
\def\tmpa{#1}%
\def\tmpb{}%
% Create the command \#2short thus you can always retrieve
% the short name
\ifx\tmpa\tmpb%
\expandafter\newcommand\csname\speciesname short\endcsname{#3}%
\else%
\expandafter\newcommand\csname\speciesname short\endcsname{#1}%
\fi%
% The command is created as such:
%   1. First execute the "full" command
%   2. Redefine the command to execute the "short" command
\edef\tmp{%
\noexpand\newcommand\noexpand#2{%
\csname\speciesname full\endcsname%
\noexpand\let\noexpand#2\expandafter\noexpand\csname\speciesname short\endcsname}%
}\tmp%
}


This will let you do this:

\species[E. coli]{\ecoli}{Escherichia coli}


which makes \ecoli be Escherichia coli for the first time and then E. coli the second time. You then always have access to \ecolishort and \ecolifull. Notice that the shorthand argument is optional. If not set it will be the "full" no matter what. With this snippet you always have access to the full and the short version of the name (which I quickly found was very necessary!).

As egreg correctly points out there should be several globals around to ensure correct execution in groups.

So the following:

\species[E. coli]\ecoli{Escherichia coli}

\species\ecolib{Escherichia colib}

\ecoli\ and \ecoli\ and \ecolishort\ and \ecolifull.
\par
\par
\ecolib\ and \ecolib\ and \ecolibshort\ and \ecolibfull.


Will show this:

-

If you use the abbreviations from the glossaries package only for species names, you probably do not want to generate a list of these abbreviations (at least I don't). Hence, it is a bit annoying that the species names in the document are highlighted as hyperlinks when the hyperref package is used.

\glsdisablehyper
\Gls{ecoli} is an example of a model species.  People study \gls{ecoli} because
people have studied \gls{ecoli}.


To re-enable the hyperlinks, the option

\glsenablehyper


can be used.

-

I prefer acronym, so here's a solution using that. What I do is define a new command \acn based on \ac, but instead of calling \acf internally to print Full Name (FN) on the first use, it calls \acl.

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{acronym}

\makeatletter
\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
\expandafter\ifx\csname ac@#1\endcsname\AC@used%
\ifAC@starred\acs*{#1}\else\acs{#1}\fi%
\else
\ifAC@starred\acl*{#1}\else\acl{#1}\acused{ecoli}\fi%
\fi
\fi
}
\makeatother

\begin{document}
\newacro{ecoli}[\emph{E. coli}]{\emph{Escheria Coli}}

\acn*{ecoli}

\acn{ecoli}

\acn{ecoli}
\end{document}


Note that the starred version doesn't marked the acronym as used, which is why the second call still prints the long version.

-

It's great that you've developed your own thing, but have you tried the biocon package, by Pieter Edelman, before? It's availble at CTAN, and does exactly what you're looking for (and it works, I've tried).

Another package available by the same author, useful for fellow biologists, is dichokey, which provides typesetting for dichotomous identification keys.

-