考虑以下代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (1,1);
\coordinate (B) at (2,2);
\draw[->] (A) -- (B);
\draw[dashed] (0,0) rectangle (3,3) node (C) {};
\draw [solid] ($(A.south west)-(0,1.5)$) -- ++(0.5, 0) node [label =right:Signalling] {};
\end{tikzpicture}
\end{document}
我如何才能以自动化的方式正确地将标签的末端与矩形的边缘对齐?
答案1
两个选择;
使用 TikZ,您可以使用节点的名称以供以后在此上下文中使用;
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (1,1);
\coordinate (B) at (2,2);
\draw[->] (A) -- (B);
\draw[dashed] (0,0) rectangle (3,3) node (C) {};
\draw node[% Note that we are inside a path not inside a node declaration
anchor=east,
append after command={([xshift=-2mm]\tikzlastnode.west) -- ++ (-0.5,0)},
inner sep=0] at (3,-0.5) {Signalling};
\end{tikzpicture}
\end{document}
或者通过pgfplots
实际绘制一个函数而不是将其作为路径给出;
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis line style=dashed,
xtick=\empty,
ytick=\empty,
legend pos={south east}
]
\addplot[->,domain=1:2] {x};\addlegendentry{Signalling}
\end{axis}
\end{tikzpicture}
\end{document}