我想将标签放置在边缘下方,这样字母就不会晃动。我可以将它们放置在在边缘使用mid
锚点,但是当我使用below
其他锚点来计算距离时。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\tikzstyle{vertex}=[circle, draw, inner sep=0.5ex]
\node [vertex] (0) {};
\foreach \n/\e in {1/a,2/b,3/c,4/d,5/e,6/f,7/g,8/h} {
\pgfmathtruncatemacro{\left}{\n-1}
\node [vertex] (\n) [right=of \left] {};
\draw (\left) -- node[anchor=mid] {\e} (\n); %on the edge, no wobble
\draw (\left) -- node [below] {\e} (\n); % below the edge, wobble
};
\end{tikzpicture}
\end{document}
答案1
node[below]
意味着node[anchor=north]
标签会因为高度不同而摇晃。如果希望所有标签对齐,可以选择base
锚点,这样所有标签都会与其基线对齐。
\documentclass[tikz,border=2mm]{standalone}
%\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\tikzstyle{vertex}=[circle, draw, inner sep=0.5ex]
\node [vertex] (0) {};
\foreach \n/\e in {1/a,2/b,3/c,4/d,5/e,6/f,7/g,8/h} {
\pgfmathtruncatemacro{\left}{\n-1}
\node [vertex] (\n) [right=of \left] {};
\draw (\left) -- node [draw, below=5mm, anchor=base] (\e) {\e} (\n); % below the edge, don't wobble
};
\draw[red] (a.base)--(h.base);
\begin{scope}[yshift=-1cm]
\node [vertex] (0) {};
\foreach \n/\e in {1/a,2/b,3/c,4/d,5/e,6/f,7/g,8/h} {
\pgfmathtruncatemacro{\left}{\n-1}
\node [vertex] (\n) [right=of \left] {};
\draw (\left) -- node [draw, below] (\e) {\e} (\n); % below the edge, wobble
};
\draw[red] (a.north)--(h.north);
\end{scope}
\end{tikzpicture}
\end{document}