3

I'm trying to display a tikzpicture and table side by side. I'm struggling to align them though, much like Display of Table and Figure row alignment the alignment is out. I tried to adapt the linked answer but it's not working for tikzpicture (I'm actually using \input{} to read the image from a .tikz file). Ideally I don't want to convert the tikz file to png. Can someone advise what I can do?

\documentclass{article}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}

\usetikzlibrary{arrows,shapes,positioning,shadows,trees,patterns}
\definecolor{blue}{HTML}{0081FF}
\definecolor{grey}{HTML}{B0B0B0}

\begin{document}

\begin{figure}[]
    \centering
    \subfloat[Oblivious Decision Tree]{
      \resizebox{0.5\textwidth}{!}{
        \begin{tikzpicture}[->,>=stealth',
            level/.style={sibling distance = 5cm/#1, level distance = 2cm},
            basic/.style={draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
            split/.style={basic, rounded corners=2pt, thin, align=center, fill=grey},
            leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=blue, text width=1cm}] 
        \node [split] {$x_1<0.75$}
            child{ node [split] {$x_2<1.25$} 
            child{ node [leaf] {$\omega_{00}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{01}$} edge from parent node[above right] {$yes$}}                          
            edge from parent node[above left] {$no$}}
            child{ node [split] {$x_2<1.25$}
            child{ node [leaf] {$\omega_{10}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{11}$} edge from parent node[above right] {$yes$}}
            edge from parent node[above right] {$yes$}
            };
        \end{tikzpicture}
      }
    }
    \subfloat[Oblivious Decision Tree]{
        \begin{tabular}{llll}
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}
    }
  \caption{}
  \label{fig:decision-tree}
\end{figure}

\end{document}

2 Answers 2

2

Like this?

enter image description here

Edit: Considering valuable comment of @ John Kormylo:

Align of the image and table at their bottoms cab be obtained by adding option [b] to the tablular. With this both have baseline at their bottom.

Off-topic: I took a liberty and suggest you to remove \resizebox and reduce size level distance and sibling distance in your decision tree. Also for "yes" and "no" I use \textit{...} instead of in-line math and reduce used font size. Better visible of text in tree nodes is obtained by reducing intensity of color. All those (off-topic) changes doesn't influence on the image position in figure but slightly improve their quality. If you not liked all those changes you still can use your original image's code and resizebox (not recommended) but table and image will still be aligned at bottom.

All changes in the image code are indicated by % <---:

\documentclass{article}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}

\usetikzlibrary{arrows,
                patterns,positioning,
                shadows,shapes,
                trees}
\definecolor{blue}{HTML}{0081FF}
\definecolor{grey}{HTML}{B0B0B0}

\begin{document}

