节点在 tikzfigure 中略微移位

节点在 tikzfigure 中略微移位

我正在尝试使用以下命令对一条线进行简单的细分:

    \draw (-6,0) -- (6,0) ; %edit here for the axis
    \foreach \x in  {-6,-4.5,-3,0,3,4.5,6} % edit here for the vertical lines
    \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
    \draw[shift={(-6,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_0 = a$};
    \draw[shift={(-4.5,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_1$};
    \draw[shift={(-3,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_2$};
    \draw[shift={(-1.5,0)},color=black] node[below] {$\cdots$};
    \draw[shift={(0,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_i$};
    \draw[shift={(1.5,0)},color=black] node[below] {$\cdots$};
    \draw[shift={(3,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_{N-2}$};
    \draw[shift={(4.5,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_{N-1}$};
    \draw[shift={(6,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_{N} = b$};
    \end{tikzpicture}

但最右侧的节点看起来比其他节点低,这很烦人。我该如何修复它?

答案1

这是由于最后一个节点标签中 b 的高度较大造成的,因此也许最简单的修复方法是\smash

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (-6,0) -- (6,0) ; %edit here for the axis
\foreach \x in  {-6,-4.5,-3,0,3,4.5,6} % edit here for the vertical lines
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
\draw[shift={(-6,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_0 = a$};
\draw[shift={(-4.5,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_1$};
\draw[shift={(-3,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_2$};
\draw[shift={(-1.5,0)},color=black] node[below] {$\cdots$};
\draw[shift={(0,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_i$};
\draw[shift={(1.5,0)},color=black] node[below] {$\cdots$};
\draw[shift={(3,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_{N-2}$};
\draw[shift={(4.5,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_{N-1}$};
\draw[shift={(6,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$x_{N} = \smash{b}$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

https://latexref.xyz/_005csmash.html#g_t_005csmash欲了解有关 ing 的更多信息\smash

答案2

@chsk 对(+1)优秀答案的编辑使其代码明显缩短

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw (-6,0) -- (6,0) ; %edit here for the axis
        \foreach \x/\y in  {-6/{$x_0 = a$},-4.5/{$x_1$},-3/{$x_2$},-1.5/{$\cdots$},0/{$x_i$},1.5/{$\cdots$},3/{$x_{N-2}$},4.5/{$x_{N-1}$},6/{$x_{N} =\smash{b}$}} % edit here for the vertical lines
        \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt)node[below] {\y};
    \end{tikzpicture}
\end{document}

相关内容