1

I am getting a value by a command. Now I am temporarily changing a saved variable which changes the command output. After I finished some code I want to reset the stored value if the value before the temporare code was 1.

My example can be stripped down to:

\newcommand{\storedvalue}{1}
\newcommand{\getstoredvalue}{\storedvalue}
\newcommand{\resetstoredvalue}{Resetting}

Some text

\let\oldvalue\protect\getstoredvalue
\renewcommand{\storedvalue}{0}

Some text which needs the stored value to be changed

\ifnum\numexpr\oldvalue>0%
    \resetstoredvalue{}%
\fi%

Of course the \getstoredvalue and the \resetstoredvalue are a lot more complicated.

I tried to use \protect. This will return the correct value, not the command. But I get Missing number, treated as zero in my if-clause.

I think this is a rather easy question. But I did not find a solution by googling. Also this may be because I was looking for the wrong searching term.

I hope somebody can help me or post a website where I can find the solution myself.

2

You have to \edef the macro \oldvalue or use

\let\oldvalue\storedvalue

because \let\oldvalue\getoldvalue would make a copy of \getoldvalue, not of its expansion.

\documentclass{article}

\begin{document}

\newcommand{\storedvalue}{1}
\newcommand{\getstoredvalue}{\storedvalue}
\newcommand{\resetstoredvalue}{Resetting}

Some text

\edef\oldvalue{\getstoredvalue} % or \let\oldvalue\storedvalue
\renewcommand{\storedvalue}{0}

Some text which needs the stored value to be changed

\ifnum\oldvalue>0
    \resetstoredvalue{}%
\fi

\end{document}

Note that no % should appear after 0 in \ifnum context.

enter image description here

  • \edef works perfectly. Thank you very much. I cannot save the \storedvalue because in my real code the command is not simply returning the value. - One more question: Why should I leave the % after the 0? I read this very often so I was thinking this is the "convention" to do it. – miile7 Jun 5 '18 at 10:33
  • 1
    @miile7 See tex.stackexchange.com/questions/34844 – egreg Jun 5 '18 at 10:51

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.