[Tex/LaTex] How to store a counter value at some point in the document

countersmacros

I'm beginning to learn LaTeX and I need some help with counters.

My goal is to store the value a certain counter reaches at some point in the document and use it later. This is my attempt:

\documentclass{article}

\begin{document}

\section{title}

text
\let\myvalue\thesection

\section{title}
text

\myvalue

\end{document}

but \myvalue prints 2 instead of 1.

I read this: What is the difference between \let and \def? but let does not seem to store the value at the definition time in my case.

What am I doing wrong?

Best Answer

A naive example of how to do that,

\documentclass{article}

\newcounter{foo}

\begin{document}

\section{title}

text
\setcounter{foo}{\value{section}}

\section{title}
text

\thefoo

\end{document}

You just create a new counter with the command \newcounter and you set it to the value you want with \setcounter.

Output

NOTE If you re looking for some referencing tool, better solutions exist with \label{} and \ref{}, among other.

\documentclass{article}

\newcounter{foo}

\begin{document}

\section{title}
\label{sec:mylabel}

text
\setcounter{foo}{\value{section}}

\section{title}

This is using the new counter: \thefoo

\noindent
This is using the label and ref commands: \ref{sec:mylabel}

\end{document}

Output 2

Related Question