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.
\parmight be redefined in certain places, I'd also use\noexpand\par. – egreg Jan 17 '18 at 21:30You 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%) at the end of lines? for a reference to%. – Werner Jan 17 '18 at 21:37\def\abcd{abcd} \def\efgh{\abcd} \def\abcd{efgh} \efghprintsefghsince the value of\abcdwas changed, even though\efghwas 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} \efghwill printabcdsince\efghis anexpandeddefinition of\abcd. Expansion could be considered synonymous with evaluation. – Werner Jan 17 '18 at 21:41\exemples, you don't want to expand\hyperlinksince it's not needed. It should remain as-is (as\hyperlink) until you use it. The reason it caused an error depends on the definition. There would be problems during expansion if the expansion includes definitions themselves. – Werner Jan 17 '18 at 22:05