# TikZ matrix delimiters are too big

I want to make a matrix with TikZ so I can put some fancy colored boxes around some elements, but I'm running into a problem: the parentheses around the matrix are too big and too far apart from the elements, at least compared to the regular \pmatrix. Here's an example:

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}

AMSMath:

$\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}$

TikZ:

$\begin{tikzpicture} \matrix[matrix of math nodes,left delimiter=(,right delimiter=)] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture}$

TikZ without delimiters + AMSMath:

$\begin{pmatrix} \begin{tikzpicture} \matrix[matrix of math nodes] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture} \end{pmatrix}$

\end{document}


If I don't use TikZ delimiters and put everything inside a \pmatrix, like in the third example, the parentheses look slightly different but they're still too big. The same thing happens with brackets. Is there a way around this?

• add draw,nodes=draw to your matrices and start adjusting inner and outer seps – percusse Sep 19 '16 at 22:57
• @percusse: the problem is that reducing inner sep shrinks not only the bounding box of the whole matrix (which is what I want) but also the bounding boxes of the individual numbers, making them be closer together (which is what I don't want). Can they be controlled separately? I'm looking at the TikZ-PGF manual and can't find anything about this. – Javier Sep 20 '16 at 2:08

The delimiters can be shifted through the styles every (left|right) delimiter:

\documentclass[a5paper]{article}% (a5paper for smaller image width)
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}

AMSMath:

$\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}$

TikZ:

$\begin{tikzpicture}[ every left delimiter/.style={xshift=.75em}, every right delimiter/.style={xshift=-.75em}, ] \matrix[ matrix of math nodes, left delimiter=(, right delimiter=), ] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture}$

\end{document}


• Also applying inner sep=1.5pt, column sep=6pt, row sep=2pt, options to \matrix makes it closed to the pmatrix. – Peter Grill Sep 20 '16 at 4:01
• Thank you! What ended up working best is a combination of this and @Ignasi's answer. I used an x/yshift of .4em and a column sep of 3.5pt and it looks almost identical to the default matrix. – Javier Sep 20 '16 at 14:47

A TikZ matrix is a node which contains other nodes, so the distance between nodes and brackets is twice the inner sep one due to internal nodes and the second is the inner sep between nodes and matrix border.

You can fix a matrix-node inner sep to 0pt and later on fix another inner sep for nodes (see How to fix a certain distance between a TikZ matrix border and its nodes?).

\documentclass[a5paper]{article}% (a5paper for smaller image width)
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}

AMSMath:

$\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}$

TikZ: default inner sep

$\begin{tikzpicture} \matrix[draw, %<- just used to show differences matrix of math nodes, left delimiter=(, right delimiter=), ] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture}$

TikZ: matrix inner sep = 0pt

$\begin{tikzpicture} \matrix[draw, %<- just used to show differences inner sep=0pt, %<- set inner sep for all nodes nodes={inner sep=.3333em}, %<- set another inner sep for inner nodes matrix of math nodes, left delimiter=(, right delimiter=), ] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture}$
\end{document}


perhaps you would like to reduce the inner sep a bit more, as below it gives

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}

AMSMath:

$\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}$

TikZ:

$\begin{tikzpicture} \matrix[matrix of math nodes,left delimiter=(,right delimiter=), outer sep = 0pt,inner sep=2pt] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture}$

TikZ without delimiters + AMSMath:

$\begin{pmatrix} \begin{tikzpicture} \matrix[matrix of math nodes] (m) { 1 & 2 & 3 \\ 4 & 5 & 6 \\ }; \end{tikzpicture} \end{pmatrix}$

\end{document}

• This isn't quite right, though. It just makes everything closer together instead of just the brackets. – Javier Sep 19 '16 at 23:53

While not convenient to code, a reasonable approximation to the AMSmath version can be obtained by doing these three things:

1. set inner sep of the tikzpicture to -.2pt or some other appropriate small value;

2. Apply one \strut to each row;

3. Make each column alignment & double (&&), with the desired column separation applied to one entry of each column (here done as &~~~& to each column of row 1)

Here is the MWE.

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}

AMSMath:

$\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix}$

TikZ:

$\begin{tikzpicture}[inner sep=-.2pt] \matrix[matrix of math nodes,left delimiter=(,right delimiter=)] (m) { \strut1 &~~~& 2 &~~~& 3 \\ \strut4 && 5 && 6 \\ }; \end{tikzpicture}$

\end{document}