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:
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:
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:
which is rendered as follows:
The offending code in PGF is in the file
the following macro is defined:
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