6

I'm trying to create a diagram and would like my nodes left aligned using the anchor=west option. I'd prefer to use relative positioning to make my diagram more flexible to changes. Here's my example:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}


\begin{tikzpicture}[%
  every node/.style = {anchor=west}]

\node[fill=red!40, draw] (n0) at (0,0) {Base node} ;
\node[fill=red!40, draw] (n1) at (0,-2) {Node with longer text} ;
\node[fill=red!40, draw] (n2) [below=of n1] {Node with even longer text} ;

\end{tikzpicture}
\end{document}

Notice how the second node uses the anchor=west style set for every node, but the third node with the longest text seems to be centered below the second vs. being left aligned with it. Is there a way to accomplish what I'm looking for?

Essentially, I want the alignment of the second node without having to specify absolute coordinates.

enter image description here

2
  • I'm thinking I answered my own question. Found this as I was scanning the manual: If you use relative coordinates together with automatic anchor coordinates, the relative coordinates are computed relative to the node's center, not relative to the border point. If there's a way to do this (like override the setting, or use some automatic math to adjust based on half the width of each node...), I'd still like to know.
    – Hendy
    Aug 24, 2012 at 20:17
  • 1
    Maybe this might help: tex.stackexchange.com/a/63469/3235
    – percusse
    Aug 24, 2012 at 22:45

2 Answers 2

6

try this

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}


\begin{tikzpicture}[%
  every node/.style = {anchor=west}]

\node[fill=red!40, draw] (n0) at (0,0) {Base node} ;
\node[fill=red!40, draw] (n1) at (0,-2) {Node with longer text} ;
\node[fill=red!40, draw] (n2) [below=of n1.west, right] {Node with even longer text} ;

\end{tikzpicture}
\end{document}

enter image description here

from de pgfmanual:

16.5.2 Basic Placement Options

Unfortunately, while perfectly logical, it is often rather counter-intuitive that in order to place a node above a given point, you need to specify the south anchor. For this reason, there are some useful options that allow you to select the standard anchors more intuitively:

/tikz/above (default 0pt)

Does the same as anchor=south. If the is specified, the node is additionally shifted upwards by the given .

above \tikz \fill (0,0) circle (2pt) node[above] {above};

above \tikz \fill (0,0) circle (2pt) node[above=2pt] {above};

/tikz/below=<offset>(default 0pt) Similar to above.

/tikz/left=<offset> (default 0pt) Similar to above.

/tikz/right=<offset> (default 0pt) Similar to above.

2
  • Could you explain what this is doing? I didn't see the right alignment in the TikZ manual for all of the relative positioning options... looks like it works, though!
    – Hendy
    Aug 24, 2012 at 22:25
  • So above sets the anchor to .south and right sets it to .west, correct? And this puts it below node.west and sets the anchor of the node being placed to .west as well. I'm still not entirely grasping passing multiple placement options, and didn't see them combined in the manual, but nevertheless, this is definitely what I was looking for! Thanks!
    – Hendy
    Aug 25, 2012 at 16:48
4

Implicitly, below implies anchor=north (and with below=of n1, the reference is n1.south), left implies anchor=east, etc.

An example (blue node shows that default reference is not n1.center, lime node shows the desired positioning):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[every node/.style={anchor=west}]

\node[fill=red!40, draw] (n0) at (0,0) {Base node} ;
\node[fill=red!40, draw] (n1) at (0,-2) {Node with longer text} ;

\node[fill=red!40, draw,below=of n1] (n2) {Node with even longer text} ;

\node[fill=blue!40, draw,below=of n1.center] (n2) {Node with even longer text} 

\node[fill=lime!40, draw,below=of n1.south west,anchor=north west]
 (n2) {Node with even longer text} ;

\end{tikzpicture}
\end{document}

enter image description here

2
  • Just to be clear, the answer is "No," right? I have to set the anchor for each node manually since it's overridden by below=of...?
    – Hendy
    Aug 24, 2012 at 22:24
  • @Hendy you can easily define a style to factorize your positioning rule. But you are right: anchor and positioning are not independent... Aug 24, 2012 at 22:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .