6
\usepackage{tikz}


\begin{document}

\begin{tabular}[b]{@{}ccc@{}}
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture}
&
$\sim$
&
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture} \\
\end{tabular}

\end{document}

enter image description here

I'm really sorry but I searched a lot but really couldn't find a solution. I want to include two pictures and one math symbol here in the tabular. I thought the default vertical position is centered! but I cannot position the \sim symbol in the middle! Thank you in advance for any help! I apologise for being such a noob!

1

3 Answers 3

7

You can set the baseline of the tikz picture to the center of the baseline:

\begin{tikzpicture}[grow=right,sloped,baseline=(current bounding box.center)]

Full code:

\documentclass{article}
\usepackage{tikz}


\begin{document}

\begin{tabular}[b]{@{}ccc@{}}
\begin{tikzpicture}[grow=right,sloped,baseline=(current bounding box.center)]   %% changed here
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}}
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}
  };
\end{tikzpicture}
&
$\sim$
&
\begin{tikzpicture}[grow=right,sloped,baseline=(current bounding box.center)]   %%% changed here
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}}
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}
  };
\end{tikzpicture} \\
\end{tabular}

\end{document}

enter image description here

3
  • Aligning on the baseline may not be optimal in this case -- the entire diagram may be easier to parse if the \sim symbol is aligned on the height of the square boxes.
    – Mico
    Sep 7, 2013 at 22:51
  • @Mico You are right. But the OP wants it in the middle.
    – user11232
    Sep 7, 2013 at 22:55
  • @HarishKumar This is a brilliant solution!!! Many many thanks to Mico as well!!! I now have two approaches!!!!
    – Jino
    Sep 8, 2013 at 10:59
6

Rather than center the \sim symbol vertically relative to the full height of the two decision trees, it may be preferable to place the symbol a bit lower so that it lines up with the symbol with the square boxes. After all, the square boxes form the "entry points" to the tree structures, so it's most "logical" to place the \sim symbol next to the entry points. One method for manually adjusting the symbol's height, used in the example below, is the "primitve" TeX command \raise.

Finding the right height for the \sim symbol can be done relatively quickly, by trial and error. In the example at hand, a value of 7.9ex seems to be about right.

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tabular}{@{}ccc@{}}
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture}
&
\raise7.9ex\hbox{$\sim$} % determine visually the amount by which argument is to be raised
&
\begin{tikzpicture}[grow=right,sloped]
\tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

\node[rectangle,draw]{}
  child{node[circle,draw]{}
  child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
  }
  child{node[circle,draw]{}
  child{node{$z$} edge from parent node[below]{$1-p$}} 
  child{node{$x$}edge from parent node[above]{$p$}}
  edge from parent node[above]{$t=s$}  
  };
\end{tikzpicture} \\
\end{tabular}
\end{document}
1
  • It works like this, Mico! Thanks a lot! I guess I was tangled/close minded with the "automatically position or center" You guys are amazing! The responses are so fast so helpful!!!
    – Jino
    Sep 8, 2013 at 10:57
1

I would a slightly different approach from the other answers. I would set the node position and then use this for the baseline. I'd also put the \sim inside its own tikz environment centered on the same node position:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tabular}[b]{@{}ccc@{}}
  \begin{tikzpicture}[baseline={(0,0)},grow=right,sloped]
    \tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
    \tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

    \node[rectangle,draw]  at (0,0) {}
      child
        {
          node[circle,draw]{}
          child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
        }
      child
        {
          node[circle,draw]{}
          child{ node {$z$} edge from parent node[below] {$1-p$} } 
          child{ node {$x$} edge from parent node[above] {$p$}   }
          edge from parent node[above]{$t=s$}  
        };
  \end{tikzpicture}
&
\tikz[baseline={(0,0)}]{\node at (0,0){$\sim$}}
&
  \begin{tikzpicture}[baseline={(0,0)},grow=right,sloped]
    \tikzstyle{level 1}=[level distance=2cm, sibling distance=2cm]
    \tikzstyle{level 2}=[level distance=2cm, sibling distance=1.5cm]

    \node[rectangle,draw] at (0,0) {}
      child{node[circle,draw]{}
      child{node{$P_{T|s}$}} edge from parent node[below]{$t\neq s$}
      }
      child{node[circle,draw]{}
      child{node{$z$} edge from parent node[below]{$1-p$}} 
      child{node{$x$}edge from parent node[above]{$p$}}
      edge from parent node[above]{$t=s$}  
      };
  \end{tikzpicture}  \\
\end{tabular}

\end{document}

enter image description here This gets everything aligned on the square but eliminates any need for guessing how far to raise one or the other of the pictures or \sim.

You must log in to answer this question.

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