![自动将标签放置在行末](https://linux22.com/image/476745/%E8%87%AA%E5%8A%A8%E5%B0%86%E6%A0%87%E7%AD%BE%E6%94%BE%E7%BD%AE%E5%9C%A8%E8%A1%8C%E6%9C%AB.png)
使用 tkiz,我想以自动方式将标签定位在行尾。
不幸的是,“自动”将标签置于行尾的中心。
例子
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\ua[6]{%
\draw (#1) -- (#4)
node[at start, auto=left]{#2}
node[at start, auto=right]{#3}
node[at end, auto=left]{#5}
node[at end, auto=right]{#6};
}
\begin{tikzpicture}[node distance=4cm]
\node(a)[draw, rectangle, align=left]{A\\head};
\node(b)[draw, rectangle, right=of a, align=left]{B\\head};
\node(c)[draw, rectangle, below=of b, align=left]{C\\head};
\node(d)[draw, rectangle, left=of c, align=left]{D\\head};
\ua{a}{a1 label}{a2 label}{b}{b1 label}{b2 label}
\ua{b}{b3 label}{b4 label}{c}{c1 label}{c2 label}
\ua{c}{c3 label}{c4 label}{d}{d1 label}{d2 label}
\ua{d}{d3 label}{d4 label}{a}{a3 label}{a4 label}
\end{tikzpicture}
\end{document}
我希望得到这样的结果(你可以明白为什么我在这里尝试使用带有标签的命令)
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[every node=./style={align=center}, node distance=4cm]
\node(a)[draw, rectangle, align=left]{A\\head};
\node(b)[draw, rectangle, right=of a, align=left]{B\\head};
\node(c)[draw, rectangle, below=of b, align=left]{C\\head};
\draw (a) -- (b)
node[at start, anchor=south west]{a1 label}
node[at start, anchor=north west]{a2 label}
node[at end, anchor=south east]{b1 label}
node[at end, anchor=north east]{b2 label};
\draw (b) -- (c)
node[at start, anchor=north east]{b1 label}
node[at start, anchor=north west]{b2 label}
node[at end, anchor=south east]{c1 label}
node[at end, anchor=south west]{c2 label};
% ... and so on...
\end{tikzpicture}
\end{document}
注意:我了解“pos”、“near end”、“near start”等等,但是使用短标签不会得到很好的结果,而使用长标签仍然会与主节点重叠。
答案1
let
使用、calc
和 的方法label
:
\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\ua[6]{
\draw (#1) -- (#4)
let
\p1=($(#4)-(#1)$),
\n1={atan2(\y1,\x1)} % for PGF < 3.0 atan2(\x1,\y1)
in
node[at start, label={\n1+45:#2}]{}
node[at start, label={\n1-45:#3}]{}
node[at end, label={\n1+135:#5}]{}
node[at end, label={\n1+225:#6}]{};
}
\begin{tikzpicture}[node distance=4cm, label distance=-2mm]
\node(a)[draw, rectangle, align=left]{A\\head};
\node(b)[draw, rectangle, right=of a, align=left]{B\\head};
\node(c)[draw, rectangle, below=of b, align=left]{C\\head};
\node(d)[draw, rectangle, left=of c, align=left]{D\\head};
\ua{a}{a1 label}{a2 label}{b}{b1 label}{b2 label}
\ua{b}{b3 label}{b4 label}{c}{c1 label}{c2 label}
\ua{c}{c3 label}{c4 label}{d}{d1 label}{d2 label}
\ua{d}{d3 label}{d4 label}{a}{a3 label}{a4 label}
\end{tikzpicture}
\end{document}
编辑:在 wrobell 的评论之后,我将其替换为,\n1={90-scalar(atan2(\p1))}
以\n1={atan2(\y1,\x1)}
使代码更具可读性。
答案2
只是一个建议。如果第二个示例对您有用,请编写一个实现它的命令。您需要两个命令,一个用于水平线,另一个用于垂直线。所有水平路径都将从左到右,垂直路径将从上到下。您必须从下一个代码中更正锚点,但它会有所帮助。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\newcommand\horzpath[6]{%
\draw (#1) -- (#4)
node[at start, anchor=south west]{#2}
node[at start, anchor=north west]{#3}
node[at end, anchor=south east]{#5}
node[at end, anchor=north east]{#6};
}
\newcommand\vertpath[6]{%
\draw (#1) -- (#4)
node[at start, anchor=north west]{#2}
node[at start, anchor=north east]{#3}
node[at end, anchor=south east]{#5}
node[at end, anchor=south west]{#6};
}
\begin{tikzpicture}[node distance=4cm]
\node(a)[draw, rectangle, align=left]{A\\head};
\node(b)[draw, rectangle, right=of a, align=left]{B\\head};
\node(c)[draw, rectangle, below=of b, align=left]{C\\head};
\node(d)[draw, rectangle, left=of c, align=left]{D\\head};
\horzpath{a}{a1 label}{a2 label}{b}{b1 label}{b2 label}
\vertpath{b}{b3 label}{b4 label}{c}{c1 label}{c2 label}
\horzpath{d}{d1 label}{d2 label}{c}{c3 label}{c4 label}
\vertpath{a}{a3 label}{a4 label}{d}{d3 label}{d4 label}
\end{tikzpicture}
\end{document}