31

I'm wanting to align/position a node relative to 2 others. Basically I'd like everything to be in rows and columns (whose is determined by the largest member). In this specific instance, I'd like node j to be centered on the intersection of the red and blue lines in the picture below. This would cause the 2 decision diamonds to be evenly spaced like f and g.

\documentclass{article} \usepackage{tikz} \begin{document} \pagestyle{empty} \usetikzlibrary{shapes, arrows, calc, positioning} % Define block styles \tikzstyle{state} = [ rounded rectangle, draw, text centered, minimum height=3em ] \tikzstyle{test} = [ diamond, draw, shape aspect=2, inner sep = 0pt, text width = 7em, text centered ] \tikzstyle{action} = [ rectangle, draw, text width=8em, inner sep = 5pt, minimum height=5em ] \begin{tikzpicture}[node distance = 1.25em, align = flush center, font = \small] % Place nodes \node [test] (f) {fLorem ipsum dolor sit amet}; \node [test, right=of f] (g) {gLorem ipsum dolor}; \node [test, below=of f] (h) {hLorem ipsum dolo}; \node [action, below=of h] (i) {iLorem ipsum dolor sit amet, consectetur adipiscing elit.}; \node [action, right=of i, fill=gray] (j) {jLorem ipsum dolor sit amet, consectetur adipiscing elit.}; \node [test, below=of i] (l) {lLorem ipsum dolor}; \node [test, below=of j, fill=gray] (m) {mLorem ipsum dolor}; \draw [red] (g) -- +(0,-10); \draw [blue] (i) -- +(10,0); \end{tikzpicture} \end{document} 

alignment

I've read some promising things about chains but think it might be over kill for my use.

2 Answers 2

35

There are two easy approaches to this. If you really just want to place one node "at the intersection of the red and blue lines", you can easily use the perpendicular coordinate system (pgfmanual Section 13.3 "Coordinates at Intersections") which is designed for exactly this use case. This is shown in the first tikzpicture in the code below.

However, as you also requested "everything to be in rows and columns (whose [size] is determined by the largest member)", there is a far better and far more sensible way to achieve the desired effect. The second example uses the TikZ \matrix command, which is equivalent to \node[matrix] (pgfmanual Section 20 "Matrices and Alignment"). With matrices, you can easily align things in centered rows and columns. The usage is similar to tabular but the cells directly contain TikZ code instead of TeX code.

Here's an example image of the two techniques applied, followed by the code that produced the image.

Output of example document

\documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \node[draw] at (0,0) (left node) {Left Node}; \node[draw] at (3,2) (top node) {Top Node}; \node[draw] at (left node -| top node) {Node at intersection}; %\node[draw,circle] at (top node |- left node) {Another node at the same position}; \draw [red] (top node) -- +(0,-3); \draw [blue] (left node) -- +(6,0); \end{tikzpicture} \vspace{1cm} \begin{tikzpicture} \matrix[column sep=1cm, row sep=1cm] (m) { & \node[draw] (top node) {Top Node};\\ \node[draw] (left node) {Left Node}; & \node[draw] {Intersection Node};\\ }; \draw [red] (top node) -- +(0,-3); \draw [blue] (left node) -- +(6,0); \end{tikzpicture} \end{document} 
3
  • 1
    You can further use the matrix of nodes key to avoid explicitly typing \node... for each cell. The options the nodes can be given through nodes={draw} key. Also they are directly named as (m-row no-col no) hence you can skip the names. Commented Sep 5, 2012 at 22:30
  • @percusse: Yes, but doesn't that make adding options to individual cell-nodes more difficult? In the OP's MWE it looks like he'll have very diverse styles for each node, which is why I used explicit \nodes with explicit styles. You're probably right though that I should at least mention that in my answer. Commented Sep 5, 2012 at 22:45
  • You are right. Maybe I was carried away a little there but you can overwrite the individual options via &|[draw,blue]| node text per entry which is slightly less work. Commented Sep 5, 2012 at 22:50
5

You can use the let syntax:

\documentclass{article} \usepackage{tikz} \begin{document} \pagestyle{empty} \usetikzlibrary{shapes, arrows, calc, positioning} % Define block styles \tikzstyle{state} = [ rounded rectangle, draw, text centered, minimum height=3em ] \tikzstyle{test} = [ diamond, draw, shape aspect=2, inner sep = 0pt, text width = 7em, text centered ] \tikzstyle{action} = [ rectangle, draw, text width=8em, inner sep = 5pt, minimum height=5em ] \begin{tikzpicture}[node distance = 1.25em, align = flush center, font = \small] % Place nodes \node [test] (f) {fLorem ipsum dolor sit amet}; \node [test, right=of f] (g) {gLorem ipsum dolor}; \node [test, below=of f] (h) {hLorem ipsum dolo}; \node [action, below=of h] (i) {iLorem ipsum dolor sit amet, consectetur adipiscing elit.}; \path let \p1=(g), \p2=(i) in node[action, fill=gray] (j) at (\x1,\y2) {jLorem ipsum dolor sit amet, consectetur adipiscing elit.}; \node [test, below=of i] (l) {lLorem ipsum dolor}; \node [test, below=of j, fill=gray] (m) {mLorem ipsum dolor}; \draw [red] (g) -- +(0,-10); \draw [blue] (i) -- +(10,0); \end{tikzpicture} \end{document} 

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.