20

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

yori
  • 5,681
  • 29
  • 59
  • 1
    I can't check what is eating the option right now but the one-liner needs outer sep=0. Also you don't need brace decoration for the brackets ;) And black!20 gives you the same color. – percusse May 03 '13 at 09:59
  • Ah, great! I would never have though of that. About the brackets: what is your suggested way to draw them without brace decoration? – yori May 03 '13 at 10:10
  • 1
    The delimiters don't need any brace decoration. Try it without the decoration library and the decoration=brace options. It will still work. – percusse May 03 '13 at 10:11
  • @percusse: Then the next question is: why does adding row sep=0.1em introduce the same unexpected shift again for the second (two-by-two) matrix? – yori May 03 '13 at 10:27
  • Instead outer sep=0 perhaps outer ysep=0 is enough – Alain Matthes May 03 '13 at 10:28
  • 1
    @Yori Indeed, as I've commented above, something is eating the proper height calculation. It smells like a bug. I hope I'll have time later to read the code to try to understand the cause. Maybe someone else would come up with a fix in the meantime. – percusse May 03 '13 at 10:47
  • @percusse See update 2 in my question. – yori May 04 '13 at 01:15
  • Ah great you found Martin's fix. I'm safe now :P – percusse May 04 '13 at 09:50

1 Answers1

4

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

yori
  • 5,681
  • 29
  • 59