TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a specific reason that there is no default macro (e.g. \pgfmath{<expression>}) that directly expands to the result of the evaluated <expression>? It always strikes me as particularly cumbersome when I have to type \pgfmathparse{\linewidth-2.7cm}\pgfmathresult just to calculate some length that's only used once.

Since such macros would come quite handy for single-use calculations I tried to define my own, but to no avail. Can you help me out with these definitions or at least expain why such a thing is not possible?

What bothers me is the fact that both my \pgflength and the default \pgfmathresult are just macros (according to \show) and both ultimately expand to a sequence of numbers. :-(



\newcommand\pgflength[1]{\pgfmathparse{#1}\pgfmathresult pt}

% Fails:

% Error message:
% ! Missing number, treated as zero.
% <to be read again> 
%                  \begingroup 
% l.14     \hspace{\pgflength{1cm+2cm}}

% Works:
\hspace{\pgfmathresult pt}

% Curiously this also fails:

% Error message:
% ! Incomplete \iffalse; all text was ignored after line 24.
% <inserted text> 
%                 \fi 

share|improve this question
up vote 14 down vote accepted

\pgfmath is not expandable, and so has to be used with a 'known' output macro to provide the result (for more on expandable code, see for example Tricks to make macros expandable and Why isn't everything expandable?). The LaTeX3 FPU is expandable, and so you can do






The LaTeX3 FPU currently does not have the same coverage as the pgfmath code, and some things will never be possible in an expandable way (for example measuring material typeset into boxes). The expandable FPU is also slower than non-expandable code in many cases (here roughly twice as long is needed for the calculation, although the precision is higher).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.