3

I am trying to create graphs side by side in a row, but I must accept that I have no idea how to do it. Following is based on my understanding. I am using tikz package for making Bayesian graphs.

\documentclass[11pt]{report}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{caption,subcaption}% http://ctan.org/pkg/{caption,subcaption}
\usetikzlibrary{positioning}
\usetikzlibrary{bayesnet}
\begin{document}

\begin{figure}
\begin{subfigure}
    \centering
    \tikz{ %
        \node[latent]                       (phi)   {$\phi$};
        \node[latent,   above = of phi]     (y)     {$\mathbf{y}$};
        \edge {phi} {y};
}
\end{subfigure}

\begin{subfigure}
    \centering
    \tikz{ %
        \node[latent]                       (a)     {$\alpha$};
        \node[latent,   above = of phi]     (b)     {$\beta$};
        \edge {a} {b};
}
\end{subfigure}
\begin{subfigure}
    \centering
    \tikz{ %
        \node[latent]                       (t)     {$\theta$};
        \node[latent,   above = of phi]     (g)     {$\gamma$};
        \edge {t} {g};
}
\end{subfigure}
\end{figure}

\end{document}

Please suggest me how it could be performed.

Edit:

As suggested in the comments to the question, I made the following changes, what I could not follow is how linewidth works. And what if I want equal distance between then in a row?

\documentclass[11pt]{report}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{caption,subcaption}% http://ctan.org/pkg/{caption,subcaption}
\usetikzlibrary{positioning}
\usetikzlibrary{bayesnet}
\begin{document}

\begin{figure}
\begin{subfigure}[b]{0.2\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (phi)   {$\phi$};
        \node[latent,   above = of phi]     (y)     {$\mathbf{y}$};
        \edge {phi} {y};
}
    \caption{\label{fig:fig1}}
\end{subfigure}
\begin{subfigure}[b]{0.4\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (a)     {$\alpha$};
        \node[latent,   above = of phi]     (b)     {$\beta$};
        \edge {a} {b};
}
    \caption{\label{fig:fig1}}
\end{subfigure}
\begin{subfigure}[b]{.5\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (t)     {$\theta$};
        \node[latent,   above = of phi]     (g)     {$\gamma$};
        \edge {t} {g};
}
    \caption{\label{fig:fig1}}
\end{subfigure}
\end{figure}

\end{document}

enter image description here

pkj
  • 511
  • 1
    You have to specify a width for the subfigure environment, e.g. \begin{subfigure}{0.3\linewidth}, and empty lines means a paragraph break also within a figure, so if you want two subfigures side by side you need to remove all empty lines between them. – Torbjørn T. Dec 20 '17 at 11:32
  • 1
    By the way, are you going to add a (sub)caption to each subgraph? If not, the subfigure environments are superfluous. – Torbjørn T. Dec 20 '17 at 11:34
  • @TorbjørnT. thanks for the reply, I have incorporated the suggestions, but what I could not get is how this line width works – pkj Dec 20 '17 at 11:55
  • \linewidth is the width of the text block, 0.3\linewidth is 30% of that. You can also use a specific length, e.g. 5cm. – Torbjørn T. Dec 20 '17 at 11:57
  • 1
    Exactly what layout are you after? Two in the first row, and one in the last? Or all three in the same row? If the latter, note that when you set the widths to 20% of the text width, 40% of the text width and 50% of the text width, the total width of the three subfigures is 110% of the text width, meaning that they don't fit, and the third one is moved to the next line. Set all three subfigure widths to 0.3\linewidth. – Torbjørn T. Dec 20 '17 at 12:45
  • 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 (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – samcarter_is_at_topanswers.xyz Apr 04 '18 at 12:04
  • probably, because all of them were good, but now I have accepted one – pkj Apr 04 '18 at 12:12

3 Answers3

1

Refer to Pictures side-by-side

\documentclass[11pt]{report}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{subfigure}
\usetikzlibrary{positioning}
\usetikzlibrary{bayesnet}

\begin{document}

\begin{figure}
    \centering
    \subfigure[Sub 1]{%
            \tikz{%
                \node[latent]                       (phi)   {$\phi$};
                \node[latent,   above = of phi]     (y)     {$\mathbf{y}$};
                \edge {phi} {y};
            }
        }
    \hfill
    \subfigure[Sub 2]{%
        \tikz{%
            \node[latent]                       (a)     {$\alpha$};
            \node[latent,   above = of phi]     (b)     {$\beta$};
            \edge {a} {b};
            }       
        }
    \hfill
    \subfigure[Sub 3]{%
        \tikz{%
            \node[latent]                       (t)     {$\theta$};
            \node[latent,   above = of phi]     (g)     {$\gamma$};
            \edge {t} {g};
            }
        }
    \hfill
    \caption{Figures}
\end{figure}

\end{document}

enter image description here

1

The subfigure environment is basically the same as a minipage environment, it creates a box of the specified width. For example if you do \begin{subfigure}{4cm}, the box will have a width of 4cm.

LaTeX has macros that contain the width of the text block though (see Difference between \textwidth, \linewidth and \hsize for the various options), and it might make more sense to use some fraction of \linewidth instead of an explicit length, as you know more or less what the total width will be.

Going back to subfigures, it might be clearer what happens if you place them in an \fbox, which will draw a visible frame around them:

three subfigures in row with \fbox

\begin{figure}
\centering
\setlength\fboxsep{0pt} % to remove inner spacing in \fbox
\fbox{% draws a box around the content
\begin{subfigure}[b]{0.3\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (phi)   {$\phi$};
        \node[latent,   above = of phi]     (y)     {$\mathbf{y}$};
        \edge {phi} {y};
}
    \caption{\label{fig:fig1}}
\end{subfigure}}
\fbox{% draws a box around the content
\begin{subfigure}[b]{0.3\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (a)     {$\alpha$};
        \node[latent,   above = of phi]     (b)     {$\beta$};
        \edge {a} {b};
}
    \caption{\label{fig:fig1}}
\end{subfigure}}
\fbox{% draws a box around the content
\begin{subfigure}[b]{.3\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (t)     {$\theta$};
        \node[latent,   above = of phi]     (g)     {$\gamma$};
        \edge {t} {g};
}
    \caption{\label{fig:fig1}}
\end{subfigure}}
\end{figure}

So you see the width of each subfigure, and that the diagram is centered inside it. There is some space between each box because a line feed in the code is the same as a space, see e.g. What is the use of percent signs (%) at the end of lines? Whenever you place multiple subfigures next to each other, you need to make sure that the total width of subfigure environments plus the space between them adds up to less than the width of the line.

If you want to add more space between the subfigures, you can for example use \hfill between them, which will push the content to either side so it fills up the line:

\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}

