3

I have a document with several commands that store numeric variables inside an extra macro. This extra macro will highlight that text so I can track them in the printed document. Like so:

\newcommand{\ScriptValue}[1]{\colorbox[gray]{0.8}{#1}}

\newcommand{\HTwoA}{\ScriptValue{10.3}}
\newcommand{\HTwoB}{\ScriptValue{11.3}}
\newcommand{\HTwoC}{\ScriptValue{10.7}}

I make extensive use of them in the text and use the fp and intcalc packages to compute new values. Those newly computed values should continue to be automatically highlighted with \ScriptValue.

In order to do this, I renew the commands from the fp package to a version where \ScriptValue is locally set to an empty macro. This was very much based in an answer from here too. Like so:

\documentclass[article,draft]{memoir}

\usepackage{color}
\usepackage{fp}

\newcommand{\ScriptValue}[1]{\colorbox[gray]{0.8}{#1}}
\let\RealScriptValue\ScriptValue

\newcommand{\HTwoA}{\ScriptValue{10.3}}
\newcommand{\HTwoB}{\ScriptValue{11.3}}
\newcommand{\HTwoC}{\ScriptValue{10.7}}

\let\RealFPmin\FPmin
\renewcommand{\FPmin}[3]{%
  \renewcommand{\ScriptValue}[1]{##1}%
  \RealFPmin{\UnmarkedResult}{#2}{#3}%
  \renewcommand{\ScriptValue}{\RealScriptValue}%
  \FPset{#1}{\ScriptValue{\UnmarkedResult}}%
}

\FPmin{\HTwoAB}{\HTwoA}{\HTwoB}

\begin{document}
  Min of A and B is \HTwoAB{}
\end{document}

Until this point, it works fine. However, I am having issues when I want to use those newly computed values in the overloaded macros one more time. For example, with the above overload of \FPmin the following fails:

\FPmin{\result}{\HTwoA}{\HTwoB}
\FPmin{\HMinAll}{\result}{\HTwoC}

This is certainly due to me messing up the overloading since it all works fine if there is no \ScriptValue in the first place. The following would work perfectly:

\documentclass[article,draft]{memoir}

\usepackage{fp}

\newcommand{\HTwoA}{10.3}
\newcommand{\HTwoB}{11.3}
\newcommand{\HTwoC}{10.7}

\FPmin{\result}{\HTwoA}{\HTwoB}
\FPmin{\HMinAll}{\result}{\HTwoC}

\begin{document}
  Min of all is \HMinAll{}
\end{document}

I believe my issue to be at the line where I call \FPset. I have tried multiple different things with lets, and expandafters, and defss, and csedefs, but still got nothing that worked reliably.

How can this be done correctly? I want this to work "invisibly". The point is for other people working on the document with me, to not worry with \ScriptValue. To have things just working for them as if they were dealing with "just" numeric variables when they call the functions from fp and intcalc.

For what is worth, I am already using the etoolbox if that makes the problem simpler.

  • I think you're better avoiding such tricks. Using \ScriptValue{\HMinAll} when you need the box seems much better and frees you from these complications. – egreg Dec 17 '15 at 21:03
3

You were never restoring the definition (there was no local scope around the \renewcommand. this seems to do what you want:

\documentclass[article,draft]{memoir}

\usepackage{color}
\usepackage{fp}

\newcommand{\ScriptValue}[1]{\colorbox[gray]{0.8}{#1}}
\let\RealScriptValue\ScriptValue

\newcommand{\HTwoA}{\ScriptValue{10.3}}
\newcommand{\HTwoB}{\ScriptValue{11.3}}
\newcommand{\HTwoC}{\ScriptValue{10.7}}

\let\RealFPmin\FPmin
\renewcommand{\FPmin}[3]{%
  \renewcommand{\ScriptValue}[1]{##1}%
  \RealFPmin{\UnmarkedResult}{#2}{#3}%
  \edef#1{\noexpand\ScriptValue{\UnmarkedResult}}%
  \let\ScriptValue\RealScriptValue
}

\FPmin{\HTwoAB}{\HTwoA}{\HTwoB}

\FPmin{\result}{\HTwoA}{\HTwoB}
\FPmin{\HMinAll}{\result}{\HTwoC}

\begin{document}
  Min of A and B is \HTwoAB{}

  Min of A and B is \HMinAll{}
\end{document}
1

Not really an answer, but an advice: you're probably trying to overload fp commands because they're clumsy and difficult to manage. Here's a better interface.

\documentclass[article,draft]{memoir}

\usepackage{xcolor}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\printvalue}{m}
 {
  \colorbox[gray]{0.8}{ \fp_eval:n { #1 } }
 }
\ExplSyntaxOff

\newcommand{\HTwoA}{10.3}
\newcommand{\HTwoB}{11.3}
\newcommand{\HTwoC}{10.7}

\begin{document}
Min of A and B is \printvalue{ min(10.3,11.3) }

Max of A and C is \printvalue{ max(\HTwoA,\HTwoC) }
\end{document}

The floating point module of expl3 features a very natural syntax and supports much more function than the old fp package.

enter image description here

  • (+1) for 'they're clumsy and difficult to manage'. – cfr Dec 17 '15 at 22:20

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.