将算法环境与文本对齐

将算法环境与文本对齐

我使用这两个包:

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

\usepackage[ruled,linesnumbered]{algorithm2e}

我的算法边距与文本不一致;例如:

在此处输入图片描述

理想情况下,我希望包裹算法的垂直线与前后文本边距完全一样宽。

使用的代码:

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

笔记:如果我将类型从 [h] 更改为 [H],它会在页面左侧完美对齐,但在右侧会更加超出范围。

我尝试了 algorithm2e 中的以下命令:

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

但似乎没有什么效果。非常感谢任何帮助。

答案1

试试这个代码。(algorithm2e很好)

定义\title\author使用

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

\maketitle

A

如果你想看到利润率,请尝试

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

相关内容