我希望在以下不起作用的 TikZ 代码中清楚表明完成任务的正确语法是什么?
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (basePt) at (4, 2);
% HOW accomplish this multiplication with hard-coded factors?
\coordinate (newPt) at (0.65,0.65)*basePt; % or perhaps 0.65*basePt
% And how to do it if, instead, a "symbolic name", e.g., myscale, were used?
% and how define that symbolic name?
% Missing here: how to define myscale to be, e.g., 0.65?
\coordinate (newerPt) at myscale*basePt;
\draw (0,0) -- (basePt) -- (newerPt);
\end{tikzpicture}
\end{document}
换句话说,如何使用坐标进行计算——既使用具体数字,又使用这些数字的符号名称——然后如何定义这些符号名称的值?
我的目的是能够轻松地修改代码开头的几个命名变量的值,tikzpicture
而不必在整个代码中更改坐标值。
答案1
该calc
库允许您计算坐标,请参阅第 13.5 节坐标计算在手册中。例如,您可以使用declare function
来定义myscale
。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[
declare function={myscale=0.65;}
]
\coordinate (basePt) at (4, 2);
\coordinate (newPt) at ($0.65*(basePt)$); %
\coordinate (newerPt) at ($myscale*(basePt)$);
\draw (0,0) -- (basePt) node{a} -- (newerPt) node{b};
\end{tikzpicture}
\end{document}