# How to get TikZ nodes not to change 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{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{document}


With output:

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?

-
I'm not sure how to solve it (which is why I'm leaving this as a comment), but I think that the problem is due to the fact that the fractions dip below the line on which the text sits, so the box that contains the tikz stuff should be typeset below the line. Normally (especially with inner sep and outer sep 0pt), it rests nicely on the line. Axy \tikz \node[inner sep=0pt,outer sep=0pt] {Axy}; illustrates the problem. –  Andrew Stacey Sep 30 '10 at 12:49

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{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{document}

-
Great! this works. only one comment needed: to name a node, place |nodename| between vertical bars. I saw it in an example, but couldn't find the documentation of it. –  Yossi Farjoun Sep 30 '10 at 16:00
@Yossi: Or you can name the matrix and then refer to its components by index: (m-1-1) for example. –  Andrew Stacey Sep 30 '10 at 18:58

I knew I'd read something about this in the pgf manual, but couldn't remember where. I've just read it again, so here's another answer. I still recommend Thorsten's solution, but I can imagine times when that isn't possible so here's how to do exactly what is asked in the question: use the baseline option on the \tikz command. That tells TeX where the "baseline" of the picture should be (and so where it should rest on the current line). The great thing is that this is computed after the picture is drawn and so can make use of information computed whilst the picture was drawn. Here's an example:

\documentclass{article}
\usepackage{tikz}

\tikzstyle{matnode} = [inner sep=0pt,outer sep=0pt,anchor=base,remember picture,overlay]

\begin{document}
$$\begin{array}{ccc} 2 & \tikz \node[inner sep=0pt,outer sep=0pt] {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,anchor=base]{\frac{6}{5}}; \\ 0 & -\frac{4}{5} & 0 \\ \end{array} \begin{array}{ccc} 2 & 0 & \frac{2}{3} \\ 0 & \frac{4}{3} & 0 \\ -\frac{2}{3} & 0 & \frac{6}{5} \\ 0 & -\frac{4}{5} & 0 \\ \end{array} \begin{array}{ccc} 2 & \tikz[baseline=0pt] \node[matnode] (a) {0}; & \frac{2}{3} \\ 0 & \frac{4}{3} & 0 \\ -\frac{2}{3} & \tikz[baseline=0pt] \node [matnode] (b) {0}; & \tikz[baseline=0pt] \node [matnode] (c) {\frac{6}{5}}; \\ 0 & -\frac{4}{5} & 0 \\ \end{array}$$

\end{document}


Incidentally, here's a trick I use for trying to figure out where the alignment is right or wrong. Stare at the document on the screen and then make your eyes go "cross-eyed" until one eye is seeing one version and the other eye is seeing the other version. Then any mis-alignments will cause the text to "jump" in or out of the page. You should look for large jumps as even a pixel difference will be noticeable.

-
Good 3-d vision trick..but it only works for matrices! –  Yossi Farjoun Oct 4 '10 at 14:32
It works! so now I can use BMAT and TikZ together!! yey! –  Yossi Farjoun Oct 4 '10 at 14:38
@Yossi: I use it whenever I've got one of these weird alignment problems, not just with matrices. It's a trick I learnt from those funny dotty pictures that were all the rage a few years ago. Anyway, glad the solution works for you. –  Andrew Stacey Oct 4 '10 at 15:17
Running the risk of having too many comments, let me just add that I found the [overlay] option for the nodes detrimental. (for the paths it is a must), I ended up defining the [matnode] style as follows: \tikzstyle{matnode} = [inner sep=0pt,outer sep=3pt,anchor=base,remember picture], whihc gave me the best results....feel free to incorporate this into your answer or leave it as a comment. –  Yossi Farjoun Oct 5 '10 at 10:46

I'd go for the "putting everything within one diagram" approach in this case, but sometimes that's not possible. An alternative is to put empty nodes at the places where you want the lines (or whatever) to go to:

\documentclass{article}
\usepackage{tikz}
\begin{document}
$$\begin{array}{ccc} 2 & \tikz \node[inner sep=0pt, outer sep=0pt]{};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{document}


(don't forget to label the nodes, and to refer to them outside the tikz command used to define them, then you need to add the options remember picture and overlay; see the pgfmanual for more on these)

-