关于浮动体的位置确定,强烈建议阅读Frank Mittelback's answer。
其中关键的参数为:
\floatsep
(default 12pt plus 2pt minus 2pt) the separation between floats in top or bottom areas\textfloatsep
(default 20pt plus 2pt minus 4pt) the separation between top or bottom area and the text area\intextsep
(default 12pt plus 2pt minus 2pt) inline floats (that have been placed "here") the separation to the surrounding text
在编组内修改\setlength{\textfloatsep}{100pt}
确实无效,我想这也许和浮动体位置确定的时机有关(我也不是很懂啊哈哈哈,再开个问题吧)。可见下例:
参考这个链接,要想让该设置只对某一浮动体环境生效可以使用etoolbox
的hook
:
% 在导言区加上这个,同时不要将`algorithm`放在编组(group)内即可...
\usepackage{etoolbox}
\BeforeBeginEnvironment{algorithm}{\setlength{\intextsep}{0pt}\setlength{\floatsep}{0pt}\setlength{\textfloatsep}{0pt}}
我简单修改了以下MWE如下:
\documentclass{article}
% https://ask.latexstudio.net/ask/question/17506.html
\usepackage[
letterpaper,
top=2cm,bottom=2cm,
left=3cm,right=3cm,
marginparwidth=1.75cm
]{geometry}
\usepackage{algorithm2e}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{etoolbox}
\BeforeBeginEnvironment{algorithm}{\setlength{\intextsep}{0pt}\setlength{\floatsep}{0pt}\setlength{\textfloatsep}{0pt}}
\begin{document}
\lipsum[1-3]
\begin{figure}[h]
\centering
\includegraphics[width=0.25\linewidth]{example-image-a}
\caption{\label{fig:frog}This frog was uploaded via the file-tree menu.}
\end{figure}
\lipsum[1-3]
% 当浮动体浮动到顶端时,下面的调整似乎无效!确实!
% \setlength{\floatsep}{300pt}
% \setlength{\textfloatsep}{100pt}
% \setlength{\intextsep}{300pt}
\RestyleAlgo{ruled}
\begin{algorithm}[htbp]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\lipsum[1-3]
\end{document}
没有