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.
