Node baseline in tikz
Did you try anchor=mid
as I suggested yesterday? Did you try looking it up to find out what it does?
Presumably not ...
mynode/.style={rectangle, anchor=mid, text height=1em, font=\sffamily, text width=7.5em, align=center, draw},
gives me
Now, clearly, you cannot have your cake and eat it. If you align the nodes with .mid
then they will be aligned on the baseline. Well and good. Then you have three choices. You could use text depth
to fix a larger depth, but this will result in one-liners looking really silly.
Or you could tolerate the arrows between the nodes not going due East.
Or you can specify the anchors for the arrows so that these are also aligned relative to .mid
, which is the solution shown above and demonstrated below. This seemed to me the only workable solution and it permits easy customisation for particular cases when required.
I also set the text height
to avoid small variations in the vertical location of the north borders of the nodes. Just remove it if you prefer not to use it.
Code:
\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{graphs, matrix, arrows.meta}
\begin{document}
\tikzset{%
my graph/.style={matrix of nodes, inner sep=.25em, row sep= 1.5em, column sep= 6.25em},
mynode/.style={rectangle, anchor=mid, text height=1em, font=\sffamily, text width=7.5em, align=center, draw},
}
\begin{tikzpicture}[>=Stealth]
\matrix[my graph]{
|[mynode] (prev)| prev &
|[mynode] (cur)| my node &
|[mynode] (next)| next \\
};
\graph[use existing nodes, edges= rounded corners] {
prev.mid east -> cur.mid west ; cur.mid east -> next.mid west;
};
\draw[red] (cur.base-|prev.west)--(cur.base-|next.east);
\begin{scope}[yshift=-20mm]
\matrix[my graph]{
|[mynode] (prev)| Look at this text. Lorem ipsum... &
|[mynode] (cur)| Now look at this text. Lorem ipsum... &
|[mynode] (next)| Finally, look at this text too. Lorem ipsum... \\
};
\graph[use existing nodes, edges= rounded corners] {
prev.mid east -> cur.mid west ; cur.mid east -> next.mid west;
};
\draw[red] (cur.base-|prev.west)--(cur.base-|next.east);
\end{scope}
\end{tikzpicture}
\end{document}
You need to define text height
and text depth
, as is explained in chapter 5 "Turorial: Diagrams as Simple Graphs" from pgfmanual
. Especially § 5.1 states:
The nodes suddenly “dance around”! There is no hope of changing the position of text inside a node using anchors. Instead, Ilka must use a trick: The problem of mismatching baselines is caused by the fact that . and digit and E all have different heights and depth. If they all had the same, they would all be positioned vertically in the same manner. So, all Ilka needs to do is to use the text height and text depth options to explicitly specify a height and depth for the nodes
\documentclass[12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs, matrix, arrows.meta}
\begin{document}
\tikzset{graph/.style={matrix of nodes, inner sep=.25em, row sep= 1.5em,
column sep= 1.5em, column sep= 6.25em}}
\tikzset{mynode/.style={rectangle, anchor=center, minimum height=1.5em,
font=\sffamily, text width=7.5em,text height=1.5ex, text depth=.25ex, align=center, draw}}
\begin{tikzpicture}[>=Stealth]
\matrix[graph]{
|[mynode] (prev)| prev &
|[mynode] (cur)| my node &
|[mynode] (next)| next \\
};
\graph[use existing nodes, edges= rounded corners] {
prev -> cur -> next;
};
% \draw[red] (cur.base-|prev.west)--(cur.base-|next.east);
\end{tikzpicture}
\end{document}