[Tex/LaTex] Brackets around matrices in TikZ

bracesmatricestikz-pgf

I'm writing a custom package for labeled matrices using TikZ. I noticed an inconsistency in TikZ. This is best illustrated with the following MWE:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[baseline=(m.west)]
\matrix (m) [draw=white!80!black, 
         left delimiter=[,right delimiter={]}, matrix of math nodes] 
         { 1 & 1 \\ };
\end{tikzpicture} 
\begin{tikzpicture}[baseline=(m.west)]
\matrix (m) [draw=white!80!black,
         left delimiter=[,right delimiter={]}, matrix of math nodes] 
         { 1 & 1 \\ 2 & 2 \\ };
\end{tikzpicture} 
\begin{tikzpicture}[baseline=(m.west)]
\matrix (m) [draw=white!80!black, 
         left delimiter=[,right delimiter={]}, matrix of math nodes] 
         { 1 & 1 \\ 2 & 2 \\ 3 & 3 \\ };
\end{tikzpicture}

\end{document}

Surprisingly (to me at least), this is rendered as follows:

enter image description here

Clearly, the braces for the single-line matrix are aligned differently from the multi-line matrix. I'd expect the braces to be slightly smaller than the grey box in all cases.

Why is this and, more importantly, how do I remedy this?

UPDATE 1: Following @percusse's comment below, the remedy seems to be to add outer sep=0pt to the options of each matrix. Now I run into the next problem: I want to control the amount of space between rows. When adding row sep=0.1em to each matrix (in addition to outer sep=0pt), this is, again surprisingly, rendered as:

enter image description here

Best Answer

I found the problem after hints from @percusse . The reason for the problem is that TeX performs some kind of rounding on the height of the delimiters. This is illustrated by the following code:

\documentclass{standalone}
\begin{document}
$\left[\vcenter{\vbox to 33pt {}}\right]$
$\left[\vcenter{\vbox to 34pt {}}\right]$
$\left[\vcenter{\vbox to 35pt {}}\right]$
$\left[\vcenter{\vbox to 36pt {}}\right]$
$\left[\vcenter{\vbox to 37pt {}}\right]$
$\left[\vcenter{\vbox to 38pt {}}\right]$
$\left[\vcenter{\vbox to 39pt {}}\right]$
$\left[\vcenter{\vbox to 40pt {}}\right]$
\end{document}

which is rendered as follows:

enter image description here

The offending code in PGF is in the file

texmf/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarymatrix.code.tex, 

the following macro is defined:

\def\tikz@delimiter#1#2#3#4#5#6#7#8{%
  \bgroup
    \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name}%
    node[outer sep=0pt,inner sep=0pt,draw=none,draw=none,fill=none,anchor=#1,at=(\tikz@last@fig@name.#2),#3]
    {%
      {\nullfont\pgf@process{\pgfpointdiff{\pgfpointanchor{\tikz@last@fig@name}{#4}}{\pgfpointanchor{\tikz@last@fig@name}{#5}}}}%
      $\left#6\vcenter{\hrule height .5#8 depth .5#8 width0pt}\right#7$%
    }
    \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}%
  \egroup%
}

The first line inside the body of the node calculates the desired height of the delimiter. The second line creates a math environment with left and right delimiters, with the inner body (\vcenter{\hrule height .5#8 depth .5#8 width0pt}) being an empty box of the desired height. However, the delimiters placed around this box are not of the correct size. So: in order to draw the brackets properly, I guess we should resort to using TikZ to draw the brackets (i.e., draw the lines "manually"), instead of letting TeX do the work.

But perhaps a better approach is to scale the TeX-generated brackets slightly to match the correct size. Martin Scharrer already implemented this. His code can be found in: Correct delimiter height in TikZ