3

(Note: I've edited this question quite a bit because it turned out my original problem was caused by two separate issues, so I'm separating them into two questions. The other question is Changing inner sep of tikz matrix without affecting the nodes inside it )

Consider the following MWE

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{matrix} % for "matrix of math nodes"

\begin{document}

\begin{equation} \left[ \begin{tikzpicture}[baseline=(current bounding box.center)] \matrix[matrix of math nodes] { a & b & c\ d & e & f \ }; \end{tikzpicture} \right] \end{equation}

\end{document}

It produces this output:

enter image description here

This isn't very satisfactory, because of the uneven spacing around the matrix.

Changing the matrix command to \matrix[matrix of math nodes, draw] { ... }; gives this result:

![enter image description here

So I think the issue is about the baseline not being where it needs to be in order for the brackets to line up correctly. But I don't know where the baseline should be, so I'm unsure what to do to fix the problem.

N. Virgo
  • 4,289
  • 2
  • 26
  • 41
  • @DevanoBethel because this is just an MWE. My actual code is a TikZ matrix containing tikz nodes with connections between them, so bmatrix wouldn't cut it. (Well, it would work if I drew each node as a separate tikz picture with remember picture active, and then drew the connections afterwards, but that sort of thing turned out to be slow.) – N. Virgo Aug 11 '22 at 03:05
  • Have you looked at the nicematrix package? It might be powerful enough to support what you are trying to achieve overall and also might have fixes for things like this already. – Andrew Stacey Aug 11 '22 at 07:31
  • 1
    @AndrewStacey thank you, I didn't know about that package and it looks generally extremely useful, regardless of whether I end up using it for this task. – N. Virgo Aug 11 '22 at 07:55

1 Answers1

3

You don't need to involve brackets and equations just yet; this is visible with just text. Creating a normal matrix as the desired outcome;

baseline $\begin{matrix} a & b\\ c & d\end{matrix}$ comparison

we can sort of tell that it uses the baseline as the average of each row's baseline. Nothing to do with the bounding box.

So, if we had odd number of rows, we could just refer to the base of the middle row;

baseline
\begin{tikzpicture}[baseline=(inner node.base)]
    \matrix (m) [matrix of math nodes] {
        a & b & c\\
        d & \node(inner node){e}; & f \\
        g & h & i \\
    };
\end{tikzpicture}
comparison

And, for a lack of finding a better option in the more general case i present:

\usetikzlibrary{calc}

...

baseline \begin{tikzpicture}[baseline=($(a.base)!0.5!(c.base)$)] \matrix (m) [matrix of math nodes,draw] { \node(a){a}; & b\ \node(c){c}; & d\ }; \end{tikzpicture} comparison

Generating the desired figure

\begin{equation}
    \left[
        \begin{tikzpicture}[baseline=($(a.base)!0.5!(c.base)$)]
            \matrix (m) [matrix of math nodes] {
                \node(a){a}; & b & c\\
                \node(c){d}; & e & f\\
            };
        \end{tikzpicture}
    \right]
\end{equation}

yields

matrix in brackets

which i think looks alright. Still some kerning issues if you decide to actually draw the border though.