I am using the fp package to write a coursework in mechanics and calculate the numerical values on the fly. Here is some example code:

\usepackage[nomessages]{fp}  % http://ctan.org/pkg/fp


P_output_kW = \FPprint{P_output_kW} kW
n_output_max_rpm = \FPprint{n_output_max_rpm} rpm

As is evident, I am attempting to print every assigned variable by name, value and measurement unit. First of all, the above code does not compile with the error:

! Missing $ inserted.

I would like to invoke into a single command (with a single parameter) the two operations:

  • print variable name, followed by =
  • print variable value

How can that be achieved?


The following sets the variable name in typewriter font, the number via \FPprint and the unit is taken from the variable name:

\usepackage[nomessages]{fp}  % http://ctan.org/pkg/fp

    \fontfamily{lmvtt}\selectfont % variable typewriter font
    % alternative: \ttfamily
    \detokenize{#1}% make _ to character
  \catcode`\_=12 %




  • +1 I liked your answer so much than mine, than I've deleted it :) – Gonzalo Medina Sep 25 '15 at 0:21
  • Thanks a ton and let the code be with you. I copied your code, quoted your name and this url. Only change is I put /FPset inside the command. This way setting any variable in code results in text output as well and I would expect that to protect from weird bugs with the calculations. – Vorac Sep 25 '15 at 10:43

This uses \csname.


\newcommand{\FPset}[2]% #1 = varaible name, #2 = value
  {\expandafter\gdef\csname FPV#1\endcsname{#2}}

\newcommand{\FPunit}[2]% #1 = variable name, #2 = units
  {\expandafter\gdef\csname FPU#1\endcsname{#2}}

\newcommand{\FPmacro}[2]% #1 = variable name, #2 = macro to store value
  {\edef #2{\csname FPV#1\endcsname}}

\newcommand{\FPprint}[1]% #1 = variable name
  {#1 $=$ \num{\csname FPV#1\endcsname} \si{\csname FPU#1\endcsname}}

\FPunit{power}{\si{\kilo\watt}}% predefined from sciunitx




  • Hey, it appears both answers work fine, I just picked the first one. Thanks for the work and for letting me know about sciunitx. +1 – Vorac Sep 25 '15 at 10:45

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.