8

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

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\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{equation}
\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 with code as given

Screenshot without aligned environment: screenshot without aligned environment

Torbjørn T.
  • 206,688
Jamie Vicary
  • 11,098
  • 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 09 '11 at 19:19
  • Using split instead of aligned seems to help, but the line spacing is a little different. – Torbjørn T. Dec 09 '11 at 19:20
  • An image which shows the incorrect behavior would also be very good. – Martin Scharrer Dec 09 '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 09 '11 at 19:24
  • i'll wait to see the answers before saying whether this is or is not likely to be a bug; it certainly seems to be the result of unfortunate interaction. all i can say for sure at the moment is that tikz didn't exist when amsmath was created, and to the best of my knowledge, no one has ever explicitly tested the two packages together in this way. i'll check back later. – barbara beeton Dec 09 '11 at 19:54
  • 2
    If the only thing that you use the aligned environment for is the numbering, you could take a look at http://tex.stackexchange.com/q/14599/86 which provides a pure TikZ way of putting in equation numbers (the code is at http://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 ...) – Andrew Stacey Dec 09 '11 at 21:18
  • @AndrewStacey But knot package is also an impressive one though :) – percusse Dec 10 '11 at 00:23

3 Answers3

6
\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} 

enter image description here

Moriambar
  • 11,466
5

Just change aligned into gathered:

\begin{equation}
\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}
\end{equation}

enter image description here

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{equation}
\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}
\end{equation}
egreg
  • 1,121,712
3

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

enter image description here

\documentclass{article}
\usepackage{tikz}% http://ctan.org/pkg/pgf
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{equation}
\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{equation}
\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.

Werner
  • 603,163