[Tex/LaTex] How to get TikZ nodes not to change alignment

arraysmatricesnodestikz-pgfvertical alignment

I'm still working on rendering a nice block matrix. I've been using BMAT but it is irrelevant for the question at hand. I want to connect various entries in an array/matrix/BMAT by lines, and I'm using TikZ for that. However, I've run into this problem: When I surround an entry with a TikZ node (for drawing a path later), it changes the vertical alignment and basically screws everything up.

Here's a MWE:

\documentclass{article} 
\usepackage{tikz}

\begin{document}
\begin{equation}
\begin{array}{ccc}
 2 & \tikz \node {$0$}; & \frac{2}{3} \\
 0 & \frac{4}{3} & 0 \\
 -\frac{2}{3} & \tikz \node [inner sep=0pt,outer sep=0pt]{$0$}; & 
   \tikz \node [inner sep=0pt,outer sep=0pt]{$\frac{6}{5}$}; \\
 0 & -\frac{4}{5} & 0 \\
\end{array}
\end{equation}

\end{document}

With output:

matrix with mis-aligned entries

As you can see, The entries that have nodes around them are vertically mismached and differently aligned from the others. Setting outer sep and inner sep to zero doesn't help….

Any ideas?

Best Answer

You could use the \matrix command provided by the pgf/tikZ package. See the sample below for some inspiration.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
  \begin{equation}
    \begin{tikzpicture}[baseline=(current bounding box.west)]
      \matrix [%
        matrix of math nodes,
        text centered
      ] {%
        2 & 0 & \frac{2}{3} \\
        0 & \frac{4}{3} & 0 \\
        -\frac{2}{3} & 0 & -\frac{4}{5} \\
      };
    \end{tikzpicture}
  \end{equation}
\end{document}