I really need help, as this problem took me hours and I feel something like out-of-options :/
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!
Document Classes ⇒ referencing a post calculated value...
- Stefan Kottwitz
- Site Admin
- Posts: 10290
- Joined: Mon Mar 10, 2008 9:44 pm
referencing a post calculated value...
Hi skuld,
welcome to the board!
You could write the counter to the .aux file. Or use the
totcount package.
Stefan
welcome to the board!
You could write the counter to the .aux file. Or use the

Stefan
LaTeX.org admin
referencing a post calculated value...
totcount does not get installed on my linux-distribution by default. i consider this a disadvantage as i am looking for a robust solution.
i resumed my search with keywords from your answer and found what i was looking for here:
thank you so much! that really helped a lot. it must have been nearly impossible to find the solution with the search-terms i used before. this thread might be short, but might help others as well. thanks again!
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}
Last edited by Stefan Kottwitz on Tue Jul 03, 2012 12:39 pm, edited 1 time in total.
referencing a post calculated value...
damn. fail! i understand how it is meant to work, but the above example does not work for me :/
...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
...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: 10290
- Joined: Mon Mar 10, 2008 9:44 pm
referencing a post calculated value...
Was an aux file produced?
Btw. the error message refers to
Stefan
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
LaTeX.org admin
referencing a post calculated value...
interesting.
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
I also found
finally i edited the code again, replacing the function with what it is expected to do:
...and got my first working version either using latex or pdflatex as a result. i really hate it, when it gets more complicated, than it should be, because that always gets error prone. Even so in this case i have no clue why...
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}
Last edited by Stefan Kottwitz on Tue Jul 03, 2012 5:06 pm, edited 1 time in total.
referencing a post calculated value...
Hi,
if you want to retrieve the last value of a counter you can write it to the
Best regards
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 16
section: \getlastvalue{section},% should be 0
page: \getlastvalue{page}% should be 1
\stepfourtimesfour{mycounter}
\end{document}
site moderator & package author
-
- Posts: 2
- Joined: Fri Mar 18, 2022 12:50 pm
referencing a post calculated value...
I have the following question regarding the answer by cgnieder (Tue Jul 03, 2012 11:09 pm):
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
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