我想指定\newcommand
使用并作为变量pgfkeys
进入环境的参数的单位,如下所示:\tikzmath
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{math}
\pgfkeys{
/myfam/.is family, /myfam,
default/.style = {length = 10cm},
length/.estore in = \mylen,
}
\newcommand{\myline}[1][]{
\pgfkeys{/myfam, default, #1}
\tikzmath{
real \mylen, \mylennew;
\mylennew = \mylen+1; % do some math operation here
}
\draw (0,0) -- (\mylennew, \mylennew); % this will specify the units as cm
}
\begin{document}
\begin{tikzpicture}
\myline[length=1] % the argument has no units here
\end{tikzpicture}
\end{document}
在上文中,命令\myline[length=1]
中的变量结果\draw
具有单位cm
。
我的问题是:
在环境之后,我如何在命令中
\tikzmath
指定变量的单位;可能是这样的:\mylennew
\draw
\draw (0,0) -- (\mylennew cm, \mylennew pt);
如果我想执行类似
\myline[length=1cm]
或 的操作\myline[length=1pt]
,它不会给出我想要的结果。我该如何\newcommand
使用的参数指定单位pgfkeys
。
答案1
我发现,tikzmath 会将任何以厘米(或毫米)为单位的变量转换为相应的点值(1 厘米 = 28.45274 pt),而不会保留单位(即点)。这是 Tikzmath 块中发生的情况:
\x = 1cm;
\newx = \x; %\newx has a value of 28.45274
我猜测这也与 Tex 数学引擎有关?
我根据不同的例子提供了两种解决方案:
\documentclass[]{article}
\usepackage{tikz}
\usepackage{amsmath,amsthm}
\usetikzlibrary{math, calc}
\pgfkeys{
/testfam/.is family, /testfam,
default/.style={x = 1cm, y = 1},
x/.estore in = \x, y/.estore in = \y}
\newcommand{\testfam}[1][]{
\pgfkeys{/testfam, default, #1}
\tikzmath{
real \x, \y, \newx, \newy;
coordinate \cd;
\newx1 = \x;
\newx2 = \x/28.45274;
\newy = \y;
\cd1 = (\newx1 pt,1 cm);
\cd2 = (2 cm,1 cm);
\cd3 = (\newx2 cm,2 cm);
\cd4 = (2 cm,2 cm);
\cd5 = (4 cm,0 cm);
\cd6 = (4 cm,\y cm);
print {$x=\x $};
print {$newx1=\newx1 $};
print {$newx2=\newx2 $};
print {$y=\y $}
print {$newy=\newy $};
}
\draw [line width = 1pt] (\cd1) -- (\cd2);
\draw [line width = 1pt, color=blue] (\cd3) -- (\cd4);
\draw [line width = 1pt, color=red] (\cd5) -- (\cd6);
}
\begin{document}
\begin{tikzpicture}
\testfam[x = 1cm, y=1] %Two cases: with and without units,
\end{tikzpicture}
\end{document}