這肯定是另一個與擴展相關的問題,因為我不知道為什麼以下
\def\MaxValueOfTok{8}%
\newtoks{\SomeTokDefinedViaDef}
\SomeTokDefinedViaDef={\MaxValueOfTok}
\pgfmathsetmacro{\Difference}{10-\the\SomeTokDefinedViaDef}%
失敗了
包 PGF 數學錯誤:未知運算子
8' or
8@'(在“10-8”中)。
如果可能的話,則偏好僅需要更改\pgfmathsetmacro
計算的解決方案,而不需要對令牌的定義方式進行任何更改。或者,調整\def
設定方式也可以。
取消註解 MWE 中的最後兩行將重現該問題。
代碼:
\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}
答案1
您可以預先進行完全擴展:
\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}
答案2
您必須使用 手動擴展它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}
答案3
pgfmath
另一種選擇是從捆綁包l3fp
中替換l3kernel
。中的解析器l3fp
負責擴展所有內容;它也解\toks
壓縮和其他暫存器的內容。不需要\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}
有幾點要注意:
- 目前,
pgfmath
提供的功能多於l3fp
; - 計算
l3fp
為很多更精確(16 位元浮點數,相對於 TeX 的尺寸範圍); 速度相當;
一些函數很可能永遠不會在表達式中使用
l3fp
,即框測量(\widthof
...)和隨機數。