0

When I add or remove some paragraph in other section of the overleaf project, the algorithm2e algorithm body collapses intermittently as shown in the attached image below. enter image description here

Following is a sample code to generate the error:

\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}

I am using 2 column page Please help.

akhil
  • 11
  • 1
    Welcome. // Please provide complete AND minimal code, which we can copy, compile and still see your problem. Starts with \documentclass, ends with \end{document}, provides all packages needed, removes everything which isn't relevant. – MS-SPO Jul 26 '22 at 11:28
  • 1
    @MS-SPO I have update the code section in my question, to give a sample code that produces the concerned error – akhil Jul 26 '22 at 11:46
  • Thanks. Four question so we understand better: 1) How shall the intended result look like? Can you provide some visualization with your question? 2) What's the reason you placed the multicol-environment inside the algorithm environment? 3) Shall the document be a 1-col or 2-col layout? 4) Will your algorithm be as long as the number of function copies suggest, which then are probably just dummies? – MS-SPO Jul 26 '22 at 13:17
  • @MS-SPO 1) The intended result would be a single algorithm spread over 2 columns in a PDF document page, such that the algorithm continues from 1st column to 2nd column after the \columnbreak asks it to do so. 3) The document has a 2-col layout. – akhil Jul 26 '22 at 13:52
  • @MS-SPO 4) My algorithm is long to an extent that it covers one page upto 90-95%. When the algorithm was appearing (as it appears sometimes on deleting or inserting text paragraph in another document page), there was no text line present in the algorithm's page. My original algorithm has just 3-4 functions. The dummies were just to replicate the bug, I have faced this bug even when the length of algorithm was just half-page (such that there were text paragraphs beneath the algo). 2) To make the algo continue to 2nd column when it encounters \columnbreak command – akhil Jul 26 '22 at 13:53
  • \columnbreak works inside multicols. I'm almost sure, you can't use it inside an algorithm2e environment. See https://tex.stackexchange.com/search?q=algorithm2e+columnbreak for similar problems, yours included. // Workarounds: 1) Try algoritmicx . 2) Stay with algorithm2e AND split your long algo intentionally into smaller parts. E.g. one could be a series of say 4 calls, and the next 4 algos detail that, leaving your with 5 algos in total, from coarse to fine. // Some people here tried e.g. https://tex.stackexchange.com/questions/18949/algorithm2e-split-over-several-pages . – MS-SPO Jul 26 '22 at 14:29
  • @MS-SPO Thanks for your answer. Regarding your comment that \columnbreak works inside multicols. I'm almost sure, you can't use it inside an algorithm2e environment., I have doubt that as you can see in the sample code, that \columnbreak is used within multicols. Also, I have used it in many other algorithms where this problem of disappearing didn't happen. So \columnbreak is not the real cause of disappearance.

    Now, even if I remove \columnbreak I face another issue in which the end tag of a procedure flows to 2nd column instead of being in 1st col.

    – akhil Jul 26 '22 at 15:32
  • This is the reason I had to bring in \columnbreak command, such that the end tag of last procedure in 1st column does not move to 2nd column. You can remove \columnbreak and check. Any solution to this problem ? – akhil Jul 26 '22 at 15:34

2 Answers2

1

(1) Remove \SetAlgoLined so the algorithm reappears (in the next page)

(2) Redefine the bottom rule adding more space to avoid the collision with the last line.

Add before \begin{document}

\makeatletter
\def\@algocf@post@ruled{%
    \kern8pt%
    \hrule width\algocf@ruledwidth height\algoheightrule\relax}%
\makeatother

a

Simon Dispa
  • 39,141
  • Thanks for your effort. Can we make the algorithm appear without removing \SetAlgoLined command ? As removing this command removes the "end" markers of procedures, if-statements and while loops. – akhil Jul 26 '22 at 18:45
0

Here's a second approach for you. Please find changes I made after the screenshot.

Result

  1. Sometimes packages can collide, either by their design or by their order. So I disabled all packages not needed here. // In reverse, when you copy my approach and start adding packages one-by-one, you will fail early and can avoid problems when they happen.
  2. I added package lipsum to create some blindtext to better see effects on page layout.
  3. I introduced shortcuts to your pseudocode via the \newcommand\demo. It takes 1 parameter and adds it to "p". Looks like the non-systematic sequences of pseudocode, \BlankLine-s, returns etc. in your initial code may be part of the confusion. All those % are needed to avoid unwanted spaces when calling the macro.
  4. Next I use multicols to create a 2-col page layout outside of environment algorithm*.
  5. Your pseudocode now is replaced by \demo{}, which gives some kind of standardization. If I do it without multicols inside, it will not be able to display all pseudocode in 1-col. Else with multicols inside it seems to typeset fine, as long as it finds room on that page. Remember the warning in the manual: it won't split pseudocode accross pages.

Code:

\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
% &lt;------------------------------------------
\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}

MS-SPO
  • 11,519