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

Compare this example:

\settowidth{\tikztwo}{\,two (2) lines\,}
\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};

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.


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:

    \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};
  • 1
    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. Nov 22 '11 at 6:48
  • 1
    @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
  • Is there a way to make this work with align=center instead of text width=<len>? For me it align to the last line instead of the first…
    – Archange
    Feb 16 '20 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.