我正在尝试将 4 个不同大小的子图对齐。这里的盒子-钾.pdf 文件包含中间带有标记的框。结果是框 1 和框 3 在左边距对齐,而不是居中(但标题居中)。
texlive-2012,为 Fedora 分发。
\documentclass{memoir}
\usepackage{pgf}
\usepackage{subfig}
\begin{document}
\begin{figure}[htbp]
\setbox1=\hbox{\pgfimage{box-1}}
\setbox2=\hbox{\pgfimage{box-2}}
\setbox3=\hbox{\pgfimage{box-3}}
\setbox4=\hbox{\pgfimage{box-4}}
\centering
\subfloat[Box 1]{
\begin{minipage}[c]{1.0\wd3}
\begin{center}
\copy1
\end{center}
\end{minipage}
}
\hspace{4em}
\subfloat[Box 2]{
\begin{minipage}[c]{1.0\wd4}
\begin{center}
\copy2
\end{center}
\end{minipage}
}
\\[2ex]
\subfloat[Box 3]{
\begin{minipage}[c][1.0\ht4]{1.0\wd3}
\begin{center}
\copy3
\end{center}
\end{minipage}
}
\hspace{4em}
\subfloat[Box 4]{
\begin{minipage}[c]{1.0\wd4}
\begin{center}
\copy4
\end{center}
\end{minipage}
}
\caption{Some boxen}
\end{figure}
\end{document}
答案1
您应该计算宽度和高度,以便捕捉最大和最小宽度:
\documentclass{memoir}
\usepackage{graphicx}
\usepackage[caption=false]{subfig}
\newlength{\alignheight}
\newlength{\alignwidth}
\newcommand{\fakeheight}[3]{%
\makebox[#1][c]{\rule[-.5\dimexpr#2\relax]{0pt}{#2}\raisebox{-.5\height}{#3}}%
}
\begin{document}
\begin{figure}[htbp]
\sbox0{\includegraphics[width=3cm,height=2.5cm]{example-image-a}}
\setlength\alignwidth{\wd0}
\setlength\alignheight{\ht0}
\sbox2{\includegraphics[width=4cm,height=3cm]{example-image-b}}
\ifdim\wd2>\alignwidth \setlength\alignwidth{\wd2}\fi
\ifdim\ht2>\alignheight \setlength\alignheight{\ht2}\fi
\sbox4{\includegraphics[width=5cm,height=2cm]{example-image-c}}
\ifdim\ht4>\alignwidth \setlength\alignwidth{\wd4}\fi
\ifdim\ht2>\alignheight \setlength\alignheight{\ht4}\fi
\sbox6{\includegraphics[width=3.5cm,height=3.5cm]{example-image}}
\ifdim\ht6>\alignwidth \setlength\alignwidth{\wd6}\fi
\ifdim\ht2>\alignheight \setlength\alignheight{\ht6}\fi
\centering
\subfloat[Box 1]{\fakeheight{\alignwidth}{\alignheight}{\usebox0}}\hspace{4em}%
\subfloat[Box 2]{\fakeheight{\alignwidth}{\alignheight}{\usebox2}}\\[2ex]
\subfloat[Box 3]{\fakeheight{\alignwidth}{\alignheight}{\usebox4}}\hspace{4em}%
\subfloat[Box 4]{\fakeheight{\alignwidth}{\alignheight}{\usebox6}}
\caption{Some boxen}
\end{figure}
\end{document}
相同的 PGF 包已弃用该命令\pdfimage
,而使用\includegraphics
。当然,这里的最大宽度和高度是已知的,但我只是使用明确的尺寸来模拟一个例子。
答案2
如果每个小页面都像这样,列内奇怪的错位就会消失
\begin{minipage}[c]{1.0\wd2}
\centering
\makebox{\copy7}
\end{minipage}
使用 minipage 的 [c] 是垂直对齐并排图形的好方法。
答案3
根据 egreg 的回答,利用memoir
“开箱即用”的子浮点功能,并使etoolbox
答案更加自动化:
\documentclass{memoir}
\usepackage{pgf,etoolbox}
\newsubfloat{figure}
\newcommand{\fourimagesbox}[3]{\begin{minipage}[c][#2]{#3}
\centering
\makebox{\copy#1}
\end{minipage}
}
\begin{document}
\begin{figure}[htbp]
\setbox1=\hbox{\includegraphics[width=.3\linewidth]{example-image-a}}
\setbox2=\hbox{\includegraphics[width=.35\linewidth]{example-image-b}}
\setbox3=\hbox{\includegraphics[width=.45\linewidth]{example-image-c}}
\setbox4=\hbox{\includegraphics[width=.25\linewidth]{example-image}}
\newlength{\himgskip}\setlength{\himgskip}{0pt}
\newlength{\vimgskip}\setlength{\vimgskip}{0pt}
\newlength{\subcapskip}\setlength{\subcapskip}{0pt}
\newlength{\lwidth}\deflength{\lwidth}{\ifdimgreater{\wd1}{\wd3}{\wd1}{\wd3}}
\newlength{\rwidth}\deflength{\rwidth}{\ifdimgreater{\wd2}{\wd4}{\wd2}{\wd4}}
\newlength{\theight}\deflength{\theight}{\ifdimgreater{\ht1}{\ht2}{\ht1}{\ht2}+\subcapskip}
\newlength{\bheight}\deflength{\bheight}{\ifdimgreater{\ht3}{\ht4}{\ht3}{\ht4}+\subcapskip}
\centering
\subbottom[Box 1]{\fourimagesbox{1}{\theight}{\lwidth}}\hspace{\himgskip}
\subbottom[Box 2]{\fourimagesbox{2}{\theight}{\rwidth}}\vspace{\vimgskip}
\subbottom[Box 3]{\fourimagesbox{3}{\bheight}{\lwidth}}\hspace{\himgskip}
\subbottom[Box 4]{\fourimagesbox{4}{\bheight}{\rwidth}}
\caption{Some boxen}
\end{figure}
\end{document}
这样,图像和标题都对齐,并且创建了一些长度以供用户调整。