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.

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

share|improve this question
    
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 '12 at 20:17
1  
Maybe this might help: tex.stackexchange.com/a/63469/3235 –  percusse Aug 24 '12 at 22:45
add comment

2 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
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 '12 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 '12 at 16:48
add comment

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

share|improve this answer
    
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 '12 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... –  Paul Gaborit Aug 24 '12 at 22:32
add comment

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.