5

This is related to Baseline of tikz picture within brackets but is a separate issue.

Consider the following MWE:

\documentclass{article}

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

\begin{document}

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

\end{document}

It produces this output:

enter image description here

I would like to get rid of the space between the nodes inside the matrix and the bounding box of the matrix. Setting the matrix's inner sep to 0 achieves this --- but it also changes the inner sep of all the nodes inside the matrix, which isn't what I want:

\documentclass{article}

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

\begin{document}

\begin{equation}
    \begin{tikzpicture}[baseline=(current bounding box.center)]
        \matrix[matrix of math nodes, draw, nodes={draw}, inner sep=0pt] {
            a & b & c \\
            d & e & f \\
        };
    \end{tikzpicture}
\end{equation}

\end{document}

enter image description here

How can I change the inner sep of the matrix node while leaving the nodes inside it alone?

I found a near-duplicate question, How to fix a certain distance between a TikZ matrix border and its nodes? . However, the answer to that question just sets the inner sep of the nodes explicitly, which I want to avoid - I want to just leave each node's inner sep at whatever value it would have if it wasn't in the matrix.

2
  • Keep everything the same but change nodes={draw} to nodes={draw, inner sep=10pt}. Aug 10 at 7:22
  • @M.AlJumaily sure, but then I have to know that the intended inner sep for the inner nodes was 10pt. Since this is part something I ultimately hope to share for others to use, I don't know that. So I want to leave the inner nodes as they are, not set their inner sep explicitly.
    – N. Virgo
    Aug 10 at 7:49

3 Answers 3

5

With the style /tikz/every outer matrix you can change the style of only the matrix node instead of the matrix's nodes.

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix} % for "matrix of math nodes"
\tikzset{tight matrix/.style={every outer matrix/.append style={inner sep=+0pt}}}
\begin{document}
\begin{equation}
    \begin{tikzpicture}[baseline=(current bounding box.center)]
        \matrix[matrix of math nodes, draw, nodes={draw}, tight matrix] {
            a & b & c \\
            d & e & f \\
        };
    \end{tikzpicture}
\end{equation}
\end{document}

Output

enter image description here

3
  • Great answer +1! I haven't encountered every outer matrix before Aug 10 at 9:37
  • 1
    feel rather weird how there isn'tSWH give a key to set the property directly and instead we need to go through a loop. (I'm going to guess that this solution will also apply to any nested matrix inside, although it doesn't sound like a good idea to nest matrices anyway)
    – user202729
    Aug 10 at 10:31
  • @user202729 Well, we could define our own \matrix command that does differentiate without us having to use every outer matrix directly. Aug 12 at 19:40
1

To my knowledge there is no inner sep parameter for the matrix node. So, when certain inner sep is fixed it decides the inner separation of nodes, but also the separation between nodes and matrix border. To avoid this behaviour, you should use M. Al Jumaily's solution: nodes={inner sep=...}.

If you don't liker it, a possible alternative could be to not draw the matrix border and draw it with a fit node. In this case, you keep original nodes inner sep (initially .3333em) and can draw the border to whatever separation you decide.

Following code shows this solution. The matrix border is not draw and three different borders are added with fit nodes.

I think it's not a good solution because you have to distinguish between real matrix node and fitting node and you have to know which inner nodes are larger to include them inside fit list.

\documentclass{article}

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

\begin{document}

\begin{equation}
    \begin{tikzpicture}[baseline=(current bounding box.center)]
        \matrix[matrix of math nodes, nodes={draw}] (A) {
            a & b & c \\
            d & e & f \\
        };
     \node[fit={(A-1-1) (A-1-2) (A-2-3)}, inner sep=0pt, draw=green, inner sep=2pt] (B) {};
     \node[fit={(A-1-1) (A-1-2) (A-2-3)}, inner sep=0pt, draw=blue, inner sep=0pt] (C) {};
     \node[fit={(A-1-1) (A-1-2) (A-2-3)}, inner sep=0pt, draw=red, inner sep=5mm] (D) {};
    \end{tikzpicture}
\end{equation}

\end{document}

enter image description here

1

This answer is based on Fritz's answer as credit should always be given.

Output

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{custom nodes/.style={
    draw=red,
    matrix of math nodes,
    cells/.expanded={
        nodes={draw=blue},
        inner xsep=\pgfkeysvalueof{/pgf/inner xsep},
        inner ysep=\pgfkeysvalueof{/pgf/inner ysep}
    }
}}
\begin{document}
    \begin{equation}
        \begin{tikzpicture}[ baseline=(current bounding box.center)]
        \matrix[custom nodes, inner sep=0pt] {
            a & b & c \\
            d & e & f \\
            };
        \end{tikzpicture}
    \end{equation}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.