使用 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}