# How to reset a macro every section? e.g. Macro that italizes a specie names

I was looking for a macro that create automatically the italic name of species. I found this great answer. Unfortunately, I have to write the complete form every document section. What I have to add to this macro for to write the full name when it is called for the first time in every section?

\documentclass{article}
\usepackage{xspace}

\newcommand{\species}[3]{%
\newcommand{#1}{\gdef#1{\textit{#3}\xspace}\textit{#2}\xspace}}

\species{\ecoli}{Escherichia coli}{E.~coli}

%%

\begin{document}
\section{Section 1}
\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli.\par
\section{Section 2}
\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli.
\end{document}


• You might want to use something like glossaries instead of your own macros. – Henri Menke Nov 13 '17 at 21:07
• @HenriMenke I tryed to use glossaries, however I found it very difficult to run in Windows. It wasn't clear how to run \makeglossaries – Daniel Valencia C. Nov 13 '17 at 21:10

Revamped version of my answer at https://tex.stackexchange.com/a/71094/4427

The following code allows to reset the long form. The command \resetspecies can be called anywhere; if no optional argument is given, all species will be reset, otherwise only those specified as in the following example.

With \xpreto{\section}{\resetspecies}, all species will be reset at a new section (use \chapter if the resetting is to be done at chapters).

The notable difference with the previous version is that the macro name should be given without the backslash (for ease of implementation).

\documentclass{article}
\usepackage{xspace}
\usepackage{xparse,xpatch}

\ExplSyntaxOn

%% Meta-Command for defining new species macros
\NewDocumentCommand{\species}{mmm}
{
\daniel_species_define:nnn { #1 } { #2 } { #3 }
}

\prop_new:N \g_daniel_species_names_prop
\prop_new:N \g_daniel_species_used_prop
\prop_new:N \l__daniel_species_temp_prop

\cs_new_protected:Nn \daniel_species_define:nnn
{% save the long and short forms in a property list
\prop_gput:Nnn \g_daniel_species_names_prop { long#1 } { #2 }
\prop_gput:Nnn \g_daniel_species_names_prop { short#1 } { #3 }
% the long form is initially chosen
\prop_gput:Nnn \g_daniel_species_used_prop { #1 } { 0 }
\cs_new_protected:cpn { #1 }
{% if the value in the "used" property list is 0 use the long form
\int_compare:nNnTF { \prop_item:Nn \g_daniel_species_used_prop { #1 } } = { 0 }
{
\textit { \prop_item:Nn \g_daniel_species_names_prop { long#1 } }
}
{
\textit { \prop_item:Nn \g_daniel_species_names_prop { short#1 } }
}
% mark the species as used
\prop_gput:Nnn \g_daniel_species_used_prop { #1 } { 1 }
\xspace
}
}

\NewDocumentCommand{\resetspecies}{ o }
{
\IfNoValueTF { #1 }
{% reset all species
\prop_clear:N \l__daniel_species_temp_prop
\prop_map_inline:Nn \g_daniel_species_used_prop
{
\prop_put:Nnn \l__daniel_species_temp_prop { ##1 } { 0 }
}
\prop_gset_eq:NN \g_daniel_species_used_prop \l__daniel_species_temp_prop
}
{% reset only the specified ones
\clist_map_inline:nn { #1 }
{
\prop_gput:Nnn \g_daniel_species_used_prop { ##1 } { 0 }
}
}
}

\ExplSyntaxOff

\xpreto{\section}{\resetspecies}

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

\section{First}
\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli. Also \abrasilense.

\resetspecies

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

\resetspecies[ecoli,celegans]

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

\section{Second}
\ecoli is an example of a model species.  People study \ecoli because
people have studied \ecoli. Also \abrasilense and \celegans.

\end{document}


Here is an option for your implementation:

\documentclass{article}

\usepackage{xspace,etoolbox}

\newcommand{\setspecies}[4]{%
#1{#2}{\renewcommand{#2}{\textit{#4}\xspace}\textit{#3}\xspace}}%
\newcommand{\species}[3]{%
\setspecies{\newcommand}{#1}{#2}{#3}%
\pretocmd{\section}{\setspecies{\renewcommand}{#1}{#2}{#3}}{}{}}

\species{\ecoli}{Escherichia coli}{E.~coli}

\begin{document}

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

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

\end{document}


The \setspecies macro can be called to redefine how the species are set. It defines it upon a call to \species, and redefines it with every \section.