当我在 overleaf 项目的其他部分添加或删除某个段落时,algorithm2e
算法主体会间歇性地崩溃,如下面的附图所示。
以下是生成错误的示例代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{algo_issue}
\date{July 2022}
\usepackage{subcaption} %a
\usepackage[ruled,vlined,linesnumbered]{algorithm2e} %a
\usepackage{amsmath} % a
\usepackage{amsfonts} % a
\usepackage{amssymb} % a
\usepackage{amsthm} % a 2
\usepackage{multicol} % a
\usepackage{multirow} % a
% \SetKwInput{KwGlobal}{Global} % Set the Global variables
\SetKwProg{myproc}{procedure}{}{end}
\begin{document}
% \maketitle
\section{Introduction}
Hello, I am trying to create a bug.
It may take some effort
\section{Abc}
this is a sample line.
\section{b}
this is sample
\begin{algorithm*}[!htb] % replaced [t] temporarliy [!htb]
\DontPrintSemicolon
\caption{ this is my caption }
\label{algo:algo1}
\begin{multicols}{2}
% \fontsize{8pt}{6pt}\selectfont
\footnotesize
%\small
\SetAlgoLined
% 1st procedure BEGINS <---------------------------------------------------------------------------
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\columnbreak
\vspace*{5px}
\BlankLine
\myproc{$\mathtt{p2}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\BlankLine
\BlankLine
\myproc{$\mathtt{p1}${ ($a$, $b$, $c$)}}
{
hello
\smallskip
hello
}
\end{multicols}
\end{algorithm*}
\end{document}
我正在使用 2 列页面,请帮忙。
答案1
答案2
这是为您提供的第二种方法。请在屏幕截图后查看我所做的更改。
- 有时,软件包可能会发生冲突,无论是由于设计还是顺序。所以我在这里禁用了所有不需要的软件包。// 相反,当您复制我的方法并开始逐个添加软件包时,您将尽早失败,并且可以避免问题发生。
- 我添加了包
lipsum
来创建一些盲文,以便更好地看到页面布局上的效果。 - 我通过 向您的伪代码引入了快捷方式
\newcommand\demo
。它接受 1 个参数并将其添加到“p”。看起来\BlankLine
您初始代码中的伪代码、-s、返回等的非系统序列可能是造成混淆的一部分。所有这些%
都是调用宏时避免出现不必要的空格所必需的。 - 接下来我用
multicols
创建 2 列页面布局外部环境algorithm*
. - 您的伪代码现在被替换为
\demo{}
,这给出了某种标准化。如果我没有multicols
里面,它将无法在 1 列中显示所有伪代码。别的和multicols
里面只要它在该页面上找到空间,它似乎就可以很好地排版。请记住手册中的警告:它不会跨页面拆分伪代码。
代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
%\title{algo_issue}% not used here
%\date{July 2022}% not used here
%\usepackage{subcaption} %a % not used here
\usepackage[ruled,vlined,linesnumbered]{algorithm2e} %a
%\usepackage{amsmath} % a % not used here
%\usepackage{amsfonts} % a% not used here
%\usepackage{amssymb} % a% not used here
%\usepackage{amsthm} % a 2% not used here
\usepackage{multicol} % a
%\usepackage{multirow} % a % not used here
\usepackage{lipsum}% to creat some blindtext
\newcommand\demo[1]{% simplifying this demo content
\BlankLine%
%
\myproc{$\mathtt{p#1}${ ($a$, $b$, $c$)}} %
{%
hello%
\smallskip%
%
hello%
}%
}
% \SetKwInput{KwGlobal}{Global} % Set the Global variables
\SetKwProg{myproc}{procedure}{}{end}% simplifies algo-writing
\begin{document}
% \maketitle% not used here
% ~~~ 2-col layout for text ~~~~~~~~
\begin{multicols}{2}
\section{Introduction}
Hello, I am trying to create a bug.
It may take some effort.
\lipsum[1]
\section{Abc}
this is a sample line.
\lipsum[2]
\section{b}
\lipsum[3]
this is sample
% ~~~ a longer algo ~~~~~~~~~
\begin{algorithm*}%[!htb] % replaced [t] temporarliy [!htb]
\DontPrintSemicolon
\caption{ this is my caption }
\label{algo:algo1}
% \fontsize{8pt}{6pt}\selectfont
\footnotesize
%\small
\SetAlgoLined% changes the vertical line
% 1st procedure BEGINS
% <------------------------------------------
\begin{multicols}{2}% let's try multicol inside
\demo{1}
\demo{2}
\demo{3}
\demo{4}
\demo{5}
\demo{6}
\demo{7}
\demo{8}
\demo{9}
\demo{10}
\demo{11}
\demo{12}
\demo{13}
\demo{14}
%\columnbreak
%\vspace*{5px}
\demo{15}
\demo{16}
\demo{17}
\demo{18}
\demo{19}
\demo{20}
\demo{21}
\demo{22}
\end{multicols}% inside algorithm2e environment
\end{algorithm*}
% ~~~ and 3 paragraphs of text again ~~~~~~~
\lipsum[4-6]
\end{multicols}% for the page layout
\end{document}