带有换行符的 Tikz 节点中的居中文本无法正常工作

带有换行符的 Tikz 节点中的居中文本无法正常工作

我想在网格中的每个节点内放置两行文本,并希望它们居中。现在我知道在其他线程中。例如,最自然的解决方案是与节点文本括号内的align=center换行符结合使用。\\

下面是图片和 MWE。如您所见,字母未居中,而数字居中。我试过:

  • align+\\
  • text width,单独或与上述组合
  • tabular节点文本括号内的环境
  • 删除\scriptsize我使用的参数正如您所见,我使用嵌套的 for 循环创建一个以蛇形形式向上计数的网格。某些索引组(外部预定义)接收某些字母,我使用定义的命令实现这一点\mylabel。这个想法来自我的问题早些时候。但是,我就是无法让字母与下面一行中的数字居中对齐。

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}

%=================================
\def\groupC{{2,11,13,22,24,33,35,44,46,55,57,66,68,77,79,88}}
\def\groupA{{3,4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,25,26,27,28,29,30,31,32,36,37,38,39,40,41,42,43,47,48,49,50,51,52,53,54,58,59,60,61,62,63,64,65,69,70,71,72,73,74,75,76,80,81,82,83,84,85,86,87}}
\def\groupH{{12,23,34,45,56,67,78}}
\def\groupO{{89}}

\newcommand{\mylabel}[1]
{%
  \pgfmathtruncatemacro{\num}{#1}
  \foreach\c in {0,...,15}
  {%
    \pgfmathtruncatemacro{\temp}{\groupC[\c]}%
    \ifnum \num = \temp \scriptsize C\fi
  }
  \foreach\a in {0,...,63}
  {%
    \pgfmathtruncatemacro{\temp}{\groupA[\a]}%
    \ifnum \num = \temp \scriptsize A\fi
  }  
  \foreach\h in {0,...,6}
  {%
    \pgfmathtruncatemacro{\temp}{\groupH[\h]}%
    \ifnum \num = \temp \scriptsize H\fi
  }
  \ifnum \num = 89 \scriptsize O\fi 
}

\begin{document}
\begin{tikzpicture}[node distance=1cm]

\foreach \i in {0,...,7}
  \foreach \j in {0,...,10}
    {\pgfmathtruncatemacro{\label}{11*\i+\j+2}
    {\pgfmathsetmacro{\x}{1*\j*(-1)^(\i) + 5*(-1)^(\i+1)}
    \node (e\label) [rectangle, draw=black, minimum width=1cm, align=center] at (\x,-1*\i) {
    \mylabel{\label}\\[-5pt]
    \scriptsize(\label)
    };
    }}

\end{tikzpicture}
\end{document}

答案1

对齐很好,但有几个你看不到的不需要的空格。尝试将命令括\mylabel在 a 中\fbox,你会看到这些空格:

\fbox{\mylabel{\label}}\\[-5pt]

您可以执行以下操作来避免出现不需要的空格:

\documentclass{standalone}
\usepackage{tikz}

%=================================
\def\groupC{{2,11,13,22,24,33,35,44,46,55,57,66,68,77,79,88}}
\def\groupA{{3,4,5,6,7,8,9,10,14,15,16,17,18,19,20,21,25,26,27,28,29,30,31,32,36,37,38,39,40,41,42,43,47,48,49,50,51,52,53,54,58,59,60,61,62,63,64,65,69,70,71,72,73,74,75,76,80,81,82,83,84,85,86,87}}
\def\groupH{{12,23,34,45,56,67,78}}
\def\groupO{{89}}

\newcommand{\mylabel}[1]
{%
  \pgfmathtruncatemacro{\num}{#1}% <-- to prevent unwanted spaces
  \scriptsize% you can put this here instead of inside each loop (unrelated)
  \foreach\c in {0,...,15}
  {%
    \pgfmathtruncatemacro{\temp}{\groupC[\c]}%
    \ifnum \num = \temp C\fi
  }% <-- to prevent unwanted spaces
  \foreach\a in {0,...,63}
  {%
    \pgfmathtruncatemacro{\temp}{\groupA[\a]}%
    \ifnum \num = \temp A\fi
  }% <-- to prevent unwanted spaces
  \foreach\h in {0,...,6}
  {%
    \pgfmathtruncatemacro{\temp}{\groupH[\h]}%
    \ifnum \num = \temp H\fi
  }% <-- to prevent unwanted spaces
  \ifnum \num = 89  O\fi
}

\begin{document}
\begin{tikzpicture}[node distance=1cm]

\foreach \i in {0,...,7}
  \foreach \j in {0,...,10}
    {\pgfmathtruncatemacro{\label}{11*\i+\j+2}
    {\pgfmathsetmacro{\x}{1*\j*(-1)^(\i) + 5*(-1)^(\i+1)}
    \node (e\label) [rectangle, draw=black, minimum width=1cm, align=center] at (\x,-1*\i) {
    \mylabel{\label}\\[-5pt]
    \scriptsize(\label)
    };
    }}

\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:为了缩短编译时间,如果图片的其他部分不需要这些组,则可以通过这种方式更改宏(并删除现在不需要的组定义)会更快:

\newcommand{\mylabel}[1]
{%
  \pgfmathtruncatemacro{\num}{#1}% <-- to prevent unwanted spaces
  \scriptsize% you can put this here instead of inside each loop
  \foreach\c in { 2,11,13,22,24,33,35,44,46,55,57,66,68,77,79,88} % GROUP C
    {\ifnum \num = \c C\fi}% <-- to prevent unwanted spaces
  \foreach\a in { 3, 4, 5, 6, 7, 8, 9,10,14,15,16,17,18,19,20,21, % GROUP A
                 25,26,27,28,29,30,31,32,36,37,38,39,40,41,42,43,
                 47,48,49,50,51,52,53,54,58,59,60,61,62,63,64,65,
                 69,70,71,72,73,74,75,76,80,81,82,83,84,85,86,87}
    {\ifnum \num = \a A\fi}% <-- to prevent unwanted spaces
  \foreach\h in {12,23,34,45,56,67,78}                            % GROUP H
    {\ifnum \num = \h H\fi}% <-- to prevent unwanted spaces
  \ifnum \num = 89  O\fi                                          % GROUP O
}

笔记:也许我应该在之前的回答中指出这一点。

相关内容