1

I want to place a two lines text inside each node in a grid and want them to be centered. Now I am aware of this among other threads. E.g. the most natural solution would be to use align=center in combination with linebreaks \\ inside the node text brackets.

Picture and MWE below. As you can see, the letters are not centered, while the numbering is. I tried:

  • align + \\
  • text width, alone and in combination with the above
  • tabular environment inside the node text brackets
  • removing the \scriptsize parameter I use As you can see, I use nested for loops to create a grid that counts up in a serpentine formation. Certain groups of indices (predefined externally) receiver certain letters, I achieve this with the defined command \mylabel. Got the idea for this from my question earlier. However, I just cant get the letters to align centered with the numbers in the line below.

enter image description here

\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}

Marijn
  • 37,699
Felix
  • 49

1 Answers1

4

The align is good, but there are a couple of unwanted spaces that you can't see. Try to enclose the \mylabel command in a \fbox and you'll see these spaces:

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

You can do the following to prevent the not needed spaces:

\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}

enter image description here

Edit: for a better compiling time if the groups are not needed in other part of the picture, it would be faster to change the macro in this way (and to remove the groups definitions, not needed now):

\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
}

Note: Perhaps I should have pointed all this in my previous answer.

Juan Castaño
  • 28,426
  • Sir, you are a saint! You have no idea just how happy you made a man! Thank you so much, I did not know about these invisible spaces! – Felix Jul 30 '21 at 10:18
  • @Felix thanks, but I'm editing my previous answer. My example had on 16 numbers and I was not aware of the compiling time. I think it can be done faster. – Juan Castaño Jul 30 '21 at 10:23
  • It indeed is very slow, but I will just precompile it and include the pdf in the main document or something like that. – Felix Jul 30 '21 at 10:26