一行中定位 tikz 节点

一行中定位 tikz 节点

我怎样才能将文本放置在时间线上方相同的高度,即让它们显示在同一行?我认为“p”比“E”高。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1]
    \draw[->,>=] (-1,0) -- (9,0);
    \draw[shift={(0,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$1$};
    \draw[shift={(0,0.8)}] node {p};
    \draw[shift={(4,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$2$};
    \draw[shift={(4,0.8)}] node {E};
    \draw[shift={(8,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$3$};
    \draw[shift={(8,0.8)}] node {J};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

应用于anchor=base将使tikzpicture三项内容与基线保持一致:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1, anchor=base]
    \draw[->,>=] (-1,0) -- (9,0);
    \draw[shift={(0,0)}] (0pt,2pt) -- (0pt,-2pt) 
        node[below] {$1$};
    \draw[shift={(0,0.8)}] node {p};
    \draw[shift={(4,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$2$};
    \draw[shift={(4,0.8)}] node {E};
    \draw[shift={(8,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$3$};
    \draw[shift={(8,0.8)}] node {J};
\end{tikzpicture}
\end{document}

答案2

在此处输入图片描述

\documentclass[tikz]{standalone}

\begin{document}
    \begin{tikzpicture}
        \draw[line width=1pt] (0,0) -- (10,0);
        \foreach \x/\y/\z in {1/1/p,5/5/E,9/9/J,}{
            \draw[line width=1pt] (\x,-2mm)
            node[below](\x){\strut\y} -- 
            (\x,2mm)node[above=0.5cm]{\z};
        }
    \end{tikzpicture}
\end{document}

基线检查似乎没问题

\documentclass[tikz]{standalone}

\begin{document}
    \begin{tikzpicture}
        \draw[line width=1pt] (0,0) -- (10,0);
        \foreach \x/\y/\z in {1/1/p,1.2/1.2/E,1.4/1.4/J,}{
            \draw[line width=1pt] (\x,-2mm)
            node[below](\x){\strut\y} -- 
            (\x,2mm)node[above=0.5cm]{\z};
        }
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容