![为什么 subfig 使用自定义宏呈现错误的标题编号?](https://linux22.com/image/384836/%E4%B8%BA%E4%BB%80%E4%B9%88%20subfig%20%E4%BD%BF%E7%94%A8%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AE%8F%E5%91%88%E7%8E%B0%E9%94%99%E8%AF%AF%E7%9A%84%E6%A0%87%E9%A2%98%E7%BC%96%E5%8F%B7%EF%BC%9F.png)
当子图被包装在自定义宏中时,子图会显示错误的标题编号。有趣的是,如果没有宏,但使用技术上相同的命令,标题编号是正确的。
使用此代码:
\documentclass[a4paper]{scrbook}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen,changepage}
\begin{document}
\newlength{\alength}
\newlength{\blength}
\newcommand{\widefloat}[1]{%
\setlength{\alength}{\textheight-\pagetotal}
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}
\ifdim\alength<\blength\clearpage\fi
\ifthispageodd{\noindent\makebox[\textwidth][l]{#1}}{\noindent\makebox[\textwidth][r]{#1}}
}
\begin{figure}
%\noindent\makebox[\textwidth][r]{
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}}
\caption{ Two foo figures. }%
\end{figure}
\end{document}
现在,如果我们交换这两行:
%\noindent\makebox[\textwidth][l]{
\widefloat{
到
\noindent\makebox[\textwidth][l]{
%\widefloat{
鉴于这两行在技术上应该以相同的方式解释,我无法弄清楚哪里出了问题。该widefloat
宏应该检测其中的内容是奇数页还是偶数页。然后它将内容刷新到左侧或右侧边距。
答案1
该代码旨在将“浮点数”排版为不是真正的浮点数,而是“只是在那里”。
如果您计划\widefloat
在figure
环境中使用,则完全不需要测量。这解决了子图标签的问题,因为它们不再排版两次。
\documentclass[a4paper]{scrbook}
\usepackage{subfig}
\usepackage{graphicx}
\begin{document}
\newcommand{\widefloat}[1]{%
\centering
\ifthispageodd
{\makebox[\textwidth][l]{#1}}%
{\makebox[\textwidth][r]{#1}}%
}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\caption{Two foo figures.}
\end{figure}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\caption{Two foo figures.}
\end{figure}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\caption{Two foo figures.}
\end{figure}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\caption{Two foo figures.}
\end{figure}
\end{document}
通过进行一些更改,如果标题适合一行,您还可以让(全局)标题左对齐或右对齐。
\documentclass[a4paper]{scrbook}
\usepackage{subfig}
\usepackage{graphicx}
\DeclareCaptionStyle{singleleft}[justification=raggedright]{}
\DeclareCaptionStyle{singleright}[justification=raggedleft]{}
\newcommand{\widecaption}{%
\ifthispageodd
{\captionsetup{style=singleleft}}
{\captionsetup{style=singleright}}
\caption
}
\begin{document}
\newcommand{\widefloat}[1]{%
\centering
\ifthispageodd
{\makebox[\textwidth][l]{#1}}%
{\makebox[\textwidth][r]{#1}}%
}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\widecaption{Two foo figures.}
\end{figure}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\widecaption{Two foo figures.}
\end{figure}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\widecaption{Two foo figures.}
\end{figure}
\begin{figure}
\widefloat{
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}
\widecaption{Two foo figures.}
\end{figure}
\end{document}
答案2
正如 @egreg 在他的评论中解释的那样,问题在于你实际上排版了所有内容两次。 仅排版一次,然后使用保存的框确实解决了您的问题:
\documentclass[a4paper]{scrbook}
\usepackage{subfig}
\usepackage{graphicx}
\begin{document}
\newlength{\alength}
\newlength{\blength}
\newsavebox\widefloatBox
\newcommand{\widefloat}[1]
{%
\setlength{\alength}{\dimexpr\textheight-\pagetotal\relax}%
\sbox\widefloatBox{\parbox{\linewidth}{#1}}%
\setlength{\blength}{\dimexpr\ht\widefloatBox+\dp\widefloatBox\relax}%
\ifdim\alength<\blength\clearpage\fi
\ifthispageodd
{\noindent\makebox[\textwidth][l]{\usebox\widefloatBox}}
{\noindent\makebox[\textwidth][r]{\usebox\widefloatBox}}%
}
\begin{figure}
\widefloat
{%
\subfloat[]{\includegraphics[scale=.5]{example-image-a}}
\subfloat[]{\includegraphics[scale=.5]{example-image-b}}
}%
\caption{Two foo figures.}%
\end{figure}
\end{document}