Tikz 坐标分数推理

Tikz 坐标分数推理

在我的代码中,我定义如下:

\def\ctrlHeight{5};
\def\ctrlHeightSpacing{5/32 * \ctrlHeight};

我这样做了:

\draw($(regWrtWire -| buff3.west) + (0, \ctrlHeightSpacing/2 cm)$) -- ($(regWrtWire -| buff3.east) + (0, 0.5*\ctrlHeightSpacing cm)$);

输出:

在此处输入图片描述

在图像中,左侧部分位于右侧部分下方,右侧部分位于正确位置。当我乘以 0.5 而不是除以 2 时,输出结果为

在此处输入图片描述

可能是什么原因?

顺便提一下,如果我这样做

\def\ctrlHeightSpacing{5*\ctrlHeight/32};

我得到的值不正确,不是 5*5/32 = 0.78125,而这应该是正确的值。可能是什么问题?

答案1

像往常一样,从片段中烘焙出一个最小示例并不是一件很有趣的事情,而且声称文档很大并不是让其他人完成所有工作的好借口。但就您而言,这种行为不是由于未公开的代码片段造成的。相反,这只是因为 pgf 按照其规则计算了您希望它计算的内容(至少对我来说这很有意义)。您可能希望在蓝色路径中进行计算。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\ctrlHeight{5}%<- a semicolon here is unnecessary
\def\ctrlHeightSpacing{5/32 * \ctrlHeight}%<- this is also a strange 
% practice. It would make more sense to say
% \pgfmathsetmacro\ctrlHeightSpacing{5/32 * \ctrlHeight}
\node[minimum width=2cm] (buff3){};
\coordinate (regWrtWire);
\draw($(regWrtWire -| buff3.west) + (0, \ctrlHeightSpacing/2 cm)$) -- ($(regWrtWire -| buff3.east) + (0, 0.5*\ctrlHeightSpacing cm)$);
\pgfmathsetmacro{\yleft}{\ctrlHeightSpacing/2 cm}
\pgfmathsetmacro{\yright}{0.5*\ctrlHeightSpacing cm}
\pgfmathsetmacro{\onecm}{1cm}
\path (-1,-1) node[text width=0.8\textwidth,align=left,anchor=north west]
{On the left side, you get 
$\displaystyle\frac{\ctrlHeightSpacing}{2\,\mbox{cm}}=
\frac{25/32}{2\cdot\onecm\,\mbox{pt}}=\yleft\,$pt (because pgf always converts
dimensionful quantities to something that is linear in pt) and 
on the right side
$0.5*\ctrlHeightSpacing\,\mbox{cm}=\displaystyle\frac{25}{64}\cdot\onecm\,\mbox{pt}=\yright\,\mbox{pt}$.};
% you probably want
\draw[blue]($(regWrtWire -| buff3.west) + (0,{( \ctrlHeightSpacing/2)*1 cm})$) -- ($(regWrtWire -| buff3.east) + (0, 0.5*\ctrlHeightSpacing cm)$);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容