考虑以下 MWE - 假设它被称为test.tex
:
\batchmode %
\ifx\article\undefined %
\documentclass[12pt,journal,onecolumn,twoside,draftcls,letterpaper]{IEEEtran} %
\else %
\documentclass{article} %
\fi
\usepackage{xcolor} % \pagecolor
\pagecolor{yellow!15}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
\begin{document}
\lipsum[1-6] HERE1
\begin{figure}[p]
\includegraphics[width=0.9\textwidth,height=0.9\textheight]{demo.png}
\caption{Demo figure One}
\label{fig:one}
\end{figure}
\lipsum[7-9] HERE2
\begin{figure}[p]
\includegraphics[width=0.9\textwidth,height=0.9\textheight]{demo.png}
\caption{Demo figure Two}
\label{fig:two}
\end{figure}
\lipsum[9-13]
\errorstopmode % if in batchmode, this will allow print of just the number of output pages and such
\end{document}
您可以使用以下bash
一行程序进行编译:
for ix in "article" "ieeetran" ; do \
cmd="pdflatex -jobname test-$ix \"\def\\$ix{}\input{test.tex}\""; \
echo "$cmd" ; \
eval "$cmd" ; \
done
...或者直接:
pdflatex -jobname test-article "\def\article{}\input{test.tex}"
pdflatex -jobname test-ieeetran "\def\ieeetran{}\input{test.tex}"
这将生成两个 pdf - 内容从第 2 页到第 5 页如下所示(单击可查看完整尺寸):
所以,这就是我想要做的:如果第一个图(fig:one)首先出现在第页current_page
;然后安排fig:one(因为它是整页[p]
浮点数)出现在第页current_page+1
;并安排fig:two出现在第页current_page+3
——也就是说,在这两个浮点数之间有一页文本,所以它们都在偶数页或奇数页上。
现在,这里,这显然发生在了test-ieeetran.pdf
,因为的开始和结束\lipsum[7-9]
恰好在不同的页面上(如“HERE1”和“HERE2”所示)——所以算法在遇到浮动元素后自然会将它们安排到下一页。
但对于test-article.pdf
,这并没有发生,并且浮动一个接一个地排列;显然,这是因为中间的文本\lipsum[7-9]
完全适合同一页(2);因此算法将遇到两个都浮动在同一页上(因为当遇到它们时,它会将它们推迟到以后,并继续排版文本);将它们都安排到下一页(3);并且由于它们无法同时放在第 3 页上,它会将 fig:two 推送到下页(4)。我认为正因为如此(浮动在同一页上),当我在这种情况下尝试这些技巧时,它们都不起作用\afterpage{\clearpage}
。\FloatBarrier
因此,为了使问题保持一般性:有没有办法安排浮动元素稍后出现在特定页面上 - 无论当前文档类是否将它们视为在同一页面上?
答案1
好吧,最终我确实希望得到一个博学的答案 - 但就目前而言,我确实在某种程度上解决了 OP 中的具体案例;所以我想我会发布,以提醒人们这可能会变得多么复杂:)
。现在两个文档类都在 pg3 上显示 fig:one,在 pg5 上显示 fig:two,否则文本会在两者之间流动;特别是test-article
现在是按要求的,如下所示:
代码如下 - 我希望我可以解释它是如何工作的,但我自己也感到困惑。:)
这是一种欺骗\afterpage
行为;我主要通过暴力破解和查看打印输出来找到解决方案 - 这里是当前代码的两个类的打印输出的比较:
% % ieeetran | % article
%
% Completed box being shipped out [1] [] | Completed box being shipped out [1] []
% \floatpageset to 3 .. | \floatpageset to 3 ..
% Completed box being shipped out [2] [] | presecond: pg.2(3) 2
% in \@floatplacement: 3, vs. 3, vs. 3; !! | Completed box being shipped out [2] []
% 'tis 3=3 | in \@floatplacement: 3, vs. 3, vs. 3; !!
% Completed box being shipped out [3] [] | 'tis 3=3
% in \@floatplacement: 4, vs. 4, vs. 3; !! | Completed box being shipped out [3] []
% 'tis 4!= 3 | in \@floatplacement: 4, vs. 4, vs. 3; !!
% \floatpageset to 5 .. | 'tis 4!= 3
% presecond: pg.4(5) 2 | \floatpageset to 5 ..
% fig:two afterpage at pg.4 | fig:two afterpage at pg.4
% Completed box being shipped out [4] [] | Completed box being shipped out [4] []
% in \@floatplacement: 5, vs. 5, vs. 5; !! | in \@floatplacement: 5, vs. 5, vs. 5; !!
% 'tis 5=5 | 'tis 5=5
% Completed box being shipped out [5] [] | Completed box being shipped out [5] []
不过,好处是它允许(或多或少)用代码将图形“包裹”在原始位置,我喜欢这一点。无论如何,固定test.tex
代码是:
\batchmode %
\ifx\article\undefined %
\documentclass[12pt,journal,onecolumn,twoside,draftcls,letterpaper]{IEEEtran} %
\else %
\documentclass{article} %
\fi
\usepackage{xcolor} % \pagecolor
\pagecolor{yellow!15}
\usepackage{lipsum}
\usepackage[demo]{graphicx}
\input{pgfutil-common}
\usepackage{pgfkeys,pgfmath}
%\usepackage{trace}
\usepackage{afterpage}
%\usepackage[verbose]{placeins} % doesn't really print if unused
\tracingoutput=1
\tracingonline=1
\makeatletter
\let\orig@floatplacement\@floatplacement
\gdef\floatpage{0}
\def\setFloatpage#1{%
\xdef\floatpage{#1}%
\typeout{\string\floatpage set to #1 ..}%
}
% based on:
% % http://tex.stackexchange.com/q/124245/ensure-figures-are-placed-on-an-odd-page/#130000
\newcommand*{\floatsonpageonly}{%
%\clearpage
\xdef\floatsonpageonlyStartedAtPg{\thepage}%
\def\@floatplacement{%
% \thepage is == to \the\c@page
\typeout{in \string\@floatplacement: \thepage, vs. \the\c@page, vs. \floatpage; !!}% \the\c@totalnumber
\ifnum\the\c@page=\floatpage%
\typeout{'tis \the\c@page=\floatpage}%
\else%
\typeout{'tis \the\c@page != \floatpage}%
\fi%
\global\@topnum\c@topnumber
\global\@toproom \topfraction\@colht
\global\@botnum \c@bottomnumber
\global\@botroom \bottomfraction\@colht
%\global\@colnum \ifodd\c@page\c@totalnumber\else\z@\fi%<<<<<<<
\global\@colnum \ifnum\the\c@page=\floatpage%
%\typeout{...}% NO \typeouts here!
\c@totalnumber% % total floats per page allowed
\else%
\z@%
\fi%
\@fpmin \floatpagefraction\@colht}%
}
\newcommand*{\floatsnormal}{%
%\clearpage
% NOTE: just the original \let:
%\let\@floatplacement\orig@floatplacement
% ... seems not to be enough; apparently needs
% to be globalized, else the modified function
% will not stop - pushing all further floats to end!
\global\let\@floatplacement\orig@floatplacement
}
\makeatother
\begin{document}
\errorstopmode
\lipsum[1-6] HERE1
\pgfmathtruncatemacro{\tmpc}{\thepage+1}
\setFloatpage{\tmpc}
\floatsonpageonly
\afterpage{
\begin{figure}[p]
\includegraphics[width=0.9\textwidth,height=0.9\textheight]{demo.png}
\caption{Demo figure One}
\label{fig:one}
\end{figure}
\afterpage{
\pgfmathtruncatemacro{\tmpc}{\tmpc+2}
\setFloatpage{\tmpc}
}
\clearpage
}
\lipsum[7-9] HERE2
% \pgfmathtruncatemacro{\tmpc}{\tmpc+2} % no can do; still on same page here
% \setFloatpage{\tmpc} % -- likewise --
% now prevent processing this at first go, too; but only if on same page as when the op started (must wrap it in \def, because we need to call afterpage with that as argument conditionally)
\typeout{presecond: pg.\thepage (\floatpage) \floatsonpageonlyStartedAtPg}
\def\myFigTwo{%
\typeout{fig:two afterpage at pg.\thepage}
\begin{figure}[p]
\includegraphics[width=0.9\textwidth,height=0.9\textheight]{demo.png}
\caption{Demo figure Two}
\label{fig:two}
\end{figure}
\afterpage{\floatsnormal} % restore normal after done!
}
\ifnum\thepage=\floatsonpageonlyStartedAtPg %
\afterpage{\myFigTwo} %
\else %
\myFigTwo %
\fi
\lipsum[9-13]
\lipsum[14-15] % bit more
\errorstopmode % if in batchmode, this will allow print of just the number of output pages and such
\end{document}