我正在尝试创建并排的图表,但我必须承认我不知道该怎么做。以下内容基于我的理解。我正在使用tikz
制作贝叶斯图的包。
\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}
请建议我如何执行。
编辑:
正如问题评论中所建议的那样,我做了以下更改,但我不明白它是如何linewidth
工作的。如果我想让一行中的距离相等怎么办?
\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}
答案1
环境subfigure
与环境基本相同minipage
,它会创建一个指定宽度的框。例如,如果您执行\begin{subfigure}{4cm}
,框的宽度将为 4cm。
LaTeX 有包含文本块宽度的宏(见\textwidth、\linewidth 和 \hsize 之间的区别对于各种选项),使用某个分数而不是明确的长度可能更有意义\linewidth
,因为您或多或少知道总宽度是多少。
回到subfigure
s ,如果将它们放在 中,可能会更清楚会发生什么\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}
因此,您可以看到每个框的宽度subfigure
,并且图表位于其中的中心。每个框之间都有一些空间,因为代码中的换行符与空格相同,例如%
行末百分号 ( ) 有什么用?每当您将多个子图形放置在一起时,您需要确保subfigure
环境的总宽度加上它们之间的空间加起来小于线的宽度。
如果您想在子图之间添加更多空间,您可以例如\hfill
在它们之间使用,这会将内容推到任一侧,从而填满线条:
\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}
正如我在评论中所说,我并不完全清楚你到底想要什么,但也许是这样的:
\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}
答案2
參閱并排图片
\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}
答案3
您还可以使用subfloatrow
环境来执行此操作,来自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}