Align algorithm environment with text

algorithmshorizontal alignment

I use these two packages:

\usepackage[left=3cm, right=2cm, top=2cm, bottom=2cm]{geometry}

\usepackage[ruled,linesnumbered]{algorithm2e}

My algorithm margin does not align with the text; for example:

enter image description here

Ideally, I would want the vertical lines wrapping the algorithm to be exactly as wide as the text margin before and after.

The code used:

\documentclass[12pt, conference]{article}
\usepackage[left=3cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage[bookmarks=true]{hyperref}
\usepackage[font=footnotesize,labelfont=bf]{caption}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{fancyhdr}
\usepackage{setspace}
\usepackage{commath}
\usepackage{mathtools, nccmath}
\usepackage{tocloft}


\begin{document}
\onehalfspacing





\title{
\vspace{-3cm}
     \begin{center}
    \begin{Huge}
        Big title \\
    \end{Huge}
    \vspace{0.8cm}
    \begin{Large}
    \author{Some name}
    \end{Large}
    \end{center}
    \date{}

}

\maketitle
\vspace{-0.8cm}
\abstract 
{abstract text
  }

\section{section}
\label{sect}
Some text
Some text

\paragraph{Some title}
Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text 

\bigskip


\begin{algorithm}[h]
%\setlength{\algomargin}{-2em}
\caption{Example Algorithm}\label{alg:alg_title}
\DontPrintSemicolon

\KwResult{$S$}
$S \gets \emptyset$\;

\For{$i=0, i < k$}{
  $x \gets 2$\;
  $x \gets 2$\;
  $x \gets 2$\;
  $S$.insert($x$)\;
 }
\end{algorithm}

\clearpage
\subsection{Examples}

\end{document}

Note: if i changed the type from [h] to [H], it aligns perfectly on the left of the page, but goes even more out of bounds on the right.

I tried the following commands from algorithm2e:

\leftskip
\rightskip
\DecMargin{length}
\IncMargin{length}
\algomargin

but nothing seems to work. Would appreciate any help.

Best Answer

Try this code. (algorithm2e is fine)

Define \title and \author and use

\begin{abstract}
    abstract text
\end{abstract}

after \maketitle

a

If you want to see the margins try

 \usepackage[left=3cm, right=2cm, top=2cm, bottom=2cm, showframe]{geometry}

.

\documentclass[12pt, conference]{article}
\usepackage[left=3cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage[bookmarks=true]{hyperref}
\usepackage[font=footnotesize,labelfont=bf]{caption}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{fancyhdr}
\usepackage{setspace}
\usepackage{commath}
\usepackage{mathtools, nccmath}
\usepackage{tocloft}

\title{\Huge    Big title} % <<<<<<<<<<<<<<<<<<<<<<<<
\author{\large Some name} % <<<<<<<<<<<<<<<<<<<<<<<<
\date{}

\begin{document}    

    \onehalfspacing
    
    \maketitle

    \begin{abstract}% <<<<<<<<<<<<<<<<<<<<<<<< changed
    abstract text
    \end{abstract}
    
    \section{section}
    \label{sect}
    Some text
    Some text
    
    \paragraph{Some title}
    Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text 
    
    \bigskip    
    
    \begin{algorithm}[h]
        %\setlength{\algomargin}{-2em}
        \caption{Example Algorithm}\label{alg:alg_title}
        \DontPrintSemicolon
        
        \KwResult{$S$}
        $S \gets \emptyset$\;
        
        \For{$i=0, i < k$}{
            $x \gets 2$\;
            $x \gets 2$\;
            $x \gets 2$\;
            $S$.insert($x$)\;
        }
    \end{algorithm}
    
    \clearpage
    \subsection{Examples}
    
\end{document}