6

MWE:

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\pagestyle{empty}

\usepackage{tikz}
\usetikzlibrary{graphs, matrix, arrows.meta}

\begin{document}
  \tikzset{graph/.style={matrix of nodes, inner sep=.25em, row sep= 1.5em,
      column sep= 6.25em}}
  \tikzset{mynode/.style={rectangle, anchor=center, minimum height=1.5em,
      font=\sffamily, text width=7.5em, 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;
    };
  \end{tikzpicture}
\end{document}

Three nodes with glyphs going below baseline

"my node" and "next" are properly aligned, but if you check "prev", you'll see that the letter "p" is above the line.

How can I fix this? Please note that I may have nodes with more than one line of text, so it should be taken into account.

UPDATE 1:

The proposed fix by Ignasi works, but even after deleting the text height property, it seems to limit the node height to just one line worth of text. This breaks nodes who have multiple lines of text and are styled with mynode too:

\documentclass[12pt]{standalone}

%\usepackage{luatex85} %I'm using lualatex
\usepackage{tikz}
\usetikzlibrary{graphs, matrix, arrows.meta}

\begin{document}
  \tikzset{graph/.style={matrix of nodes, inner sep=.25em, row sep= 1.5em,
      column sep= 6.25em}}
  \tikzset{mynode/.style={rectangle, anchor=center, minimum height=1.5em, 
      font=\sffamily, text width=7.5em, text depth=.25ex, 
      align=center, draw}}

  \begin{tikzpicture}[>=Stealth]
    \matrix[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 -> cur -> next;
    };
    \draw[red] (cur.base-|prev.west)--(cur.base-|next.east);
  \end{tikzpicture}

\end{document}

Broken example

  • anchor=mid ?? – cfr Jun 16 '16 at 14:23
2

You need some text height and text depth as is explained in tutorial Diagram as Simple Graphs from pgfmanual.

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

enter image description here

  • It works, but only for single line nodes. If one sets text depth and text width, the node will not stretch beyond minimum width and the text will overflow the node. – amyspark Jun 17 '16 at 13:20
  • @amyspark As I'm not sure about the meaning of your comment, please update the question with a non working example and what you would like to obtain in this case. – Ignasi Jun 17 '16 at 14:37
  • Sorry for the confusion, I'll update it now. – amyspark Jun 18 '16 at 1:44
  • @Ignasi, to me is not clear, where should be baseline (at the first line?), does the nodes heights can be different etc. Demands in question are contradictory. – Zarko Jun 18 '16 at 2:02
  • I wanted not to style nodes depending on whether they have one or more lines of text (see the non-working example above wrttext depth). – amyspark Jun 19 '16 at 19:04
4

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

aligned to <code>.mid</code> anchor

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.

fixed depth

Or you could tolerate the arrows between the nodes not going due East.

angled arrows

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}

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.