有没有办法在 TikZ 中指定“绝对打印距离”,以便即使图像被缩放,某些距离仍保持完整并以指定的距离打印?例如
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\draw (A)--node[below]{4}(B);
\node at ($(A)+(180:.3cm)$){A};
\node at ($(B)+(0:.3cm)$){B};
\end{tikzpicture}
\hskip5mm
\begin{tikzpicture}[scale=0.1]
\coordinate (A) at (0,0);
\coordinate (B) at (40,0);
\draw (A)--node[below]{40}(B);
\node at ($(A)+(180:.3cm)$){A};
\node at ($(B)+(0:.3cm)$){B};
\end{tikzpicture}
请注意“下方”保持不变,但节点文本的距离在缩放版本中较小。有没有办法指定“.3cm”,使其在打印输出时为 .3cm,而不受图像缩放的影响?TIA。
答案1
设置在与坐标或线条有指定距离的标签不受缩放的影响,因此请用 A 和 B 标记线条的末端,而不是将标签构建为单独的节点。
\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate[label={left=0.3cm:A}] (A) at (0,0);
\coordinate[label={right=0.3cm:B}] (B) at (4,0);
\draw (A)--node[below]{4}(B);
\end{tikzpicture}
\begin{tikzpicture}[scale=0.1]
\coordinate[label={left=0.3cm:A}] (A) at (0,0);
\coordinate[label={right=0.3cm:B}] (B) at (40,0);
\draw (A)--node[below]{40}(B);
\end{tikzpicture}
\end{document}
TikZ 提供了多种放置标签的选项。请参阅TikZ 手册了解详情。下面,标签向上移动了3mm
(除了水平距离之外)。
\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate[label={[yshift=3mm]left=3mm:A}] (A) at (0,0);
\coordinate[label={[yshift=3mm]right=3mm:B}] (B) at (4,0);
\draw (A)--node[below]{4}(B);
\end{tikzpicture}
\begin{tikzpicture}[scale=0.1]
\coordinate[label={[yshift=3mm]left=3mm:A}] (A) at (0,0);
\coordinate[label={[yshift=3mm]right=3mm:B}] (B) at (40,0);
\draw (A)--node[below]{40}(B);
\end{tikzpicture}
\end{document}
答案2
另一种可能性:coordinate [xshift=..., yshift=...]
不受缩放的影响。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagenumbering{gobble}
\begin{tikzpicture}[scale=2]
%\draw (0, 0) -- (1, 1) -- ++(1, 0); % doesn't work
%\draw (0, 0) -- (1, 1) -- ++([xshift=1cm] 0, 0); % doesn't work
\draw (0, 0)
-- (1, 1)
coordinate [xshift=1cm] (tmp)
-- (tmp);
\end{tikzpicture}
\end{document}
或者,anchor
按照平常的方式使用。
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,0);
\draw (A)--node[below]{4}(B);
\node [anchor=0, outer sep=0.2cm] at (A){A};
\node [anchor=180, outer sep=0.2cm] at (B){B};
\end{tikzpicture}
\hskip5mm
\begin{tikzpicture}[scale=0.1]
\coordinate (A) at (0,0);
\coordinate (B) at (40,0);
\draw (A)--node[below]{40}(B);
\node [anchor=0, outer sep=0.2cm] at (A){A};
\node [anchor=180, outer sep=0.2cm] at (B){B};
\end{tikzpicture}
并outer sep
控制间距。