Question: 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:

```
\documentclass{article}
\usepackage[nomessages]{fp}  % http://ctan.org/pkg/fp
\begin{document}

\section{Requirements}
\FPset{P_output_kW}{11}
\FPset{n_output_max_rpm}{5000}

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

```

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?

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

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

\newcommand*{\VarOutput}[1]{%
  \begingroup
    \fontencoding{T1}%
    \fontfamily{lmvtt}\selectfont % variable typewriter font
    % alternative: \ttfamily
    \detokenize{#1}% make _ to character
  \endgroup
  ~=~%
  \FPprint{#1}%
  \,%
  \ExtractUnit{#1}%
}
\newcommand*{\ExtractUnit}[1]{%
  \expandafter\ExtractUnitAux\detokenize{#1_}\relax
}
\begingroup
  \catcode`\_=12 %
  \gdef\ExtractUnitAux#1_#2\relax{%
    \ifx\\#2\\%
      #1
    \else
      \ExtractUnitAux#2\relax
    \fi
  }
\endgroup

\begin{document}
\section{Requirements}
\FPset{P_output_kW}{11}
\FPset{n_output_max_rpm}{5000}

\VarOutput{P_output_kW}\\
\VarOutput{n_output_max_rpm}
\end{document}

```

> 
> [![Result](https://i.stack.imgur.com/3QRTo.png)](https://i.stack.imgur.com/3QRTo.png)
> 
> 
>
This uses \csname.

```
\documentclass{article}
\usepackage{showframe}
\usepackage{siunitx}
\usepackage{pgfmath}
\parindent=0pt

\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}}

\begin{document}
\FPset{power}{5}
\FPunit{power}{\si{\kilo\watt}}% predefined from sciunitx
\FPprint{power}

\FPset{time}{2}
\FPunit{time}{\si{\hour}}
\FPprint{time}\par

\FPmacro{power}{\x}
\FPmacro{time}{\t}
\pgfmathparse{\x*\t}
\FPset{energy}{\pgfmathresult}
\FPunit{energy}{\si{\kilo\watt\hour}}
\FPprint{energy}
\end{document}

```

[![units](https://i.stack.imgur.com/F7Jd9.png)](https://i.stack.imgur.com/F7Jd9.png)