
我總是將浮動環境放在首次引用的段落末尾。
但是當段落分為兩頁(從第一頁底部開始到第二頁頂部結束)時,我遇到了問題。浮動可以很容易地放置在第一頁的頂部,因為它是在第一頁上的段落的第一句中直接引用的,即使浮動也在此頁上。但是 Latex 將浮動放在第二頁上,導致第一頁半空(因為現在缺少浮動)和第二頁半空(因為浮動現在是該頁面上唯一的東西)。
有沒有比在段落之前手動移動來源中的浮動環境更簡單的方法(當上述條件不再成立時,例如插入附加文字時,會出現所有問題)?
MWE(第二頁有浮動)
\documentclass{article}
\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}
\begin{document}
\null
\vfill
\lipsum[1-3]
In the first sentence I reference the float~\ref{alg:Algo}. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph.
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine
First Line\;
First Line\;
First Line\;
First Line\;
First Line\;
\Return{result}\;
\end{algorithm}
\end{document}
MWE(首頁有浮動)
\documentclass{article}
\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}
\begin{document}
\null
\vfill
\lipsum[1-3]
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine
First Line\;
First Line\;
First Line\;
First Line\;
First Line\;
\Return{result}\;
\end{algorithm}
In the first sentence I reference the float~\ref{alg:Algo}. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph. After the first sentence there are many more sentences and the float ist put after the whole paragraph.
\end{document}
答案1
若要重複使用巨集名稱,請第二次使用 \renewcommand。
\documentclass{article}
\usepackage{algorithm2e}
\usepackage[nopar]{lipsum}
\begin{document}
\null\vfill
\lipsum[1-3]
\newcommand{\InsertAlgorithmHere}{%
\begin{algorithm}[tbp]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\LinesNumbered
\caption{Caption}
\label{alg:Algo}
\Input{-}
\Output{-}
\BlankLine
First Line\;
First Line\;
First Line\;
First Line\;
First Line\;
\Return{result}\;
\end{algorithm}}
In the first sentence I reference the float~\ref{alg:Algo}.\InsertAlgorithmHere
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
After the first sentence there are many more sentences and the float ist put after the whole paragraph.
\end{document}