# TikZ: Positioning of multiline nodes

Do you know an elegant way to align multiline nodes according to first line, e.g. for use in a legend?

Compare this example:

\documentclass{article}
\usepackage[%
paperwidth=7.5cm,%
paperheight=5.5cm,%
margin=0.25cm,%
marginparwidth=0cm,%
centering,%
]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}
\pagestyle{empty}
%%%%%%%%%%%%%%%%
\begin{document}
\sffamily
%--------------------------------------%
\newlength{\tikztwo}
\settowidth{\tikztwo}{\,two (2) lines\,}
\newlength{\tikzthree}
\settowidth{\tikzthree}{\ three (3)\,}
%--------------------------------------%
\begin{tikzpicture}[very thick,every node/.style={node distance=0.2cm}]
% The grid:
\draw[step=0.1cm,very thin, lightgray] (0,0) grid (6,4.5);
\draw[step=0.5cm,thin, gray] (0,0) grid (6,4.5);
%------------------------------------------------%
% Only for comparison:
\draw[blue,text=black] (0.25,3.5) -- (1,3.5)
node[anchor=west]{one line entry};
%------------------------------------------------%
% No positioning at all, "anchor=north west" is even worse:
\draw[magenta,text=black] (3.25,3.5) -- (4,3.5)
node[anchor=west,text width=\tikztwo]{two lines entry 0};
%------------------------------------------------%
% First attempt, node (A) only for positioning:
\draw[green] (0.25,2.5) -- (1,2.5) node[anchor=west](A){two};
\draw (2.12,2.285) node[text width=\tikztwo]{two (2) lines entry 1};
% Second attempt, node (posA) only for positioning:
\draw[green] (3.25,2.5) -- (4,2.5) node[anchor=west](nodeA){} node[anchor=west](posA){two}
node[below right=-0.439cm and -0.276cm of nodeA,text=black,text width=\tikztwo]
{two (2) lines entry 2};
%------------------------------------------------%
% First attempt, node (B) only for positioning:
\draw[red] (0.25,1.5) -- (1,1.5) node[anchor=west](B){three};
\draw (1.88,1.048) node[text width=\tikzthree] {three (3) lines entry 1};
% Second attempt, node (posB) only for positioning:
\draw[red] (3.25,1.5) -- (4,1.5) node[anchor=west](nodeB){} node[anchor=west](posB){three}
node[below right=-0.411cm and -0.276cm of nodeB,text=black,text width=\tikzthree]
{three (3) lines entry 2};
\end{tikzpicture}
\end{document}


You can see first a two line node without positioning. After that I show the two attempts to manage what I want. Both have drawbacks, though: With both the positioning is done manually.
With he first one you have to change the coordinates of the nodes, if you change the coordinates of the path.
With the second this one does not happen, but you have to change the coordinates, if you change the text, exactly if a sign with uppercase length is added or removed (remove the number with the brackets for a test). Also it is not intuitive, that there I have to change the second coordinate to have an effect in the x-axis and vice versa.

BTW: The nodes that I marked “only for positioning” can be removed or commented out after the work is done.

-
I just posted an answer to a very similar question in another thread: tex.stackexchange.com/questions/39597/… I think it might help ;-) –  Sašo Živanović Jul 21 '12 at 7:00
@SašoŽivanović: Thanks for the hint! –  Speravir Jul 22 '12 at 0:31

If you use the anchor mid west for aligning the node, this will refer to the mid height of the first line, giving you the result you want:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [blue, text=black] (0.25,3.5) -- ++(0.75,0) node (A) [anchor=mid west]{one line entry};
\draw [magenta, text=black] (A.east) -- ++(0.75,0) node (B) [anchor=mid west, text width=1.5cm]{two lines entry 0};
\end{tikzpicture}
\end{document}

-
Perhaps better : \draw[blue,text=black] (0.25,3.5) -- ++(.75,0) node (A) [anchor=mid west]{one line entry};and then \draw[magenta,text=black] (A.east) -- ++(.75,0) node (B) [anchor=mid west,text width=1.5cm]{two lines entry 0}; . This is more simple to manage. –  Alain Matthes Nov 22 '11 at 6:48
@Altermundus: I just used the OP's example, but you're right, might as well make the code nicer. –  Jake Nov 22 '11 at 6:56
@Jake: Aah, I didn't know, that it works for multiline nodes. –  Speravir Nov 22 '11 at 15:15
@Altermundus: Yes, it's much better. I've forgotten that. –  Speravir Nov 22 '11 at 15:21