Tikz:如何对齐简单节点的文本?

Tikz:如何对齐简单节点的文本?

我尝试制作一个漂亮的小图形,作为字符串匹配的示例。我的问题是“b”与 a 和 c 不对齐,我找不到处理这个问题的方法:

\begin{figure}[h]

            \begin{tikzpicture}[align=bottom,shorten >=1pt,node distance=1cm,on grid,auto]
              \node(q_0) {$c$};
              \node(q_1) [right of=q_0] {$b$};
              \node(q_2) [right of=q_1] {$a$};
              \node(q_3) [right of=q_2] {$c$};
              \node(q_4) [right of=q_3] {$b$};
              \node(q_5) [right of=q_4] {$a$};
              \node(q_6) [right of=q_5] {$b$};
              \node(q_7) [right of=q_6] {$a$};

              \node(t_0) [below of=q_0]{$a$};
              \node(t_1) [right of=t_0] {$c$};
              \node(t_2) [right of=t_1] {$b$};
              \node(t_3) [right of=t_2] {$a$};
              \node(t_4) [right of=t_3] {$a$};
              \node(t_5) [right of=t_4] {$c$};
              \node(t_6) [right of=t_5] {$b$};
              \node(t_7) [right of=t_6] {$a$};
              \node(t_8) [right of=t_7] {$c$};
              \node(t_9) [right of=t_8] {$b$};
              \node(t_10) [right of=t_9] {$a$};
              \node(t_11) [right of=t_10] {$b$};
              \node(t_12) [right of=t_11] {$c$};

              \path[->] (q_0) edge node {$\neq$} (t_0);
              \end{tikzpicture}
    \end{figure}

答案1

您可以使用

every node/.style={font=\strut}

在此处输入图片描述

代码

\documentclass[tikz,margin=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[
    shorten >=1pt,
    every node/.style={font=\strut},
    node distance=1cm,
    auto
  ]
  \node(q_0) {$c$};
  \node(q_1) [right of=q_0] {$b$};
  \node(q_2) [right of=q_1] {$a$};
  \node(q_3) [right of=q_2] {$c$};
  \node(q_4) [right of=q_3] {$b$};
  \node(q_5) [right of=q_4] {$a$};
  \node(q_6) [right of=q_5] {$b$};
  \node(q_7) [right of=q_6] {$a$};

  \node(t_0) [below of=q_0]{$a$};
  \node(t_1) [right of=t_0] {$c$};
  \node(t_2) [right of=t_1] {$b$};
  \node(t_3) [right of=t_2] {$a$};
  \node(t_4) [right of=t_3] {$a$};
  \node(t_5) [right of=t_4] {$c$};
  \node(t_6) [right of=t_5] {$b$};
  \node(t_7) [right of=t_6] {$a$};
  \node(t_8) [right of=t_7] {$c$};
  \node(t_9) [right of=t_8] {$b$};
  \node(t_10) [right of=t_9] {$a$};
  \node(t_11) [right of=t_10] {$b$};
  \node(t_12) [right of=t_11] {$c$};

  \path[->] (q_0) edge node {$\neq$} (t_0);
\end{tikzpicture}
\end{document}

但我会用矩阵来定位节点

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,auto]
  \matrix[
      matrix of math nodes,
      row sep={1cm,between origins},
      column sep={1cm,between origins}
    ](m){%
      c&b&a&c&b&a&b&a\\
      a&c&b&a&a&c&b&a&c&b&a&b&c\\
   };
  \path[->](m-1-1)edge node{$\neq$}(m-2-1);
\end{tikzpicture}
\end{document}

答案2

我认为节点矩阵是一种更优雅的解决方案,但不太优雅的替代方法是base right=ofpositioning库而不是中使用on grid

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
  \begin{tikzpicture}[shorten >=1pt,node distance=1cm]
      \node(q_0)  {$c$};
      \node(q_1) [base right=of q_0] {$b$};
      \node(q_2) [base right=of q_1] {$a$};
      \node(q_3) [base right=of q_2] {$c$};
      \node(q_4) [base right=of q_3] {$b$};
      \node(q_5) [base right=of q_4] {$a$};
      \node(q_6) [base right=of q_5] {$b$};
      \node(q_7) [base right=of q_6] {$a$};

      \node(t_0) [below of=q_0]{$a$};
      \node(t_1) [base right=of t_0] {$c$};
      \node(t_2) [base right=of t_1] {$b$};
      \node(t_3) [base right=of t_2] {$a$};
      \node(t_4) [base right=of t_3] {$a$};
      \node(t_5) [base right=of t_4] {$c$};
      \node(t_6) [base right=of t_5] {$b$};
      \node(t_7) [base right=of t_6] {$a$};
      \node(t_8) [base right=of t_7] {$c$};
      \node(t_9) [base right=of t_8] {$b$};
      \node(t_10) [base right=of t_9] {$a$};
      \node(t_11) [base right=of t_10] {$b$};
      \node(t_12) [base right=of t_11] {$c$};

    \path[->] (q_0) edge node {$\neq$} (t_0);
  \end{tikzpicture}
\end{document}

对齐节点

从视觉上看,它们似乎没有对齐,但是当我在查看器中将它们围起来时,它们似乎对齐了!

相关内容