[Tex/LaTex] TikZ matrix delimiters are too big

delimiterstikz-matrixtikz-pgf

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?

enter image description here

Best Answer

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}

Result