Document Classes ⇒ referencing a post calculated value...
referencing a post calculated value...
i have a table in which a calculation is done by LaTeX. Actually I am just feeding a module with values and have neither influence of what is calculated nor do I know how exactly it works. I just know from the modules documentation how to put values in and how to get the result out.
My problem is: I want to mention the result of this calculation in the text before the table, but the referenced counter is zero as it's value gets evaluated later on. Any ideas would be appreciated!
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
- Stefan Kottwitz
- Site Admin
- Posts: 10324
- Joined: Mon Mar 10, 2008 9:44 pm
referencing a post calculated value...
welcome to the board!
You could write the counter to the .aux file. Or use the

Stefan
referencing a post calculated value...
i resumed my search with keywords from your answer and found what i was looking for here:
Code: Select all
\documentclass{article}\usepackage{scrlfile}\newcounter{mycounter}\BeforeClosingMainAux{\label{mycounter}}\makeatletter\newcommand*{\demomycounter}{% This is only for demonstration and not part of the suggestion\@whilenum \value{page}<2\do {%Usage of mycounter with value \refstepcounter{mycounter}\themycounter.\par}%}\begin{document}Last value of mycounter was \ref{mycounter}.\demomycounter\end{document}
referencing a post calculated value...
...is it possible that my LaTeX-installation sucks somehow? i executed pdflatex in a loop, so that the reference from the aux-file can be used, but all i get is a
???
and this over and over...Code: Select all
$ pdflatex -interaction=nonstopmode -halt-on-error test4.tex...LaTeX Warning: Reference 'counter' on page 1 undefined on input line 15....$ pdflatex -interaction=nonstopmode -halt-on-error test4.tex...LaTeX Warning: Reference 'counter' on page 1 undefined on input line 15...
- Stefan Kottwitz
- Site Admin
- Posts: 10324
- Joined: Mon Mar 10, 2008 9:44 pm
referencing a post calculated value...
Btw. the error message refers to
counter
, not to mycounter
, so your tested code must be different. The code above works for me, with a correct counter value in the second run. Perhaps check or post the tested code.Stefan
referencing a post calculated value...
sorry for renaming that variable. i changed nothing, but its name. I found a hint in an old bug-report, where LaTeX had problems with special characters in counter variable names. I decided to rewrite and not just copy the code- just for sure.
i tried the snipplet on another machine running FreeBSD 9 and a fresh compiled version of LaTeX and found out, that
\BeforeClosingMainAux
was not defined in \usepackage{scrlfile}
, where I expected it to be- i really don't know why it is not.I also found
\AfterLastShipout
from \usepackage{atveryend}
as an alternative, which was found but produced nothing, where the ???
had been before.finally i edited the code again, replacing the function with what it is expected to do:
Code: Select all
\documentclass{article}% \usepackage{scrlfile} << not needed as it does not work vv\newcounter{mycounter}% \BeforeClosingMainAux{\label{mycounter}} << did not work, not found in scrlfile\makeatletter\newcommand*{\demomycounter}{% This is only for demonstration and not part of the suggestion\@whilenum \value{page}<2\do { %Usage of mycounter with value \refstepcounter{mycounter}\themycounter.\par}%}\begin{document}Last value of mycounter was \ref{mycounter}.\demomycounter\label{mycounter} % << this is where BeforeClosingMainAux should have been called\end{document}
referencing a post calculated value...
if you want to retrieve the last value of a counter you can write it to the
aux
file (as Stefan already said). Here is a short code that does not need any package:Code: Select all
\documentclass{article}\newcounter{mycounter}\makeatletter% this command gets written in the aux file:\def\lastcountervalue#1#2{\expandafter\xdef\csname l@#1@value\endcsname{#2}}% this is the user command to save the last value:\def\savelastvalue#1{%\AtEndDocument{\write\@auxout{\string\lastcountervalue{#1}{\arabic{#1}}}}}% this is the user command to retrieve the last value:\def\getlastvalue#1{%\@ifundefined{l@#1@value}%{??}{\@nameuse{l@#1@value}}}% this will issue a latex warning to rerun when one of the last values has changed:\AtEndDocument{\def\lastcountervalue#1#2{%\def\reserved@a{#2}%\expandafter\ifx\csname l@#1@value\endcsname\reserved@a\else\@tempswatrue\fi}}\makeatother% let's do some stepping:\def\stepfourtimes#1{\stepcounter{#1}\stepcounter{#1}\stepcounter{#1}\stepcounter{#1}}\def\stepfourtimesfour#1{\stepfourtimes{#1}\stepfourtimes{#1}\stepfourtimes{#1}\stepfourtimes{#1}}\begin{document}\savelastvalue{mycounter}\savelastvalue{section}\savelastvalue{page}mycounter: \getlastvalue{mycounter},% should be 16section: \getlastvalue{section},% should be 0page: \getlastvalue{page}% should be 1\stepfourtimesfour{mycounter}\end{document}
-
- Posts: 2
- Joined: Fri Mar 18, 2022 12:50 pm
referencing a post calculated value...
I would like to use the last value further, e.g. to assign this value to a counter and the like. How should I modify the command \getlastvalue for this purpose? - Thanks