# eappto trigger undefined control sequence error when used with hyperlink

You can jump to the question in bold at the bottom if you want

I want to add examples of theorem at the end of my documents (with link after theorem to acess examples) but while having the exemple written under the theorem in my code (easier to manage).

I made this code :

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}

\newcounter{exemplei}

\def\exemples{}

\newcommand{\exemple}[1]{
\hypertarget{exemple\arabic{exemplei}}{\theexemplei}
\appto\exemples{\hyperlink{exemple\arabic{exemplei}}{Back} \theexemplei #1

}
\stepcounter{exemplei}
}
\begin{document}

Additions
\exemple{1+1=2}

\newpage

Multiplications
\exemple{1x1=1}

\newpage

\exemples

\end{document}


Unfortunatly back link didn't work. I discovered it was because \arabic{exemplei} was returning the last value and not the current value (probably because \appto was executing the code when showing the variable and not when adding text to it). I found that I had to replace \appto by \eappto:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}

\newcounter{exemplei}

\def\exemples{}

\newcommand{\exemple}[1]{
\hypertarget{exemple\arabic{exemplei}}{\theexemplei}
\eappto\exemples{\hyperlink{exemple\arabic{exemplei}}{Back} \theexemplei #1

}
\stepcounter{exemplei}
}
\begin{document}

Additions
\exemple{1+1=2}

\newpage

Multiplications
\exemple{1x1=1}

\newpage

\exemples

\end{document}


But now this code trigger undefined control sequence error. Why ? How to fix it ?

I also discovered that I don't have error when not having hyperlink in content I add to the variable.

## 1 Answer

While the counter values need to be expanded, \hyperlink should not, so you should use \noexpand\hyperlink as part of the \exemple macro:

\newcommand{\exemple}[1]{%
\hypertarget{exemple\arabic{exemplei}}{\theexemplei}%
\eappto\exemples{\noexpand\hyperlink{exemple\arabic{exemplei}}{Back}
\theexemplei{} #1\noexpand\par}%
\stepcounter{exemplei}%
}


Spurious spaces have been removed (using % at the line-ends) and I inserted a forced \par if you want to separate each element.

• Since \par might be redefined in certain places, I'd also use \noexpand\par. – egreg Jan 17 '18 at 21:30
• @egreg: True. I've updated the code. – Werner Jan 17 '18 at 21:31
• Thanks. Surprised by how quick I get answer :D You also answered a question I asked myself : is there a way to do a command not printing text : i have to use %; Do you have a link explaining this about what expand means ? Because in reality I have no idea what it really means, I just think it's to make it evaluate latter, or something related to redifinition. Things I found on internet about expand seems very complicated (I don't even know what a token is :D). – Matheod Jan 17 '18 at 21:31
• @Matheod: See What is the use of percent signs (%) at the end of lines? for a reference to %. – Werner Jan 17 '18 at 21:37
• @Matheod: Expansion is a tricky concept in TeX and a core part of the language. For example \def\abcd{abcd} \def\efgh{\abcd} \def\abcd{efgh} \efgh prints efgh since the value of \abcd was changed, even though \efgh was set before this change. Regular programming languages don't do this, since definitions are evaluated and set at the time of definition. Instead, \def\abcd{abcd} \edef\efgh{\abcd} \def\abcd{efgh} \efgh will print abcd since \efgh is an expanded definition of \abcd. Expansion could be considered synonymous with evaluation. – Werner Jan 17 '18 at 21:41