我仍在学习如何使用pgfkeys
。这是我正在编写的一段代码。它可以很好地编译:
\documentclass{article}
\RequirePackage{xparse}
\RequirePackage{pgfkeys,pgffor}
\makeatletter
\pgfkeys
{ /ae/polynomials/.cd,
base/.initial = x,
coefficients/.initial={3,5,2},
}
\NewDocumentCommand{\polynomial}{ O{}m }
{\pgfqkeys{/ae/polynomials}{coefficients={#2},#1}
\ae@simple@polynomial
}
\def\ae@simple@polynomial
{\begingroup
\pgfkeys
{/ae/polynomials/.cd,
coefficients/.get=\mycoefficients,
base/.get=\myvar,
}%%"
%%\edef\mycoefficients{\pgfkeysvalueof{coefficients}}%%'
\def\mydenominator{}%%'
\def\myfraction{}%%'
%% this next line is to assist in finding the degree of the polynomial
\foreach \coef [count=\coefi] in \mycoefficients{}
\edef\degree{\number\numexpr\coefi\relax}%%'
\foreach \mytmpnumerator/\coef [count=\coefi] in \mycoefficients%%'
{%% find the degree of the current term in the polynomial
\edef\degree{\number\numexpr\degree-\coefi\relax}
%% determine whether there is a fracion present
\ifx\mytmpnumerator\coef\else\def\myfraction{\frac}\def\mydenominator{\coef}\fi
%% examine the coefficient
\ifnum\coefi>1\relax
\ifnum\mytmpnumerator<0\relax
%% subtract the terms: reset numerator to a positive number
-\edef\mytmpnumerator{\number\numexpr0-\mytmpnumerator\relax}%
\else
%% add the terms
+\fi
\fi
%% print the coefficient
\myfraction{\mytmpnumerator}{\mydenominator}
%% print out the variables and their powers
\ifcase\degree\relax
\or
\myvar\else
\myvar^{\degree}
\fi
%%'
}
\endgroup
}
\makeatother
\begin{document}
\[
\polynomial[coefficients={3,2,4/3}]{}
\]
\end{document}
现在pgf
手册中有以下示例:
\pgfkeys{/text/.store in=\mytext}
\def\a{world}
\pgfkeys{/text=Hello \a!}
\def\a{Gruffalo}
\mytext
这看起来很简单。但是当我按照这个例子重写代码时,我得到了错误。
我的第一次尝试是做这样的事情:
\def\ae@simple@polynomial
{\begingroup
\pgfkeys
{/ae/polynomials/.cd,
coefficients/.estore in=\mycoefficients,
base/.estore in=\myvar,
}%%'
....
<Remainder of definition is same as original MWE>
但这会导致错误:
! Undefined control sequence.
<argument> \mycoefficients
l.59 \polynomial[coefficients={3,2,4/3}]{}
?
这似乎与手册的建议不符pgf
。
尽管如此,我还是尝试了其他方法:
\def\ae@simple@polynomial
{\begingroup
\def\mycoefficients{}%%'
\def\myvar{}%%'
\pgfkeys
{/ae/polynomials/.cd,
coefficients/.estore in=\mycoefficients,
base/.estore in=\myvar,
}%%'
....
<Remainder of definition is same as original MWE>
这次我没有收到错误,但\pgfkeys
没有将我的值存储在变量中。
我究竟做错了什么?
更新
我以前做过类似的事情:
\edef\mycoefficients{\pgfkeysvalueof{/ae/polynomials/coefficients}}`
这样做是可行的。但我想偷懒,避免必须为每个尝试定义的内部宏写出键的完整路径名。