39

I have two unaligned nodes

|AAAAAAA|

        |B|

and I want to have the node with the B aligned to the left of the node with AAAs and below it

|AAAAAAA|

|B|

How can this be done?

lockstep
  • 250,273
Mário
  • 1,171
  • 1
    Welcome to tex.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting. – Martin Scharrer Mar 16 '11 at 20:56
  • 1
    Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count. This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). Otherwise please state what is missing so that people can help you better. – Martin Scharrer Dec 07 '11 at 10:05

2 Answers2

49

You can position "B" below and left of "AAA" without having to know the position of it using the positioning library:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
    \node [draw] (A) at (4,5) {AAAA};
    \node [draw,below=of A.west,anchor=west] (B) {B};
\end{tikzpicture}
\end{document}

Gives:

       Result

Martin Scharrer
  • 262,582
  • 4
    Note that you can change the distance by adding a value before of, e.g. below=2cm of A. – Martin Scharrer Mar 16 '11 at 22:29
  • Thanks, your examples helped me to understand the workings of anchor: it makes a node's anchor point (north, east, ...) coincide with the node's position point (defined with "at (x,y)", "below=of p", etc). – Mário Mar 17 '11 at 09:33
  • 2
    For example, using

    \begin{tikzpicture}[node distance=5mm] \node[draw, anchor=west] (1) {AAAAAAAA}; \node[draw, below=of 1.south west, anchor=north west] (2) {B}; \end{tikzpicture}

    we can left align the two nodes and have a precise distance of 5mm between them. Thanks a lot. @MichaelUmmels

    – Mário Mar 17 '11 at 09:49
  • 8
    Important note: one must use below=of A.west, anchor=west (which is what Martin has written) not anchor=west, below=of A.west (which looks just as good, but seems not to work). – John Wickerson Mar 13 '15 at 15:22
  • @MartinScharrer Why does placing anchor=west after below instruction works, but not the other way around? (Just like John pointed out in the comment above) – Andriy Drozdyuk Nov 19 '16 at 18:03
  • @drozzy: below seems to modify/overwrite the anchor position. – Martin Scharrer Nov 25 '16 at 19:36
23

Use anchor=west for both nodes. For example:

\begin{tikzpicture}
\node[anchor=west] (1) at (0,1) {AAAAAAAA};
\node[anchor=west] (2) at (0,0) {B};
\end{tikzpicture}
Michael Ummels
  • 8,604
  • 3
  • 36
  • 27
  • Thanks, I understand now: you are placing the west anchors of two nodes at two vertically aligned points. That does the trick, the only limitation is that if the top node increases its heigth you have to move the point of the second node further below. To avoid this the second node could be placed using "below=of 1.south west". – Mário Mar 17 '11 at 09:42
  • Yes, I agree that Martin's solution (inlcuding node distance) is more flexible. – Michael Ummels Mar 17 '11 at 10:14