2

I know I can manually vertical align text in nodes via the text height/text depth keys. I know also I can set the total height to be the same with the same two keys.

But what if I want both effects at the same time? See my examples:

\documentclass{scrartcl}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        \matrix[every node/.style={draw,rectangle,text width=4em}]{
            \node {arr}; & \node {KMUX}; & \node {rather long text}; \\
        };
    \end{tikzpicture}

    \begin{tikzpicture}
        \matrix[every node/.style={draw,rectangle,text width=4em, text height=1.5ex, text depth=6ex, text centered}]{
            \node {arr}; & \node {KMUX}; & \node {rather long text}; \\
        };
    \end{tikzpicture}
\end{document}

this produces: result

So I think it's obvious that the problem with the first one is that the boxes doesn't have the same height. Now when trying to fix that, I set the height/depth manually. But now the three nodes aren't vertically centered in their boxes (text centered apparently only configures horizontal centering).

Any ideas on this? (I know there are many questions out there on similar issues, but I can't find one tackling this issue)

3 Answers 3

5

With matrix library, lines automatically adopted to height cell:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
    \begin{tikzpicture}
\matrix [matrix of nodes,
         nodes={inner sep=2mm, anchor=center,
                text width=4em, align=center},
         draw, inner sep=0pt] (m)
{
arr &   KMUX    &   rather long text in four lines   \\
};
\draw   (m-1-1.east |- m.north) -- (m-1-1.east |- m.south)
        (m-1-2.east |- m.north) -- (m-1-2.east |- m.south);
    \end{tikzpicture}
\end{document}

enter image description here

Addendum: It is not clear what you after.

You can set nodes height with prescribed height (as is suggested in @hpekristiansen answer) which is rigid and require that needed maximum height is know in advance. This can be automated (on very complex way) by measuring height of all nodes and on the end use the largest among them.

One way may be not to draw nodes but write simple table, for example by use of tabularray package:

\documentclass{scrartcl}
\usepackage{tabularray}

\begin{document}
    \begin{table}[!ht]
\begin{tblr}{hlines, vlines,
             colspec = {*{3}{Q[c, m, wd=4em]}}
             }
arr &   KMUX    &   rather long text in four lines   \\
\end{tblr}
    \end{table}
\end{document}

which gives:

enter image description here

However, if you for some reason still like to have this this table as tikz node, you can insert it in node as shown below:

\documentclass{scrartcl}
\usepackage{tabularray}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
N/.style = {draw, inner sep=0pt}
                      ]
\node [N]   {\begin{tblr}{vlines,
             colspec = {*{3}{Q[c, m, wd=4em]}},}
            arr &   KMUX    &   rather long text in four lines \\
            \end{tblr}
            };
    \end{tikzpicture}
\end{document}

Result of compilation is the same as before.

3
  • Well this solves my issue with minimum height not being generic. Just being curious, is there another way for drawing the borders (setting draw, rectangle on the nodes results in borders around the individual text nodes (not sharing the same height)) since doing so manually takes extra code (or is using a foreach loop is as nice as it gets)?
    – atticus
    Mar 6, 2022 at 15:07
  • @atticus,i can't figured out what is your problem. Does showed result doesn't give what you after?
    – Zarko
    Mar 6, 2022 at 15:22
  • Indeed it does, I'm just wondering if this manual drawing the cell borders can be automated, but I guess using a foreach for each row/column is as nice as it gets.
    – atticus
    Mar 6, 2022 at 15:27
3

This could be an alternative to similar constructions with tcolorbox

\documentclass{scrartcl}
\usepackage[most]{tcolorbox}

\begin{document}

\begin{tcbitemize}[raster columns=3, raster equal height, raster width center=9cm, raster column skip=-.5mm, colback=white, sharp corners, halign=center, valign=center]
\tcbitem arr 
\tcbitem KMUX    
\tcbitem long text in four lines 
\end{tcbitemize}

\begin{tcbitemize}[raster columns=4, raster equal height, raster width center=9cm, raster column skip=-.5mm, colback=white, sharp corners, halign=center, valign=center]
\tcbitem arr 
\tcbitem KMUX    
\tcbitem long text in four lines 
\tcbitem like previous but with another column
\end{tcbitemize}
\end{document}

enter image description here

2
\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\matrix[every node/.style={draw, rectangle, text width=4em, minimum height=10ex, text centered}]{
\node {arr}; & \node {KMUX}; & \node {rather long text}; \\
};
\end{tikzpicture}
\end{document}

Three boxes with centered text

5
  • Well didn't thought about this. Any ideas on how to make this generic? (so that if I insert a new node which uses even more vertical space, the other nodes get scaled up vertically as well)
    – atticus
    Mar 6, 2022 at 14:07
  • 1
    I think you would need to store your node texts in a structure and loop through them to find the one with maximum height and then typeset the nodes afterwards with this as the minimum height. There would be no clean simple solution for such a thing. Mar 6, 2022 at 14:20
  • 1
    Note that you can add column sep=-\pgflinewidth to avoid those double lines between the cells.
    – SebGlav
    Mar 6, 2022 at 14:30
  • Hm, that sounds like pretty much overhead (of course it is logical that it has to be done that way but nevertheless the need of trial and error for finding the right maximum height for each new,larger node isn't that fortunate). After all this isn't that different of a simple/normal tex table/tabular.
    – atticus
    Mar 6, 2022 at 14:38
  • Maybe then in this case the nicematrix package (with the nicearray environments) is an alternative, but having a nicearray environment and a tikzpicture afterwards to "annotate" the table isn't that nice either.
    – atticus
    Mar 6, 2022 at 14:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .