Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can the north anchors of the nodes in a going to the right chain be aligned in a tikz picture?

As can be demonstrated by the following example, by default they seem to be aligned by the center anchor.

\documentclass[tikz]{standalone}

\usetikzlibrary{shapes.multipart,chains,}

\tikzset{
  token/.style={
    rectangle split,
    rectangle split parts=2,
    rectangle split part fill={cyan!25,yellow!50},
    rectangle split ignore empty parts,
    draw,
  },
}

\begin{document}

\begin{tikzpicture}[start chain=going right,node distance=1mm]
  \node[on chain,token] {TWhile};
  \node[on chain,token] {TLParen};
  \node[on chain,token] {TId\nodepart{two}ip};
  \node[on chain,token] {TEq};
  \node[on chain,token] {TId\nodepart{two}z};
  \node[on chain,token] {TRParen};
  \node[on chain,token] {TPlusPlus};
  \node[on chain,token] {TId\nodepart{two}ip};
\end{tikzpicture}

\end{document}

Output of the given document:

share|improve this question
    
I believe you are looking for start chain=going base right. Actually using the north anchor would need a different implementation of the <direction>=of <another node> key options. The right in going right means that the west anchor from a new node is placed right=of \tikzchainprevious which means right of \tikzchainprevious.east (if on grid=false). –  Qrrbrbirlbel Jun 16 '13 at 20:56
    
@Qrrbrbirlbel, maybe you want to make an answer from your comment. –  Romildo Jun 17 '13 at 1:05
    
I have posted an answer, though I added some more discussion and ideas that can be helpful (hopefully). –  Qrrbrbirlbel Jun 17 '13 at 17:02

1 Answer 1

up vote 3 down vote accepted

They are actually not aligned by the center anchor (but the center anchor do lie on one straight line as the center anchor is at the same height as the west and the east anchors).

The right in going right means that the west anchor from a new node is placed right=of \tikzchainprevious which means right of \tikzchainprevious.east (see reference 1).

Besides the obvious directions like for example above and below right, there also exist mid left|right and base left|right which uses the mid|base west|east anchors respectively. These anchors are dependent from the actual text line containing the first part node (or the only one). The difference is only a vertical shift as the mid anchors are simply .5ex above their base counter parts.

With start chain=going base right, you’ll get:

enter image description here

This looks weird because:

  • the only q is the only letter in the blue boxes that has a depth. I recommend using either

    • text depth=+0pt, or

      enter image description here

    • text depth=\depthof{q}.

      enter image description here

For the yellow boxes I’d apply font=\vphantom{i} and \smashing the p. Sadly, the keys text depth and text height do not affect node parts (they values aren’t considered at all), you can also use font=\vphantom{ip} to get something similar to text depth=\depthof{q} for the main node text:

enter image description here

Another, rather easy solution would be to use font=\strut for every part:

enter image description here


If one wants to actually use the north anchors (or rather the north east and north west anchors), one can add:

\makeatletter
\tikzset{north left/.code =\tikz@lib@place@handle@{#1}{north east}{-1}{0}{north west}{1}}
\tikzset{north right/.code=\tikz@lib@place@handle@{#1}{north west}{1}{0}{north east}{1}}
\tikzset{south left/.code =\tikz@lib@place@handle@{#1}{south east}{-1}{0}{south west}{1}}
\tikzset{south right/.code=\tikz@lib@place@handle@{#1}{south west}{1}{0}{south east}{1}}

\tikzset{west above/.code=\tikz@lib@place@handle@{#1}{south west}{0}{1}{north west}{1}}
\tikzset{west below/.code=\tikz@lib@place@handle@{#1}{north west}{0}{-1}{south west}{1}}
\tikzset{east above/.code=\tikz@lib@place@handle@{#1}{south east}{0}{1}{north east}{1}}
\tikzset{east below/.code=\tikz@lib@place@handle@{#1}{north east}{0}{-1}{south east}{1}}
\makeatother

and use it with start chain=going north right. It doesn’t make a difference in this case because all nodes have the same (text) height.

They can be used to top-/bottom-/left-/right-align nodes while placing them in a row. (This works best for rectangular node shapes.)
For example, using start chain=going south right creates:

enter image description here

Reference

  1. Package PGF Math Error: Unknown operator `o' or `of' (It’s about the positioning library.)
  2. TikZ multipart nodes: How to set text attributes (opacity) for some/all parts

Code

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{shapes.multipart,chains}
\tikzset{
  token/.style={
    rectangle split,
    rectangle split parts=2,
    rectangle split part fill={cyan!25,yellow!50},
    rectangle split ignore empty parts,
    draw}}

\begin{document}
\begin{tikzpicture}[
  start chain=going base right,
  node distance=1mm,
  every on chain/.append style={
%    font=\strut,
    text depth=+0pt,
    every two node part/.append style={
      font=\strut,
      font=\vphantom{i}
    }
  }
  ]
  \node[on chain,token] {TWhile};
  \node[on chain,token] {TLParen};
  \node[on chain,token] {TId\nodepart{two}i\smash{p}};
  \node[on chain,token] {TEq};
  \node[on chain,token] {TId\nodepart{two}z};
  \node[on chain,token] {TRParen};
  \node[on chain,token] {TPlusPlus};
  \node[on chain,token] {TId\nodepart{two}i\smash{p}};
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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