1

I tried to combine two cells into one in a TikZ matrix:

\documentclass{standalone}

\usepackage{tikz}

\usetikzlibrary{matrix} \usetikzlibrary{fit}

\begin{document}

\begin{tikzpicture} \matrix (diagram) [matrix of nodes, nodes in empty cells, nodes={draw}] { & \ };

\node [draw, fit=(diagram-1-1)(diagram-1-2)] {$T$} ; \end{tikzpicture}

\end{document}

(Note: I've added the option nodes={draw} to display the underlying nodes for troubleshooting purposes only)

I get a combined node that is centred properly around the two underlying nodes, but the text $T$ is not centred in its node border for some reason:

T is not centred in the outer border

Why is this happening, and how do I fix it so that the text is centred?

Frink
  • 47
  • The matrix nodes have no content so their height is just twice the inner ysep. – cfr Oct 17 '23 at 03:34
  • What are you trying to achieve? The fit functionality is not made for nodes with text in it since it uses text height and text depth to set the size of the node. The given answers solve this in different ways but you're going to run into problems as soon as you want to put more complex text inside the nodes. A PGF/TikZ matrix isn't really made for multicol but solutions exist that try their best. – Qrrbrbirlbel Oct 17 '23 at 12:05

2 Answers2

2

There are two problems. The first is that the height of the matrix nodes is equal to only twice the inner ysep because they have no content, whereas the T node has height equal to twice the inner ysep plus the height of the T. There are various ways to address this. One simple approach is to measure the height of the T and set that as text height for the matrix nodes. (If you have other content in the matrix in your real document, minimum text height may be more appropriate.)

The second is that you need to anchor the T node relative to the nodes of the matrix. Basically, you want the T's .base anchor at .base of the combined node. In this case, that's just diagram.base. Again, if you have more nodes, you can create the fit node and then use <name of fit node>.base.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}
\newlength\myTht
\settoheight\myTht{T}

\begin{document}

\begin{tikzpicture} \matrix (diagram) [matrix of nodes, nodes in empty cells, nodes={draw}, text height=\myTht] {% or minimum text height if other nodes have deeper or taller material & \ }; \node [anchor=base] at (diagram.base) {$T$}; \node [draw, fit=(diagram-1-1)(diagram-1-2)] {} ; % \node (fitted) [draw, fit=(diagram-1-1)(diagram-1-2)] {} ; % \node [anchor=base] at (diagram-1-1.base -| fitted.center) {$T$}; \end{tikzpicture}

\end{document}

aligned 'T'

EDIT

You could also use

  \node (diagram-fit) [draw, fit=(diagram-1-1)(diagram-1-2)] {} ;
  \node  at (diagram-fit) {$T$};

This suggests that the default anchor for the fitted node is still center. Certainly, the T is centred without specifying any anchors. This should also work if your matrix includes more nodes, which you don't want influencing the placement of T.

However, when you try to place the $T$ into diagram-fit at the same time as you create/draw diagram-fit, things go pear-shaped. This isn't actually especially unusual. There are other cases where you effectively need to use two separate operations to get sensible alignment by default. For example \draw ... node ... will give you something different from \draw ... coordinate ... ; \node at () .... It's not that there's anything special about the fitted node, I don't think. I suspect it is that it is not a straightforward \node. (But this is just a suspicion - I haven't looked.)

cfr
  • 198,882
  • Thanks, this will work. The root of my problem seems to be that, by default, a fitted node's baseline is at the centre of the fitted node, even if you supply text to it. So the height of the text makes it so that it is not centred in the node. Another solution I've come across is to make the fitted node with no content, then make another node containing the text you want and placed at the centre of the fitted node (fitted.center). See https://tex.stackexchange.com/questions/424402/why-my-text-in-a-fit-node-is-not-vertically-centered – Frink Oct 18 '23 at 19:25
  • @Frink It depends what you want. Do you want it aligned to the baseline or to the centre? You don't see a difference here but if you had, say, x instead of T, would you want the x centred? Or would you want it on the baseline? – cfr Oct 18 '23 at 21:50
  • I wanted it aligned to the centre because that's what happens with a normal node but for some reason using a fitted node doesn't align it to the centre. – Frink Oct 19 '23 at 22:15
  • @Frink So \node [anchor=center] at (diagram.center) {$T$};? – cfr Oct 20 '23 at 00:29
  • @Frink Or you can just use the default anchors in that case. See edit. – cfr Oct 20 '23 at 00:39
2
  • You have a problem how fit insert his text in node: it is not vertical centered.
  • One possible solution is insert node text as node label in center of node:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,
                matrix}

\begin{document}

\begin{tikzpicture} \matrix (diagram) [matrix of nodes, nodes in empty cells, nodes={draw, ultra thin} ] { & \ };

\node[draw, fit=(diagram-1-1)(diagram-1-2), label=center:$T$] {}; % <--- \end{tikzpicture}

\end{document}

enter image description here

Zarko
  • 296,517