如何强制所有标签在 Tikz 中保持在同一水平线上

如何强制所有标签在 Tikz 中保持在同一水平线上

注意相对节点高度

我试图让所有标签与线保持相同的距离。我尝试过分离节点,但即使我删除除“1”和“N+1”之外的所有节点,三个字符的标签仍然更高。我还尝试过:

  • x 尺度
  • 标签距离
  • 标签大小 = \tiny
  • 内分隔符 = 0pt
  • 外间隔 = 0pt

但似乎没有什么可以让三个字符标签相对于行占据相同的相对位置。有什么建议吗?我附上了快速复制的完整代码以及一张图片。

\documentclass[12pt, twoside]{article}

\usepackage{tikz}
\tikzset{every node/.style={draw,shape=circle}}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \tikzstyle{every node}=[draw,shape=circle]
        \draw[very thick](0,0) node[circle,fill,inner sep=1pt,label=above:$1$](1){} -- (1,0);
        \draw[very thick](1,0) node[circle,fill,inner sep=1pt,label=above:$2$](2){} -- (2,0);
        \draw[very thick](2,0) -- (4,0);
        \draw[very thick](4,0) node[circle,fill,inner sep=1pt,label=above:$i-1$](i-1){} -- (5,0);
        \draw[very thick](5,0) node[circle,fill,inner sep=1pt,label=above:$i$](i){} -- (6,0);
        \draw[very thick](6,0) node[circle,fill,inner sep=1pt,label=above:$i+1$](i+1){} -- (7,0);
        \draw[very thick](7,0) -- (9,0);
        \draw[dotted, very thick](9,0) node[circle,fill,inner sep=1pt,label=above:$N$](N){} -- (10,0)
         node[circle,fill,inner sep=1pt,label=above:$N+1$](N+1){};
    \end{tikzpicture}
    \caption{Visualization of meshing elements and intervals including the complex $N+1$ node}
    \label{fig:nodeline}
\end{figure}

\end{document}

答案1

样式every node也定义标签的节点样式。当标签较宽时,圆圈会更大,因此标签实际上看起来比线更高。

编辑:作为伊格纳西他在下面的评论中指出,还需要考虑标签的不同文本深度。以下示例对此进行了补偿:

every node/.append style = {text depth=0.2ex}

看看以下解决方案是否能满足您的要求:

\documentclass[12pt, twoside]{article}
\usepackage{tikz}

\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}[
dot/.style = {circle,fill=black,inner sep=1pt, node contents={}}
every node/.append style = {text depth=0.2ex}
                        ]
\draw[very thick] (0,0) node (1)   [dot,label=$1$]  -- (1,0);
\draw[very thick] (1,0) node (2)   [dot,label=$2$]  -- (2,0);
\draw[very thick] (2,0) -- (4,0);
\draw[very thick] (4,0) node (i-1) [dot,label=$i-1$] -- (5,0);
\draw[very thick] (5,0) node (i)   [dot,label=$i$]   -- (6,0);
\draw[very thick] (6,0) node (i+1) [dot,label=$i+1$] -- (7,0);
\draw[very thick] (7,0) -- (9,0);
\draw[dotted,very thick] (9,0)  node (N)   [dot,label=$N$] -- 
                         (10,0) node (N+1) [dot,label=$N+1$];
    \end{tikzpicture}
    \caption{Visualization of meshing elements and intervals including the complex $N+1$ node}
    \label{fig:nodeline}
\end{figure}
\end{document}

上述代码可以进一步简化,例如使用循环:

\documentclass[12pt, twoside]{article}
\usepackage{tikz}

\begin{document}

\begin{figure}
    \centering
    \begin{tikzpicture}[
dot/.style = {circle,fill=black,inner sep=1pt}
every node/.append style = {text depth=0.2ex}
                        ]
\draw[very thick]           (0,0) -- (9,0);
\draw[dotted,very thick]    (9,0) -- (10,0);
    \foreach \i/\j in {0/1, 1/2, 4/i-1, 5/i, 6/i+1, 9/N, 10/N+1}
\node (\j) [dot,label=$\j$] at (\i,0) {}; 
    \end{tikzpicture}
\caption{Visualization of meshing elements and intervals including the complex $N+1$ node}
    \label{fig:nodeline}
\end{figure}

\end{document}

两种情况下的结果都是一样的。

在此处输入图片描述

相关内容