4

How to align a node vertical align to one node, and horizontal align to another one?

For example, given below code

\documentclass{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{makecell}
\usetikzlibrary{arrows,shapes,automata,petri,positioning}
\tikzset{
    data/.style={
        ellipse,
        thick,
        draw=black,
        minimum size=6mm
    },
    process/.style={
        rectangle,
        thick,
        draw=black
    },
} 

\pagestyle{empty}
\begin{document}
    \begin{tikzpicture}[node distance=1 and 1,>=stealth',bend angle=45,auto]
        \node [data] (a) {a};

        \node [process,align=center] (b) [below= of a] {bbbbbbbbbbb}
            edge[pre] (a);

        \node [data,align=center] (c) [below= of b]{cc\\ccc\\ccccc}
            edge[pre]   (b);

        \node [process,align=center] (d) [below right= of c] {dddd\\ddddddd}
            edge[pre]   (c);

        \node [data]    (e)     [above right= of d] {e}
            edge[post]  (d);

        \node [process] (f) [above= of e]   {f}
            edge[post]  (e);

        \node [data]    (g)    [above= of f]   {g}
            edge[post]  (f);
    \end{tikzpicture}
\end{document}

Result: enter image description here

I want three nodes e, f, g will be vertical base/mid/center alignment, and (a, g), (bb, f) (ccc,e) will be horizontal base/mid/center alignment.

For example, above image is what I want. Note that the red line will not be displayed, it is represented for "base alignment line"

enter image description here

Thanks for your help.

3
  • Maybe this will be clearer to more experienced tikz users but I really don't understand what you are trying to do.
    – cfr
    May 30, 2014 at 2:34
  • @cfr I try to draw the result. Sorry for my bad description
    – thuanle
    May 30, 2014 at 3:19
  • you might try a matrix of nodes
    – cmhughes
    May 30, 2014 at 4:09

2 Answers 2

3

You can use `orthogonal coordinates like:

\node [process] (f) at (b-|g) {f}

Code:

\documentclass{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{makecell}
\usetikzlibrary{arrows,shapes,automata,petri,positioning,calc}
\tikzset{
    data/.style={
        ellipse,
        thick,
        draw=black,
        minimum size=6mm
    },
    process/.style={
        rectangle,
        thick,
        draw=black
    },
}

\pagestyle{empty}
\begin{document}
    \begin{tikzpicture}[node distance=1 and 1,>=stealth',bend angle=45,auto]
        \node [data] (a) {a};
        \node [data]    (g)    [right = 4cm of a]   {g};
        \node [process,align=center] (b) [below= of a] {bbbbbbbbbbb}
            edge[pre] (a);
        \node [process] (f) at (b-|g) {f}
              edge[pre]  (g);

        \node [data,align=center] (c) [below= of b]{cc\\ccc\\ccccc}
            edge[pre]   (b);
        \node [data] (e) at (c-|f) {e}
              edge[pre]  (f);

        \node [process,align=center,anchor=center] (d) at ([yshift=-2cm]$(c.center)!0.5!(e.center)$)
           {dddd\\ddddddd}
            edge[pre]   (c) 
            edge[pre]   (e);     
    \end{tikzpicture}
\end{document}

enter image description here

With tikz matrix:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,shapes,automata,petri}
\begin{document}
\tikzset{
    data/.style={
        ellipse,
        thick,
        draw=black,
        minimum size=6mm
    },
    process/.style={
        rectangle,
        thick,
        draw=black
    },
}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,nodes={align=center,anchor=center},%
                column sep=1cm,%
                row sep=1cm]
{
 |[data]|a                                  &  &  |[data]| g \\
 |[process]| bbbbbbbbbbb                    &  &  |[process]| f \\
 |[data,text width = 0.8cm]|cc  ccc ccccc   &  &  |[data]| e \\
            &  |[process,text width = 2cm]| dddd ddddd$d$ &    \\
  };
 \draw (m-1-1) edge[post] (m-2-1)
       (m-2-1) edge[post] (m-3-1)
       (m-3-1) edge[post] (m-4-2);
  \draw (m-1-3) edge[post] (m-2-3)
       (m-2-3) edge[post] (m-3-3)
       (m-3-3) edge[post] (m-4-2);
\end{tikzpicture}
\end{document}
3
  • Thanks so much for the answer. This is exactly I am looking for. Can you post me some keyword for the orthogonal coordinates. I want to learn more (then I will ask less). I try to find in pgf manual at (ctan.org/pkg/pgf) but it is too long.
    – thuanle
    May 30, 2014 at 4:45
  • 1
    @poiu060: It is easy. (c-|f) means x coordinate same as that of f and y coordinate as that of c. So you take the x-coordinate of f and y-coordinate of c.
    – user11232
    May 30, 2014 at 4:55
  • 1
    @poiu060: Refer to pgfmanual section 13.3.1 Intersections of Perpendicular Lines pages 138-
    – user11232
    May 30, 2014 at 4:58
1

This is a possible solution where matrix of nodes are used for the alignment. In particular the (f) to (e) line applies orthogonal coordinate skill.

enter image description here

Code

\documentclass{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{makecell}
\usetikzlibrary{matrix,arrows,shapes,automata,petri,positioning,calc}
\tikzset{
    data/.style={
        ellipse,
        thick,
        draw=black,
        minimum size=6mm
    },
    process/.style={
        rectangle,
        thick,
        draw=black
    },
line/.style = {draw, -latex'},
} 

\pagestyle{empty}

\begin{document}
\begin{tikzpicture}[node distance=1 and 1,>=stealth',bend angle=45,auto]

 \matrix[matrix of nodes, column sep=1cm, row sep=1cm]{
\node [data] (a) {a};
&
\node [data] (g) {g};    \\
\node [process,align=center] (b) {bbbbbbbbbbb};
&   \node [process] (f)   {f}; \\
\node [data,align=center] (c){cc\\ccc\\ccccc};   
&\node [data]  at (c.center) (e)  {e};\\
};
%Draw edges
\node [process,align=center,yshift=-2cm] (d) at ($(c)!0.5!(e)$,0){dddd\\ddddddd};
 \draw[line] (a)  --  (b);
 \draw[line] (b)  --  (c);
 \draw[line] (c)  --  (d);
 \draw[line] (g)  --  (f);
 \draw[line] (f)  --  (f|-c.north);
 \draw[line] (e)  --  (d);
\end{tikzpicture}
\end{document}
1
  • Thanks for your solution. I prefer the answer of @HarishKumar because of arrows displayed better.
    – thuanle
    May 30, 2014 at 4:43

You must log in to answer this question.

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