# Clash between aligned environment and TikZ?

The following code mis-renders the multi-line label:

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{aligned} \begin{tikzpicture} \node (A) at (0,0) {A}; \node (B) at (0,2) {B}; \draw (A) to node [anchor=east, align=right] {my\\multi-line\\label} (B); \end{tikzpicture} \end{aligned}
\end{document}


It's all the fault of the aligned environment - which, however, is necessary to get the equation number to typeset in the proper place with respect to the diagram.

So: is this a weird interaction bug? Can it be solved? Or is there a hack that just lets me align my picture using a different method to avoid this strange issue? Or perhaps my TikZ is buggy, or this isn't the correct way to do multi-line labels.

Screenshot with code as above:

Screenshot without aligned environment:

-
Do you have a complete MWE of "the equation" you mention? I'm pretty sure it can be resolved without "blaming" the aligned environment. :) –  Werner Dec 9 '11 at 19:19
Using split instead of aligned seems to help, but the line spacing is a little different. –  Torbjørn T. Dec 9 '11 at 19:20
An image which shows the incorrect behavior would also be very good. –  Martin Scharrer Dec 9 '11 at 19:23
Hi Werner - I've given a MWE in the question! Typeset it with and without the aligned environment and you'll see what I mean. –  Jamie Vicary Dec 9 '11 at 19:24
If the only thing that you use the aligned environment for is the numbering, you could take a look at tex.stackexchange.com/q/14599/86 which provides a pure TikZ way of putting in equation numbers (the code is at bazaar.launchpad.net/~tex-sx/tex-sx/development/files as tikzeqnos.tex but as the file extension suggests, I haven't worked on it much since the original code. My converting-answers-to-packages time recently has been taken up with some random knot package. Maybe I should switch to the equation number one for a while ...) –  Loop Space Dec 9 '11 at 21:18

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{align}
\tabular{r|} \multicolumn{1}{r@{}}{\makebox[0pt]{A}}\\
my \\ multi-line \\ label \\
\multicolumn{1}{r@{}}{\makebox[0pt]{B}}
\endtabular
\end{align}

\end{document}


-

Just change aligned into gathered:

\begin{gathered} \begin{tikzpicture} \node (A) at (0,0) {A}; \node (B) at (0,2) {B}; \draw (A) to node [anchor=east, align=right] {my\\multi-line\\label} (B); \end{tikzpicture} \end{gathered}


Actually, gathered (as well as aligned) changes the line spacing, so maybe you'll prefer to use a gathered* environmen defined as

\newenvironment{gathered*}{\gathered\openup-1\jot}{\endgathered}


Actually, a simpler solution might be to say

\begin{tikzpicture}[baseline=(current bounding box.center)] \node (A) at (0,0) {A}; \node (B) at (0,2) {B}; \draw (A) to node [anchor=east, align=right] {my\\multi-line\\label} (B); \end{tikzpicture}

-
This is the simplest solution, so I like it the most - but it affects the line spacing! –  Jamie Vicary Dec 9 '11 at 23:23
@JamieVicary Just like aligned. I'll add a workaround. –  egreg Dec 9 '11 at 23:25

Would "boxing" the "my multiline label" work? Boxing here refers to placing the contents in a tabular.

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{aligned} \begin{tikzpicture} \node (A) at (0,0) {A}; \node (B) at (0,5) {B}; \draw (A) to node [anchor=east] {\begin{tabular}{r@{}}my\\[\jot]multi-line\\[\jot]label\end{tabular}} (B); \end{tikzpicture} \end{aligned}
\end{document}


Using a r@{} column specification for the tabular removes the column separation on the right, providing a similar alignment as obtained with align=right. \\[\jot] spreads out the tabular lines, similar to renewing \arraystretch, but allows for line-by-line control. Use it if needed.

-
The boxing isn't necessary, you could just put the tabular directly in the node. –  Torbjørn T. Dec 9 '11 at 19:36
@TorbjørnT.: Thanks for the suggestion... –  Werner Dec 9 '11 at 19:53