Take the 2-minute tour ×
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.

This must be another of those expansion related issues as I don't know why the following

\def\MaxValueOfTok{8}%
\newtoks{\SomeTokDefinedViaDef}
\SomeTokDefinedViaDef={\MaxValueOfTok}

\pgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedViaDef}%

fails with

Package PGF Math Error: Unknown operator 8' or8@' (in '10-8').

If possible, would prefer a solution that requires only changing the \pgfmathsetmacro computation, and not require any changes to how the token is defined. Alternatively, adjusting how the \def is set up would work as well.

Un-commenting the last two lines in the MWE will reproduce the problem.

Code:

\documentclass{article}
\usepackage{tikz}% Easy way to get pgfmath

\def\MaxValueOfTok{8}%

\newtoks{\SomeTokDefinedDirectly}
\newtoks{\SomeTokDefinedViaDef}

\SomeTokDefinedDirectly={7}
\SomeTokDefinedViaDef={\MaxValueOfTok}

\begin{document}
\pgfmathsetmacro{\Difference}{10-5}%
Differerence is \Difference (numerical).

\pgfmathsetmacro{\Difference}{10-\MaxValueOfTok}%
Differerence is \Difference (using def).

\pgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedDirectly}%
Differerence is \Difference (using SomeTokDefinedDirectly).

%\pgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedViaDef}%
%Differerence is \Difference (using SomeTokDefinedViaDef).
\end{document}
share|improve this question
    
\usepackage{pgf} is better. –  percusse Aug 28 '12 at 8:57

3 Answers 3

up vote 9 down vote accepted

You can do complete expansion beforehand:

\documentclass{article}
\usepackage{pgf}% Easy way to get pgfmath

\def\epgfmathsetmacro#1#2{\begingroup
  \edef\x{\endgroup\noexpand\pgfmathsetmacro\noexpand#1{#2}}\x}

\def\MaxValueOfTok{8}%

\newtoks\SomeTokDefinedDirectly
\newtoks\SomeTokDefinedViaDef

\SomeTokDefinedDirectly={7}
\SomeTokDefinedViaDef={\MaxValueOfTok}

\begin{document}
\epgfmathsetmacro{\Difference}{10-5}%
Differerence is \Difference (numerical).

\epgfmathsetmacro{\Difference}{10-\MaxValueOfTok}%
Differerence is \Difference (using def).

\epgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedDirectly}%
Differerence is \Difference (using SomeTokDefinedDirectly).

\epgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedViaDef}%
Differerence is \Difference (using SomeTokDefinedViaDef).
\end{document}
share|improve this answer

You'll have to expand it manually, using 10\expandafter-\the\SomeTokDefinedViaDef.

\documentclass{article}
\usepackage{tikz}% Easy way to get pgfmath

\def\MaxValueOfTok{8}%

\newtoks{\SomeTokDefinedDirectly}
\newtoks{\SomeTokDefinedViaDef}

\SomeTokDefinedDirectly={7}
\SomeTokDefinedViaDef={\MaxValueOfTok}

\begin{document}
\pgfmathsetmacro{\Difference}{10-5}%
Differerence is \Difference (numerical).

\pgfmathsetmacro{\Difference}{10-\MaxValueOfTok}%
Differerence is \Difference (using def).

\pgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedDirectly}%
Differerence is \Difference (using SomeTokDefinedDirectly).

\pgfmathsetmacro{\Difference}{10\expandafter-\the\SomeTokDefinedViaDef}%
Differerence is \Difference (using SomeTokDefinedViaDef).
\end{document}
share|improve this answer

Another option is to replace pgfmath by l3fp from the l3kernel bundle. The parser in l3fp takes care of expanding everything; it also unpacks the contents of \toks and other registers. No need for \the.

\documentclass{article}

%% Load expl3 for l3fp, and xparse for \NewDocumentCommand.
\usepackage{expl3, xparse}
\ExplSyntaxOn
\NewDocumentCommand {\explfpsetmacro} { m m }
  { \tl_set:Nx #1 { \fp_to_tl:n {#2} } }
\ExplSyntaxOff
%%

\def\MaxValueOfTok{8}%

\newtoks{\SomeTokDefinedDirectly}
\newtoks{\SomeTokDefinedViaDef}

\SomeTokDefinedDirectly={7}
\SomeTokDefinedViaDef={\MaxValueOfTok}

\begin{document}
\explfpsetmacro{\Difference}{10-5}%
Differerence is \Difference (numerical).

\explfpsetmacro{\Difference}{10-\MaxValueOfTok}%
Differerence is \Difference (using def).

\explfpsetmacro{\Difference}{10-\SomeTokDefinedDirectly}%
Differerence is \Difference (using SomeTokDefinedDirectly).

\explfpsetmacro{\Difference}{10-\SomeTokDefinedViaDef}%
Differerence is \Difference (using SomeTokDefinedViaDef).
\end{document}

A few things to note:

  • at the moment, pgfmath provides more functions than l3fp;
  • computations with l3fp are much more precise (16 digits floating points, versus the range of TeX's dimensions);
  • speed is comparable;

  • a few functions will most likely never be implemented for use within l3fp expressions, namely box measurements (\widthof...) and random numbers.

share|improve this answer

Your Answer

 
discard

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.