`TikZ` 图中节点对齐不正确

`TikZ` 图中节点对齐不正确

以下代码呈现了一条数轴图。我在上面标注了-8abc、和。任意两个连续数字之间的距离为。(我将图放大了。)从节点命令排版的、、、d、和的标签沿着它们下方的水平线完美对齐。为什么 不在同一条水平线上对齐?e1033/4abcde10-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.
\path[name path=a_path_for_the_label_a_b_c_d_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_b_c_d_e, by=label_for_a}];
\node[anchor=south, inner sep=0] at (label_for_a){\textit{a}};
\path[name path=path_for_the_label_b] (b) -- ($(b) +(0,-0.5)$);
\path[name intersections={of=path_for_the_label_b and a_path_for_the_label_a_b_c_d_e, by=label_for_b}];
\node[anchor=south, inner sep=0] at (label_for_b){\textit{b}};
\path[name path=path_for_the_label_c] (c) -- ($(c) +(0,-0.5)$);
\path[name intersections={of=path_for_the_label_c and a_path_for_the_label_a_b_c_d_e, by=label_for_c}];
\node[anchor=south, inner sep=0] at (label_for_c){\textit{c}};
\path[name path=path_for_the_label_d] (d) -- ($(d) +(0,-0.5)$);
\path[name intersections={of=path_for_the_label_d and a_path_for_the_label_a_b_c_d_e, by=label_for_d}];
\node[anchor=south, inner sep=0] at (label_for_d){\textit{d}};
\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_b_c_d_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

如果您的 MWE 背后只是一些练习而不是其他任何东西,那么您的 MWE 可以以以下更简单的形式重写:

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

\begin{document}
    \begin{tikzpicture}[xscale=1.42,
every node/.append style = {text height=1em, text depth=0.5ex, inner sep=0pt},
                        ]
% x-axis 
\draw[latex-latex] (-3.5,0) -- (3.5,0);
\foreach \x [count=\xx from -3] in {-8, a, b, c, d, e, 10}
{
\ifnum\xx=-3
    \draw (\xx,1mm) -- + (0,-2mm) node[below,xshift=-1ex] {$\x$};
\else
    \draw (\xx,1mm) -- + (0,-2mm) node[below] {$\x$};
\fi
}
    \end{tikzpicture}
\end{document}

上面的代码应该是不言自明的......

在此处输入图片描述

答案2

除非你有一些理由让事情变得复杂,而这些理由在你的例子中没有显示出来,否则像这样简单的事情有什么问题呢?我不相信你需要交叉点和你使用过的所有路径:

\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 are typeset.
\node[anchor=base] at ($(-8) +(0,-12pt)$){\makebox[0pt][r]{$-$}8};
\node[anchor=base] at ($(a)  +(0,-12pt)$){\textit{a}};
\node[anchor=base] at ($(b)  +(0,-12pt)$){\textit{b}};
\node[anchor=base] at ($(c)  +(0,-12pt)$){\textit{c}};
\node[anchor=base] at ($(d)  +(0,-12pt)$){\textit{d}};
\node[anchor=base] at ($(e)  +(0,-12pt)$){\textit{e}};
\node[anchor=base] at ($(10) +(0,-12pt)$){10};

% 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}

它产生了这个,完全对齐(注意anchor=base): 结果

而且,即使您有一个g标签(即延伸到基线以下的字符),它仍会保持对齐。

相关内容