6

To demonstrate, here's a MWE:

\documentclass[a4paper,titlepage]{article}

\usepackage{pgf}
\usepackage{siunitx}

\begin{document}
    \pgfmathparse{1 + 1}
    \pgfmathprintnumberto{\pgfmathresult}{\calcResult}

    % Both this line
    \pgfmathparse{\calcResult + 1}

    % and this one
    \SI{\calcResult}{\cm}

    % do not work.
\end{document}

The errors I see are:

! Package PGF Math Error: Unknown operator `$' or `$2' (in '$2$+ 1').

! siunitx error: "invalid-token-in-number"
!
! Invalid token '$' in numerical input.

! siunitx error: "invalid-number"
!
! Invalid numerical input '$2$'.
!
! See the siunitx documentation for further information.
4
  • \pgfmathprintnumberto{2}{\calcResult} saves $2$. You can use \pgfmathsetmacro{\calcResult}{1+1}
    – user11232
    Commented Mar 27, 2015 at 0:16
  • @HarishKumar That makes sense, but I'd like to be able to use the rounded version provided by \pgfmathprintnumberto in the same way I can use the macro defined by \pgfmathsetmacro.
    – the6p4c
    Commented Mar 27, 2015 at 0:21
  • Use int\pgfmathsetmacro{\calcResult}{int(1+1)}.
    – user11232
    Commented Mar 27, 2015 at 0:22
  • Answer added. :)
    – user11232
    Commented Mar 27, 2015 at 0:37

2 Answers 2

7

\pgfmathprintnumberto{2}{\calcResult} results in $2$. You can use \pgfmathsetmacro{\calcResult}{1+1} directly. To round the number to integers you can use int as in \pgfmathsetmacro{\calcResult}{int(1+1)}

\documentclass[a4paper,titlepage]{article}

\usepackage{pgf}
\usepackage{siunitx}

\begin{document}

    \pgfmathsetmacro{\calcResult}{int(1+1)}
    \pgfmathprintnumber{\calcResult}

    % Both this line
    \pgfmathparse{\calcResult + 1}
    \pgfmathresult

    % and this one
    \SI{\calcResult}{\cm}

    % do not work.
\end{document}

enter image description here

4

Harish's answer is spot on, but an alternative is to use the LaTeX3 FPU to do the work

\documentclass[a4paper,titlepage]{article}

\usepackage{siunitx}
\usepackage{expl3}
\ExplSyntaxOn
  \cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\begin{document}

\SI{\fpeval{1 + 1}}{\cm}

\SI[round-integer-to-decimal, round-mode = places, round-precision = 1]%
  {\fpeval{1 + 1}}{\cm}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .