1

Note: This seems like it has to be asked somewhere, but my Google-foo is failing me. If someone can point me to it, I can delete this question.

I am trying to save a number to the .aux file, and then import that value and assign it to a counter later. I'm toying with the SageTex code to try and find a way to assign a value to a counter based on a sage variable. However, since sage requires two passes, I am trying to use the zref system to use a temporary value for the counter while writing out the sage variable to the .aux file as a label, then pulling that value in on the second pass.

I am new to the zref system so I may just be missing an obvious command, but I can't seem to find a way to important a value from .aux as an argument in the \setcounter command. Here is a MWE:

\documentclass{article}
\usepackage{zref}
\usepackage{zref-user}
\usepackage{xifthen}

\begin{document}
\makeatother

\zref@newlist{SageStorageList}
\zref@setmainlist{SageStorageList}
\zref@newprop{sageContent}[0]{0}
\zref@addprop{SageStorageList}{sageContent}

\newcommand{\storeSage}[2]{
    \zref@wrapper@immediate{
        \zref@setcurrent{sageContent}{#2}
        \zlabel{#1}
        }
    }

\newcounter{Temp}
\storeSage{TestMe}{5}
\zref[sageContent]{TestMe}
\expandafter\setcounter\expandafter{Temp}\expandafter{\expandafter\zref[sageContent]{TestMe}}

\ifthenelse{\zref[sageContent]{TestMe} > 0}{Ha}{Oh...}

\end{document}

The \expandafter chain is probably overkill but I wanted to ensure the expansion was happening before counter values. Nonetheless I get a "expecting number" error. I tried other expansion configurations in case counters were special for expansion, no luck. This generated the fewest errors (the only error being the "expecting a number" error if you comment out the ifthenelse command).

  • Unfortunately, \expandafter doesn't work the way you want. The second \expandafter will try to expand the T which isn't expandable. – TH. Jun 18 '17 at 3:19
  • Hm, does it need a \bgroup \egroup thing then? – Jason Jun 18 '17 at 3:26
  • No, that wouldn't change anything. \bgroup is just another name for { and in any event, both are single tokens. \expandafter always expands the second token to follow it one time. – TH. Jun 18 '17 at 3:38
  • Oh. Here's a related question where the answer was essentially the same: use \zref@extract. tex.stackexchange.com/questions/95420/… – TH. Jun 18 '17 at 3:52
2

You want to use the expandable version of \zref, \zref@extract. I wrapped that in a \recallSage macro that pairs with your \storeSage macro.

\documentclass{article}
\usepackage{zref}
\usepackage{xifthen}

\makeatletter
\zref@newlist{SageStorageList}
\zref@setmainlist{SageStorageList}
\zref@newprop{sageContent}[0]{0}
\zref@addprop{SageStorageList}{sageContent}

\newcommand*\storeSage[2]{
    \zref@wrapper@immediate{%
        \zref@setcurrent{sageContent}{#2}%
        \zref@label{#1}%
    }%
}
\newcommand*\recallSage[1]{%
    \zref@extract{#1}{sageContent}%
}
\makeatother

\newcounter{Temp}

\begin{document}

\storeSage{TestMe}{5}
\recallSage{TestMe}
\setcounter{Temp}{\recallSage{TestMe}}

\ifthenelse{\recallSage{TestMe} > 0}{Ha}{Oh...}

\end{document}

By using \zref@label, you can avoid the need to use the zref-user package. Now everything is nicely packaged up in the two macros \storeSage and \recallSage.

  • 1
    Perfect, exactly what I was looking for thanks! – Jason Jun 18 '17 at 3:49
1

A version with crossreftools and storing a generic content with \crtcrossreflabel instead of zref.

Use \crtcrossreflabel{content}[labelname] and \crtrefnumber to extract the stored content later on. \crtrefnumber is expandable.

\documentclass{article}

\usepackage{crossreftools}

\crtrefundefinedtext{0}% Returns 0 for undefined references!

\newcommand{\storeSage}[2]{%
  \crtcrossreflabel*{#2}[#1]% 
}

\begin{document}

\newcounter{Temp}

\storeSage{TestMe}{5}


\setcounter{Temp}{\crtrefnumber{TestMe}}

\ifnum0<\crtrefnumber{TestMe}%
Ha!
\else
Oh!
\fi
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.