`TikZ` 图中节点对齐不正确(版本 2)

`TikZ` 图中节点对齐不正确(版本 2)

以下代码呈现了数字线的图表。我在上面标注了 -8、a、e 和 10。(我将图放大了 3/4。)从节点命令排版的 a、e 和 10 标签沿着它们下方的绿色水平线完美对齐。为什么 -8 没有在同一条水平线上对齐?

\documentclass{amsart}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}


\begin{document}    
\begin{tikzpicture}

\coordinate (-8) at ({(3/4)*(-4)},0);
\coordinate (a) at ({(3/4)*-2.5},0);
\coordinate (b) at ({(3/4)*(-1)},0);
\coordinate (c) at ({(3/4)*0.5},0);
\coordinate (d) at ({(3/4)*2},0);
\coordinate (e) at ({(3/4)*3.5},0);
\coordinate (10) at ({(3/4)*5},0);

%The labels for -8 and 10 are typeset.
\node[anchor=north, inner sep=0] (label_for_-8) at ($(-8) +(0,-0.15)$){\makebox[0pt][r]{$-$}8};
\node[anchor=north, inner sep=0]  (label_for_10) at ($(10) +(0,-0.15)$){10};

%The labels for a, b, c, d, and e are typeset.
\draw[green,  name path=a_path_for_the_label_a_e] (label_for_-8.south) -- (label_for_10.south);
\path[name path=path_for_the_label_a] (a) -- ($(a) +(0,-0.5)$);
\path[name intersections={of=path_for_the_label_a and a_path_for_the_label_a_e, by=label_for_a}];
\node[anchor=south, inner sep=0] at (label_for_a){\textit{a}};
\path[name path=path_for_the_label_e] (e) -- ($(e) +(0,-0.5)$);
\path[name intersections={of=path_for_the_label_e and a_path_for_the_label_a_e, by=label_for_e}];
\node[anchor=south, inner sep=0] at (label_for_e){\textit{e}};


%The number line is drawn.
\draw[latex-latex] ({(3/4)*(-5)},0) -- ({(3/4)*6},0);


%Tick marks are drawn.
\draw ($(-8) +(0,2pt)$) -- ($(-8) +(0,-2pt)$);
\draw ($(a) +(0,2pt)$) -- ($(a) +(0,-2pt)$);
\draw ($(b) +(0,2pt)$) -- ($(b) +(0,-2pt)$);
\draw ($(c) +(0,2pt)$) -- ($(c) +(0,-2pt)$);
\draw ($(d) +(0,2pt)$) -- ($(d) +(0,-2pt)$);
\draw ($(e) +(0,2pt)$) -- ($(e) +(0,-2pt)$);
\draw ($(10) +(0,2pt)$) -- ($(10) +(0,-2pt)$);

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

该线不是水平的。

如果您添加和节点draw的选项,您将看到其中第一个具有一定的深度,即在 8 下面有一些空白。第二个中不存在这种情况。-810

一个简单的修复方法是在绘制绿线时使用锚点base而不是锚点,即south

\draw[green,name path=a_path_for_the_label_a_e] (label_for_-8.base) -- (label_for_10.base);

例如:

在此处输入图片描述

这里,锚点之间画了绿线south,锚点之间画了红线base,并且画出了节点的边框,因此您可以看到 8 下方的空白。


当然,你可以用更少的代码来实现这一点,而不必从每个坐标向下创建所有虚拟路径并计算交点。y 值为a且 x 值为 的坐标b由 给出(a -| b),因此你可以执行例如

\node [anchor=base] at (label_for_-8.base -| a) {\textit{a}};

放置一个节点,\textit{a}使得该节点的基线与节点的基线位于相同的 y 值-8

在此处输入图片描述

\documentclass{amsart}

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

\coordinate (-8) at ({(3/4)*(-4)},0);
\coordinate (a) at ({(3/4)*-2.5},0);
\coordinate (b) at ({(3/4)*(-1)},0);
\coordinate (c) at ({(3/4)*0.5},0);
\coordinate (d) at ({(3/4)*2},0);
\coordinate (e) at ({(3/4)*3.5},0);
\coordinate (10) at ({(3/4)*5},0);

%The labels for -8 and 10 are typeset.
\node[anchor=north, inner sep=0] (label_for_-8) at ($(-8) +(0,-0.15)$){\makebox[0pt][r]{$-$}8};
\node[anchor=north, inner sep=0]  (label_for_10) at ($(10) +(0,-0.15)$){10};

\draw [green,very thin] (label_for_-8.base) -- (label_for_10.base);

%The labels for a, b, c, d, and e are typeset.
\foreach \x in {a,...,e}
   \node [anchor=base] at (label_for_-8.base -| \x) {\textit{\x}};

%The number line is drawn.
\draw[latex-latex] ({(3/4)*(-5)},0) -- ({(3/4)*6},0);

%Tick marks are drawn.
\foreach \x in {-8,a,b,c,d,e,10}
  \draw ($(\x) +(0,2pt)$) -- ($(\x) +(0,-2pt)$);

\end{tikzpicture}
\end{document}

相关内容