![为什么当我的“shift”坐标改变时,“above”的含义似乎也改变了?](https://linux22.com/image/290748/%E4%B8%BA%E4%BB%80%E4%B9%88%E5%BD%93%E6%88%91%E7%9A%84%E2%80%9Cshift%E2%80%9D%E5%9D%90%E6%A0%87%E6%94%B9%E5%8F%98%E6%97%B6%EF%BC%8C%E2%80%9Cabove%E2%80%9D%E7%9A%84%E5%90%AB%E4%B9%89%E4%BC%BC%E4%B9%8E%E4%B9%9F%E6%94%B9%E5%8F%98%E4%BA%86%EF%BC%9F.png)
这两个例子之间的唯一区别是的大小xshift
。
带有 和 的 MWExshift=-1.5in
将xshift=-1.0in
文本放置在我期望的位置,但带有xshift=-0.75in
和 的 MWE则不会xshift=-0.5in
:
above
有人能解释一下为什么要改变的解释吗?
\documentclass[border=8pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\newcommand{\MyTixZPicture}[1]{%
\begin{tikzpicture}[c/.style={xshift=#1}]
\node [above left] at (0,0) {\texttt{xshift=$#1$}};
\node[draw,circle,inner sep=1pt,label={right:A}] (A) at (0,0) {};
\node[draw,circle,inner sep=1pt,label={right:B}] (B) at (0,-3in) {};
\node[circle,inner sep=1pt]
(C) at ($([c]A)!0.5!([c]B)$) {C}
edge [->,blue] node [midway,above,red,sloped] {to the top} ([c]A)
edge [->,gray] node [midway,above,blue,sloped] {to the bottom} ([c]B);
\foreach \myn in {A,B}
{
\draw ([c]\myn) ++ (-8pt,0) -- (\myn) ;
}
\end{tikzpicture}%
}%
\begin{document}
\MyTixZPicture{-1.5in}
\MyTixZPicture{-1.0in}
\MyTixZPicture{-0.75in}
\MyTixZPicture{-0.5in}
\end{document}
答案1
这与库中的内部舍入有关calc
。如果我们将计算排除在外,则一切都按预期工作(参见“正确!”和“也正确!”文本):
\documentclass[border=8pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\MyTixZPicture}[1]{%
\begin{tikzpicture}[c/.style={xshift=#1}]
\node [above left] at (0,0) {$\texttt{xshift}=#1$};
\node[draw,circle,inner sep=1pt,label={right:A}] (A) at (0,0) {};
\node[draw,circle,inner sep=1pt,label={right:B}] (B) at (0,-3in) {};
\node[circle,inner sep=1pt]
(C) at ($([c]A)!0.5!([c]B)$) {C}
edge [->,blue] node [midway,above,red,sloped] {to the top} ([c]A)
edge [->,gray] node [midway,above,blue,sloped] {to the bottom} ([c]B)
(C) ++(-0.5,0) -- +(0,3) node[midway,above,sloped] {correct!}
(C) ++(-0.5,0) -- +(0,-3) node[midway,below,sloped] {also correct!};
\foreach \myn in {A,B}
{
\draw ([c]\myn) ++ (-8pt,0) -- (\myn) ;
}
\end{tikzpicture}%
}%
\begin{document}
\foreach \offset in {-1.5,-1.0,-0.75,-0.5} {
\MyTixZPicture{\offset in}
}
\end{document}
我们可以使用较少的代码来复制这种情况,如下所示:
\documentclass[tikz,border=5pt]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \a in {89.99,90.00,90.01} {
\draw (0,0) -- ++(\a:1) node[midway,above,sloped] {\a};
}
\end{tikzpicture}
\end{document}
现在,考虑输出:
直到 90°,线会稍微向右倾斜,这会导致above
节点“位于左侧”。我认为,在 90° 时,这有点儿像掷骰子,但行为始终一致,即 90° 可以“按预期”工作。但任何大于 90° 的角度都会稍微向右倾斜左边,这会导致above
将节点放置在“右侧”。
因此,在您的原始代码中,计算中的任何小的舍入误差($([c]A)!0.5!([c]B)$)
都可能导致绘制的路径向一个方向或另一个方向略微倾斜,从而导致您观察到的行为。
1sp
下面是最后一个例子,显示路径“垂直度”的错误(TeX 的最小单位)足以导致这种情况:
\documentclass[tikz,border=5pt]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \a in {1,0,-1} {
\draw (0,0) -- ++(\a sp,1) node[midway,above,sloped] {\a};
}
\end{tikzpicture}
\end{document}
作为一种解决方法,请避免多次进行移位:改用|-
坐标说明符将保证正交性,并以最小的更改提供所需的结果:
\documentclass[border=8pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\MyTixZPicture}[1]{%
\begin{tikzpicture}[c/.style={xshift=#1}]
\node [above left] at (0,0) {$\texttt{xshift}=#1$};
\node[draw,circle,inner sep=1pt,label={right:A}] (A) at (0,0) {};
\node[draw,circle,inner sep=1pt,label={right:B}] (B) at (0,-3in) {};
\node[circle,inner sep=1pt]
(C) at ($([c]A)!0.5!([c]B)$) {C}
edge [->,blue] node [midway,above,red,sloped] {to the top} (C |- A)
edge [->,gray] node [midway,above,blue,sloped] {to the bottom} (C |- B);
\foreach \myn in {A,B}
{
\draw ([c]\myn) ++ (-8pt,0) -- (\myn) ;
}
\end{tikzpicture}%
}%
\begin{document}
\foreach \offset in {-1.5,-1.0,-0.75,-0.5} {
\MyTixZPicture{\offset in}
}
\end{document}