2

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?

4

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

  • +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
2

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

  • 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.