Document Classesreferencing a post calculated value...

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
skuld
Posts: 4
Joined: Tue Jul 03, 2012 1:01 am

referencing a post calculated value...

Post by skuld »

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!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

referencing a post calculated value...

Post by Stefan Kottwitz »

Hi skuld,

welcome to the board!

You could write the counter to the .aux file. Or use the totcount package.

Stefan
LaTeX.org admin
skuld
Posts: 4
Joined: Tue Jul 03, 2012 1:01 am

referencing a post calculated value...

Post by skuld »

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:

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}
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!
Last edited by Stefan Kottwitz on Tue Jul 03, 2012 12:39 pm, edited 1 time in total.
skuld
Posts: 4
Joined: Tue Jul 03, 2012 1:01 am

referencing a post calculated value...

Post by skuld »

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 ??? 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
...
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

referencing a post calculated value...

Post by Stefan Kottwitz »

Was an aux file produced?
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
skuld
Posts: 4
Joined: Tue Jul 03, 2012 1:01 am

referencing a post calculated value...

Post by skuld »

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 \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}
...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...
Last edited by Stefan Kottwitz on Tue Jul 03, 2012 5:06 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

referencing a post calculated value...

Post by cgnieder »

Hi,

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}
Best regards
site moderator & package author
GuntherReissig
Posts: 2
Joined: Fri Mar 18, 2022 12:50 pm

referencing a post calculated value...

Post by GuntherReissig »

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
Post Reply