As I said in a comment, it wasn't entirely clear to me exactly what you're after, but perhaps this:

output of last code block

\documentclass[11pt]{report}
\usepackage{tikz} % loads graphicx
\usepackage{amsmath}
\usepackage{caption,subcaption}% http://ctan.org/pkg/{caption,subcaption}
\usetikzlibrary{positioning}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.3\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (phi)   {$\phi$};
        \node[latent,   above = of phi]     (y)     {$\mathbf{y}$};
        \edge {phi} {y};
}
    \caption{\label{fig:fig1}}
\end{subfigure}\hfill
\begin{subfigure}[b]{0.3\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (a)     {$\alpha$};
        \node[latent,   above = of phi]     (b)     {$\beta$};
        \edge {a} {b};
}
    \caption{\label{fig:fig1}}
\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}
    \centering
    \tikz{ %
        \node[latent]                       (t)     {$\theta$};
        \node[latent,   above = of phi]     (g)     {$\gamma$};
        \edge {t} {g};
}
    \caption{\label{fig:fig1}}
\end{subfigure}
\end{figure}
\end{document} 
Torbjørn T.
  • 206,688
  • Also, I think the fact that you have TikZ diagrams here is completely irrelevant, it would be the same problem if you had an image included with \includegraphics instead. – Torbjørn T. Dec 20 '17 at 13:57
  • yes... may be, I am just new to latex and faced this problem with tikz – pkj Dec 20 '17 at 14:16
1

You also can do that with a subfloatrow environment, from floatrow:

\documentclass[11pt]{report}
\usepackage[showframe]{geometry}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{caption, subcaption, floatrow}
\usetikzlibrary{positioning}
\usetikzlibrary{bayesnet}

\begin{document}

\begin{figure}
 \floatsetup{subfloatrowsep =none}
\ffigbox{%[\FBwidth]
\begin{subfloatrow}
    \ffigbox[0.25\textwidth]{\caption{Sub 1}}{%
           \tikz{%
                \node[latent] (phi) {$\phi$};
                \node[latent, above = of phi] (y) {$\mathbf{y}$};
                \edge {phi} {y};
            }
        }
    \ffigbox[0.25\textwidth]{\caption{Sub 2}}{%
       \tikz{%
            \node[latent] (a) {$\alpha$};
            \node[latent, above = of phi] (b) {$\beta$};
            \edge {a} {b};
            }
        }
    \ffigbox[0.25\textwidth]{\caption{Sub 3}}{%
        \tikz{%
            \node[latent] (t) {$\theta$};
            \node[latent, above = of phi] (g) {$\gamma$};
            \edge {t} {g};
            }
        }
\end{subfloatrow}}%
{\caption{Figures}}
\end{figure}

\end{document} 

enter image description here

Bernard
  • 271,350