使用{algorithm2e}宏包该怎么既调整浮动体与上下文本间距又不影响其他浮动体(如图片、表格)的效果?

发布于 2024-11-12 21:40:42

main.tex
使用{algorithm2e}宏包实现了也给算法伪代码,但是我发现伪代码浮动体与上下文本间距过大,我该怎么调整才能既缩小间距又不影响其他浮动体(如图片、表格)的效果?尤其是当浮动体浮动到下一页顶端时,局部调整intextsep、textfloatsep和floatsep似乎也失效了。
希望不要用H,用H的话会过紧,而且依然不能自由调整。
附件是最小实现。
image.png

查看更多

关注者
0
被浏览
501
2 个回答
双木林
双木林 2024-12-17
这家伙很懒,什么也没写!

请问您解决了嘛

Sagittarius Rover
Sagittarius Rover 2025-01-13
我要成为Typst糕手/(ㄒoㄒ)/~~

关于浮动体的位置确定,强烈建议阅读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}确实无效,我想这也许和浮动体位置确定的时机有关(我也不是很懂啊哈哈哈,再开个问题吧)。可见下例:

image.png

参考这个链接,要想让该设置只对某一浮动体环境生效可以使用etoolboxhook:

% 在导言区加上这个,同时不要将`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}

image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览