\begin{figure}
    \centering
    \subfloat[Oblivious Decision Tree]{
        \begin{tikzpicture}[
            ->,>=stealth',
            level/.style={sibling distance = 3cm/#1, level distance = 1cm},  % <---
            basic/.style={draw, drop shadow, font=\sffamily\small,
                          rounded corners=2pt, thin}, % <---
            split/.style={basic,  fill=grey},% <---
             leaf/.style={basic, fill=blue}, % <---
              lbl/.style={font=\small, #1}   % <---
                            ]
        \node [split] {$x_1<0.75$}
            child{ node [split] {$x_2<1.25$}
                child{ node [leaf] {$\omega_{00}$} edge from parent node[lbl=left] {\textit{no}}}
                child{ node [leaf] {$\omega_{01}$} edge from parent node[lbl=right] {\textit{yes}}}
            edge from parent node[lbl=left] {\textit{no}}
                }
            child{ node [split] {$x_2<1.25$}
                child{ node [leaf] {$\omega_{10}$} edge from parent node[lbl=left] {\textit{no}}}  % <---
                child{ node [leaf] {$\omega_{11}$} edge from parent node[lbl=right] {\textit{yes}}} % <---
            edge from parent node[lbl=right] {\textit{yes}}  % <---
                };
        \end{tikzpicture}
    }\hfill
    \subfloat[Oblivious Decision Tree]{
        \begin{tabular}[b]{llll}
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}
    }
  \caption{}
  \label{fig:decision-tree}
\end{figure}

\end{document}
5
  • Awsome yes that's excellent! Jul 9, 2019 at 3:52
  • One question, if I "cut" \begin{tikzpicture} .. \end{tikzpicture} and paste into a .tikz file, then import using \input{../figures/decision-tree.tikz} the height of the image changes? Do you know how to manage that? Jul 9, 2019 at 4:01
  • 3
    The default for tikzpicture is to put the baseline at the bottom. All you need to do is put the baseline at the bottom of tabular. Jul 9, 2019 at 4:47
  • @JohnKormylo, thank you very much. I correct code accordingly and also add some more explanation to answer.
    – Zarko
    Jul 9, 2019 at 6:40
  • 1
    @DavidWaterworth, all my off-topic changes doesn't changing anything in your intention of use of image code. Aligning is determined by option [b] added to tabular environment.
    – Zarko
    Jul 9, 2019 at 6:50
3

adjustbox comes with the option valign. I chose valign=b because I do not know what you are after, but you can change this of course.

\documentclass{article}
\usepackage{float}
\usepackage{subfig}
\usepackage{adjustbox}
\usepackage{tikz}

\usetikzlibrary{arrows,shapes,positioning,shadows,trees,patterns}
\definecolor{blue}{HTML}{0081FF}
\definecolor{grey}{HTML}{B0B0B0}

\begin{document}

\begin{figure}[]
    \centering
    \subfloat[Oblivious Decision Tree]{
      \adjustbox{max width=0.49\textwidth,valign=b}{
        \begin{tikzpicture}[->,>=stealth',
            level/.style={sibling distance = 5cm/#1, level distance = 2cm},
            basic/.style={draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
            split/.style={basic, rounded corners=2pt, thin, align=center, fill=grey},
            leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=blue, text width=1cm}] 
        \node [split] {$x_1<0.75$}
            child{ node [split] {$x_2<1.25$} 
            child{ node [leaf] {$\omega_{00}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{01}$} edge from parent node[above right] {$yes$}}                          
            edge from parent node[above left] {$no$}}
            child{ node [split] {$x_2<1.25$}
            child{ node [leaf] {$\omega_{10}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{11}$} edge from parent node[above right] {$yes$}}
            edge from parent node[above right] {$yes$}
            };
        \end{tikzpicture}
      }
    }~\subfloat[Oblivious Decision Tree]{
        \adjustbox{valign=b}{\begin{tabular}{llll}
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}}
    }
  \caption{}
  \label{fig:decision-tree}
\end{figure}

\end{document}

enter image description here

The advantage of this is that you can load and align the same tikzpicture in different ways without modifying it.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{mypic.tex}
\begin{tikzpicture}[->,>=stealth',
            level/.style={sibling distance = 5cm/#1, level distance = 2cm},
            basic/.style={draw, text width=2cm, drop shadow, font=\sffamily, rectangle},
            split/.style={basic, rounded corners=2pt, thin, align=center, fill=grey},
            leaf/.style={basic, rounded corners=6pt, thin,align=center, fill=blue, text width=1cm}] 
        \node [split] {$x_1<0.75$}
            child{ node [split] {$x_2<1.25$} 
            child{ node [leaf] {$\omega_{00}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{01}$} edge from parent node[above right] {$yes$}}                          
            edge from parent node[above left] {$no$}}
            child{ node [split] {$x_2<1.25$}
            child{ node [leaf] {$\omega_{10}$} edge from parent node[above left] {$no$}}
            child{ node [leaf] {$\omega_{11}$} edge from parent node[above right] {$yes$}}
            edge from parent node[above right] {$yes$}
            };
\end{tikzpicture}
\end{filecontents*}
\usepackage{float}
\usepackage{subfig}
\usepackage{adjustbox}
\usepackage{tikz}

\usetikzlibrary{arrows,shapes,positioning,shadows,trees,patterns}
\definecolor{blue}{HTML}{0081FF}
\definecolor{grey}{HTML}{B0B0B0}

\begin{document}

\begin{figure}[]
    \centering
    \subfloat[Oblivious Decision Tree]{
      \adjustbox{max width=0.49\textwidth,valign=b}{\input{mypic.tex}}
    }~\subfloat[Oblivious Decision Tree]{
        \adjustbox{valign=b}{\begin{tabular}{llll}
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}}
    }
  \caption{}
  \label{fig:decision-tree}
\end{figure}

\end{document}

The above was under the assumption that you want to have the freedom to choose the alignment. If you are sure that you want to align at the bottom, you can do as John Kormylo pointed out, only align the tabular. This variation is on something else, namely that you can produce the tree much more conveniently with forest. It allows you to do everything automatically.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{myforest.tex}
\begin{forest}
for tree={l sep=2em,draw,blur shadow,font=\sffamily, rectangle,thin,align=center, 
    where n children=0{rounded corners=6pt,fill=myblue}{rounded corners=2pt,
    fill=mygrey},
    where n=1{edge label={node[above left,pos=0.9,font=\sffamily\small]{yes}}}{%
    edge label={node[above right,pos=0.9,font=\sffamily\small]{no}}},
    edge={-stealth}}
[$x_1<0.75$
 [$x_2<1.25$
  [$\omega_{00}$]
  [$\omega_{01}$]
 ]
 [$x_2<1.25$
  [$\omega_{00}$]
  [$\omega_{01}$]
 ]
] 
\end{forest}
\end{filecontents*}
\usepackage{float}
\usepackage{subfig}
\usepackage{forest}
\usetikzlibrary{shadows.blur}
\definecolor{myblue}{HTML}{0081FF}
\definecolor{mygrey}{HTML}{B0B0B0}
\begin{document}
\begin{figure}[]
    \centering
    \subfloat[Oblivious Decision Tree]{\input{myforest.tex}%
    }\qquad\subfloat[Oblivious Decision Tree]{\begin{tabular}[b]{llll} % John Kormylo
            \hline
            \(x_1<0.75\) & \(x_2<1.25\) & q & w \\
            \hline
            false & false & 00b & \( w_{00} \) \\
            false & true & 01b & \( w_{01} \) \\
            true & false & 10b & \( w_{10} \) \\
            true & true & 11b & \( w_{11} \) \\
            \hline
        \end{tabular}}
  \caption{}
  \label{fig:decision-tree}
\end{figure}
\end{document}

enter image description here

2
  • Thanks very much, both answers do what I want Jul 9, 2019 at 3:54
  • 1
    Thanks, yes I realised later that they are slightly different and yours is closer to what I need. The valign seems to work better since it's outside the file. Jul 9, 2019 at 5:52

You must log in to answer this question.

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