目标
我的目标是根据顶部、中部和底部对齐小页面中的figure
s、 s 和文本table
最大的这样的minipage
。这使得它与底部的相关链接和参考资料有所不同(我相信),这些链接和参考资料详细介绍了基线周围的文字。我不知道哪个数字最大。
例如,请考虑下图:
请注意,A
和C
与底部对齐的C
C
第四张图像与最大的图像的中间对齐。
这个算法非常简单:
- 将所有
minipage
环境放入一个盒子中,并测量高度 minipage
然后使用每个设置\begin{minipage}[b][MAX HEIGHT][<alignment>]
,以便每个都“锚定”到底部,每个都有相同的高度(关键),然后可以指定每个内容的位置(t
,,c
或b
)。
问题
我遇到的问题是,当我尝试caption
这些对象时,编号会下降。例如,我得到以下内容:
读过盒子里面有没有一个开关可以关闭 LaTeX 计数器?,我以为我可以用\savecounters@
和来解决这个问题restorecounters@
。
问题
figure
我该如何修改代码,使s 和s的编号subfigure
符合预期?解决方案有望自然扩展到表格。
我非常乐意接受实现相同想法的替代方案;我曾尝试过adjustbox
例如,包,但到目前为止还没有运气。
数学家协会
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{graphicx}
\usepackage[showframe=true,textwidth=12cm]{geometry}
\usepackage{amsmath}
\usepackage{subcaption}
\usepackage{environ}
\newsavebox{\vabox}
\makeatletter
\NewEnviron{verticallyaligned}{%
% temporarily set \vamaxheight to nothing during the measurements
\let\vamaxheight\relax
% temporarily disable figure numbering
\savecounters@
% measure the height of the body
\begin{lrbox}{\vabox}
\BODY%
\end{lrbox}%
% turn figure numbering back on
\restorecounters@
% set the height of the minipage box
\newlength{\vamaxheight}
\setlength{\vamaxheight}{\ht\vabox}
% output the body, which now contains the new height :)
\noindent\makebox[\linewidth][c]{\mbox{}\hfill\BODY\hfill\mbox{}}%
}
\begin{document}
\begin{verticallyaligned}
\noindent
\begin{minipage}[b][\vamaxheight][b]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\captionof{figure}{}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][t]{.15\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\captionof{figure}{}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][t]{.20\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-c}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][c]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][t]{.20\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}
\end{minipage}%
\end{verticallyaligned}
\begin{figure}[!htb]
\begin{verticallyaligned}
\begin{subfigure}[b][\vamaxheight][t]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{}
\end{subfigure}%
\hfill
\begin{subfigure}[b][\vamaxheight][t]{.15\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption{}
\end{subfigure}%
\end{verticallyaligned}
\caption{}
\end{figure}
\begin{figure}[!htb]
\begin{verticallyaligned}
\begin{subfigure}[b][\vamaxheight][c]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{}
\end{subfigure}%
\hfill
\begin{subfigure}[b][\vamaxheight][c]{.15\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption{}
\end{subfigure}%
\end{verticallyaligned}
\caption{}
\end{figure}
\end{document}
参考
答案1
这里我介绍\pushfigure[alignment]{width}{content}{captioningof}
如何将图形推送到“堆栈”,然后\popfigures
将它们全部放在一行上。在所有情况下,应\captionof
为任何被“推送”的图形添加标题。底部对齐是默认设置。
\pushfigure
操作阶段保存所有参数,并测量的高度(加上任何深度)。content
它保存已推送的所有内容的最大高度+深度。
该\popfigures
阶段使用已保存\stackinset
的图形对齐方式,按照最大内容高度规则对图形执行对齐。这样可以实现图形的正确垂直对齐。指定的任何标题都应用于垂直对齐下方\rule
,这将使所有标题保持在同一高度,无论内容如何对齐。
在每次弹出数字的开头和之后都会插入一个\hfill
。所有参数(计数器/长度)都会在“弹出”结束时重置,以准备下一个“推送”序列。
对于这个 MWE,我将值降低\abovecaptionskip
到 6pt,我发现在使用子图时这在视觉上更令人愉悦。
编辑以演示tabular
和\parbox
内容。重新编辑以说明底部对齐tabular
和\parbox
框仍然可以具有深度,与底线上的降序字母相关。
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{graphicx}
\usepackage[showframe=true,textwidth=12cm]{geometry}
\usepackage{amsmath}
\usepackage{subcaption}
\usepackage{stackengine,ifthen}
\newcounter{figstack}
\newcounter{figindex}
\newlength\fight
\newcommand\pushfigure[4][b]{%
\stepcounter{figstack}%
\expandafter\def\csname %
figalign\romannumeral\value{figstack}\endcsname{#1}%
\expandafter\def\csname %
figwd\romannumeral\value{figstack}\endcsname{#2}%
\expandafter\def\csname %
figcontent\romannumeral\value{figstack}\endcsname{#3}%
\expandafter\def\csname %
figcap\romannumeral\value{figstack}\endcsname{#4}%
\setbox0=\hbox{%
\begin{minipage}{#2}#3\end{minipage}}%
\ifdim\dimexpr\ht0+\dp0\relax>\fight\global\setlength{\fight}{%
\dimexpr\ht0+\dp0\relax}\fi%
}
\newcommand\popfigures{%
\setcounter{figindex}{0}%
\hfill%
\whiledo{\value{figindex}<\value{figstack}}{%
\stepcounter{figindex}%
\def\tmp{\csname figwd\romannumeral\value{figindex}\endcsname}%
\begin{minipage}[t]{\tmp}%
\centering%
\stackinset{c}{}%
{\csname figalign\romannumeral\value{figindex}\endcsname}{}%
{\csname figcontent\romannumeral\value{figindex}\endcsname}%
{\rule{0pt}{\fight}}\par%
\csname figcap\romannumeral\value{figindex}\endcsname%
\end{minipage}%
\hfill%
}%
\setcounter{figstack}{0}%
\setlength{\fight}{0pt}%
\hfill%
}
\setlength\abovecaptionskip{6pt}
\begin{document}
\pushfigure{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image-a}}%
{\captionof{figure}{}}
\pushfigure[t]{.15\textwidth}
{\includegraphics[width=\textwidth]{example-image-b}}%
{\captionof{figure}{}}
\pushfigure[t]{.2\textwidth}
{\includegraphics[width=\textwidth]{example-image-c}}%
{}
\pushfigure[c]{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image}}%
{}
\pushfigure[c]{.2\textwidth}
{\includegraphics[width=\textwidth]{example-image}}%
{}
\noindent\popfigures
\begin{figure}[!htb]
\captionsetup{labelformat=parens}
\pushfigure[t]{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image-a}}
{\captionof{subfigure}{}}
\pushfigure{.15\textwidth}
% {\includegraphics[width=\textwidth]{example-image-b}}
{\begin{tabular}[b]{ccc}a&b&c\\dd&e&fff\\%
a&b&c\\x&yy& z\end{tabular}}
{\captionof{subfigure}{}}
\popfigures\par
\captionsetup{labelformat=default}
\caption{}
\end{figure}
\begin{figure}[!htb]
\captionsetup{labelformat=parens}
\pushfigure[c]{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image-a}}
{\captionof{subfigure}{}}
\pushfigure[c]{.15\textwidth}
% {\includegraphics[width=\textwidth]{example-image-b}}
{\parbox{\textwidth}{%
This is text and a bit more text and some more text}}
{\captionof{subfigure}{}}
\popfigures\par
\captionsetup{labelformat=default}
\caption{}
\end{figure}
\end{document}
表格版本
与图表版本直接类似,但\poptables
(子)标题位于图表上方。但请注意,如果表格标题是多行的,则标题将相对于同一行中的其他标题底部对齐。
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{graphicx}
\usepackage[showframe=true,textwidth=12cm]{geometry}
\usepackage{amsmath}
\usepackage{subcaption}
\usepackage{stackengine,ifthen}
\newcounter{tblstack}
\newcounter{tblindex}
\newlength\tblht
\newcommand\pushtable[4][b]{%
\stepcounter{tblstack}%
\expandafter\def\csname %
tblalign\romannumeral\value{tblstack}\endcsname{#1}%
\expandafter\def\csname %
tblwd\romannumeral\value{tblstack}\endcsname{#2}%
\expandafter\def\csname %
tblcontent\romannumeral\value{tblstack}\endcsname{#3}%
\expandafter\def\csname %
tblcap\romannumeral\value{tblstack}\endcsname{#4}%
\setbox0=\hbox{%
\begin{minipage}{#2}#3\end{minipage}}%
\ifdim\dimexpr\ht0+\dp0\relax>\tblht\global\setlength{\tblht}{%
\dimexpr\ht0+\dp0\relax}\fi%
}
\newcommand\poptables{%
\setcounter{tblindex}{0}%
\hfill%
\whiledo{\value{tblindex}<\value{tblstack}}{%
\stepcounter{tblindex}%
\def\tmp{\csname tblwd\romannumeral\value{tblindex}\endcsname}%
\begin{minipage}[b]{\tmp}%
\centering%
\csname tblcap\romannumeral\value{tblindex}\endcsname\par%
\stackinset{c}{}%
{\csname tblalign\romannumeral\value{tblindex}\endcsname}{}%
{\csname tblcontent\romannumeral\value{tblindex}\endcsname}%
{\rule{0pt}{\tblht}}%
\end{minipage}%
\hfill%
}%
\setcounter{tblstack}{0}%
\setlength{\tblht}{0pt}%
\hfill%
}
\setlength\abovecaptionskip{6pt}
\begin{document}
\pushtable{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image-a}}%
{\captionof{table}{}}
\pushtable[t]{.15\textwidth}
{\includegraphics[width=\textwidth]{example-image-b}}%
{\captionof{table}{}}
\pushtable[t]{.2\textwidth}
{\includegraphics[width=\textwidth]{example-image-c}}%
{}
\pushtable[c]{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image}}%
{}
\pushtable[c]{.2\textwidth}
{\includegraphics[width=\textwidth]{example-image}}%
{}
\noindent\poptables
\bigskip\begin{table}[!htb]
\captionsetup{labelformat=default}
\caption{}
\captionsetup{labelformat=parens}
\pushtable[t]{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image-a}}
{\captionof{subtable}{}}
\pushtable{.15\textwidth}
% {\includegraphics[width=\textwidth]{example-image-b}}
{\begin{tabular}[b]{ccc}a&b&c\\dd&e&fff\\%
a&b&c\\x&yy& z\end{tabular}}
{\captionof{subtable}{}}
\poptables\par
\end{table}
\begin{table}[!htb]
\captionsetup{labelformat=default}
\caption{}
\captionsetup{labelformat=parens}
\pushtable[c]{.1\textwidth}
{\includegraphics[width=\textwidth]{example-image-a}}
{\captionof{subtable}{}}
\pushtable[c]{.15\textwidth}
% {\includegraphics[width=\textwidth]{example-image-b}}
{\parbox{\textwidth}{%
This is text and a bit more text and some more text}}
{\captionof{subtable}{}}
\poptables\par
\end{table}
\end{document}
现在棘手的部分是,如果想要将表格和图形放在同一行。宏\popfigures
通过使用顶部对齐的迷你页面来实现垂直对齐。宏\poptables
通过使用底部对齐的迷你页面来实现垂直对齐。如果一行必须同时容纳顶部和底部字幕,则这两种方法都不起作用。
那个问题还没有解决。
答案2
subcaption 包有些问题。例如,您不能在子图内使用 \captionof{subfigure}(其他地方都可以)。
我通过永久地用 minipage 替换 subfigure 并从头开始创建自己的 \caption 来解决了以前解决方案中的问题。
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{graphicx}
\usepackage[showframe=true,textwidth=12cm]{geometry}
\usepackage{amsmath}
\usepackage{subcaption}
\usepackage{environ}
\newlength{\reservemaxheight}
\newcommand{\oldcaption}{}% reserve name
\makeatletter
\newcommand{\newcaption}[2][\@empty]{% same as caption
\refstepcounter{subfigure}%
\ifx#1\@empty\addcontentsline{lof}{subfigure}{\hbox to\@tempdima {\thesubfigure\hfil }{\ignorespaces #2}}%
\else\addcontentsline{lof}{subfigure}{\hbox to\@tempdima {\thesubfigure\hfil }{\ignorespaces #1}}%
\fi
\rule{0pt}{1.5\baselineskip}(\thesubfigure) #2}
\makeatother
\def\newsubfigure{\let\oldcaption=\caption
\let\caption=\newcaption
\minipage}%
\def\endnewsubfigure{\endminipage
\let\caption=\oldcaption}%
\AtBeginDocument{% can't replace it any earlier
\let\subfigure=\newsubfigure
\let\endsubfigure=\endnewsubfigure}
\newsavebox{\vabox}
\makeatletter
\NewEnviron{verticallyaligned}{%
% temporarily set \vamaxheight to nothing during the measurements
\let\vamaxheight\relax
% temporarily disable figure numbering
\savecounters@
% measure the height of the body
\begin{lrbox}{\vabox}
\BODY%
\end{lrbox}%
% turn figure numbering back on
\restorecounters@
% set the height of the minipage box
\let\vamaxheight\reservemaxheight
\setlength{\vamaxheight}{\ht\vabox}%
\addtolength{\vamaxheight}{-\dp\vabox}% change back to positive
% output the body, which now contains the new height :)
\noindent\makebox[\linewidth][c]{\mbox{}\hfill\BODY\hfill\mbox{}}%
}
\makeatother
\begin{document}
\begin{verticallyaligned}
\noindent
\begin{minipage}[b][\vamaxheight][b]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\captionof{figure}{}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][t]{.15\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\captionof{figure}{}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][t]{.20\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-c}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][c]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}
\end{minipage}%
\hfill
\begin{minipage}[b][\vamaxheight][t]{.20\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}
\end{minipage}%
\end{verticallyaligned}
\begin{figure}[!htb]
\begin{verticallyaligned}
\begin{subfigure}[b][\vamaxheight][t]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{test}
\end{subfigure}%
\hfill
\begin{subfigure}[b][\vamaxheight][t]{.15\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption[short caption]{long caption}
\end{subfigure}%
\end{verticallyaligned}
\caption{}
\end{figure}
\begin{figure}[!htb]
\begin{verticallyaligned}
\begin{subfigure}[b][\vamaxheight][c]{.10\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{}
\end{subfigure}%
\hfill
\begin{subfigure}[b][\vamaxheight][c]{.15\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption{}
\end{subfigure}%
\end{verticallyaligned}
\caption{}
\end{figure}
\listoffigures
\end{document}