# tikz tow rows of fixed size text rectangles

I am learning tikz and I wish to use it to draw two rows of fixed size boxes each with a printable character in them to display data. I have tried to do this in the attached example, but I cannot figure out how to get the text boxes to be the same size.

Can someone point out how I can get the text boxes to all render the same size? Also how can I get the label strings to be at the correct vertical height to match the rows.

\documentclass{report}
\usepackage{tikz}

\begin{document}
text before
\begin{figure}
\begin{tikzpicture}

\tikzset{red/.style={rectangle, draw,fill=red, font=\ttfamily, minimum width =10pt, minimum height=10pt}}
\tikzset{grn/.style={rectangle, draw, fill=green, font=\ttfamily, minimum width =10pt, minimum height=10pt}}
\tikzset{blk/.style={font=\ttfamily, minimum width =10pt, minimum height=10pt}}

% Row 1 AbCDE
\node[blk] (L1) {Row 1 Label};
\node[red] (r1)  at ([xshift=1cm] L1.east) {A};
\node[grn, anchor=west] (r2) at (r1.east){b};
\node[red, anchor=west] (r3) at (r2.east){c};
\node[grn, anchor=west] (r4) at (r3.east){D};
\node[red, anchor=west] (r5) at (r4.east){E};
% Row 2 aB.De
\node[blk]  at (L1.south) {Row 2 Label};
\node[red, anchor=north]  at (r1.south){a};
\node[grn, anchor=north]  at (r2.south){B};
\node[red, anchor=north]  at (r3.south){.};
\node[grn, anchor=north]  at (r4.south){D};
\node[red, anchor=north]  at (r5.south){e};
\end{tikzpicture}
\end{figure}
text after
\end{document}

• You are almost there, it is just that 10pt is too small so easily overridden with bigger characters. Changing to 20 pt will reveal what I mean – percusse Sep 30 '15 at 11:52
• Thanks. What about the Row labels? Should changing to 20 pt fix those? I am not seeing that. – Joahua Sep 30 '15 at 11:56
• You're missing anchor=north in the definition of Row 2 Label. By default, TikZ is placing the centre of the box containing 'Row 2 Label' at the south of Row 1 label. – David R Sep 30 '15 at 12:25
• @percusse gladly. My internal idea of what constitutes "best practice remark" is mostly cobbled together from reading TeX.SE, so it might not be something that everyone agrees on. Suggestions from everyone else are welcome :) – David R Nov 20 '17 at 1:11
• Oops: my comment above should read "everyone", not "everyone else". – David R Nov 20 '17 at 18:52

As percusse commented, to get the rectangles to have the same size, impose a minimum height as well as a minimum width, and set both to be at least as large as the highest/widest block. If you want these two values to be the same (so your rectangles are squares), you can use the minimum size key to set both at once. To make your second row's label line up correctly use anchor=north, as in the the second row of coloured blocks.

In the spirit of best encouraging practice (at least, as far as I understand it: please correct me if I'm mistaken) I made a few other changes too.

• Changed the red and green styles to inherit from blk
• Renamed them to rblk and gblk to avoid clashing with the built-in colours red and green.
• Used the optional argument to tikzpicture to set styles locally, rather than globally.

Here's the result of these changes.

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
blk/.style = {rectangle, font=\ttfamily, minimum size=20pt},
rblk/.style = {blk, draw, fill=red},
gblk/.style = {blk, draw, fill=green}
]

% Row 1 AbCDE
\node[blk] (L1) {Row 1 Label};
\node[rblk] (r1)  at ([xshift=1cm] L1.east) {A};
\node[gblk, anchor=west] (r2) at (r1.east){b};
\node[rblk, anchor=west] (r3) at (r2.east){c};
\node[gblk, anchor=west] (r4) at (r3.east){D};
\node[rblk, anchor=west] (r5) at (r4.east){E};
% Row 2 aB.De
\node[blk, anchor=north]  at (L1.south) {Row 2 Label};
\node[rblk, anchor=north]  at (r1.south){a};
\node[gblk, anchor=north]  at (r2.south){B};
\node[rblk, anchor=north]  at (r3.south){.};
\node[gblk, anchor=north]  at (r4.south){D};
\node[rblk, anchor=north]  at (r5.south){e};
\end{tikzpicture}
\end{document}


There is a small problem with the above: notice that each character is vertically centred within the coloured blocks. (Compare for instance . to D.)

We can fix this by forcing each block to use the same amount of height and depth for each character, rather than a "shrink-to-fit" approach. The relevant keys are text height and text depth.

If I change the style of blk to be

blk/.style = {rectangle, font=\ttfamily, text height=1.5ex, text depth=.25ex, minimum size=3.5ex}


I get the second diagram below in which the baselines coincide. The values I used for the height/depth keys below are from the tutorial Diagrams as Simple Graphs in the TikZ manual: section 5 at the time of writing. In particular, the units of height/depth are ex, which—roughly speaking—scales with the height of the font being used