如何将 algorithm2e 与外面的文本对齐?

如何将 algorithm2e 与外面的文本对齐?

我无法将算法与算法之外的文本对齐。以下是示例:

\documentclass[11pt]{article}
\pagestyle{empty}

\oddsidemargin -0.25in
\textwidth 7.0in         
\topmargin 0.0in
\headheight 0.0in
\headsep 0.0in
\topskip 0.0in
\footskip 0.4in
\textheight 8.8in         

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{graphicx}  %for including .jpeg files
\usepackage[algoruled,vlined,linesnumbered]{algorithm2e}
\usepackage{verbatim}
\usepackage{enumitem}
\usepackage{float}
\usepackage{amstext} % for \text macro
\usepackage{array}   % for \newcolumntype macro

\begin{document}
\begin{enumerate}
  \item 
    \begin{enumerate}
      \item Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm
        \begin{figure}[h]
          \centering
          \begin{minipage}[h]{0.7\linewidth}
            \begin{algorithm}[H]
              \caption{\textsc{Some Algo}($I$)}
              \SetKwInOut{Input}{input}
              \SetKwInOut{Output}{output}
              \SetCustomAlgoRuledWidth{1cm}
              \SetAlgoLined
              \DontPrintSemicolon
              \SetArgSty{textnormal}

              \Input{Some input description.}
              \Output{Some output description.} 

              \For{$i \leftarrow 1$ to $n$} {
                Do something \;
              }
            \end{algorithm}
          \end{minipage}
        \end{figure}

        Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm
        \begin{figure}[h]
          \centering
          \begin{minipage}[h]{0.9\linewidth}
            \begin{algorithm}[H]
              \caption{\textsc{Some Algo}($I$)}
              \SetKwInOut{Input}{input}
              \SetKwInOut{Output}{output}
              \SetCustomAlgoRuledWidth{1cm}
              \SetAlgoLined
              \DontPrintSemicolon
              \SetArgSty{textnormal}

              \Input{Some input description.}
              \Output{Some output description.} 

              \For{$i \leftarrow 1$ to $n$} {
                Do something \;
              }
            \end{algorithm}
          \end{minipage}
        \end{figure}
    \end{enumerate}
\end{enumerate}
\end{document}

其结果如下: 算法与周围的文本不对齐

在这两种情况下,算法之外的文本都没有与算法的开头水平对齐(向左对齐)。有人能给我一些建议吗?

答案1

你做错了。不要将算法放在浮点数中并要求它保持原位,只需使用algorithm带有Here 浮点数说明符的环境即可。如果您希望算法比文本块的其余部分更窄,那么您可以将其包装在 a 中minipage(就像您已经做的那样)。

\documentclass{article}

\usepackage[algoruled,vlined,linesnumbered]{algorithm2e}

\begin{document}

\begin{enumerate}
  \item 
  \begin{enumerate}
    \item Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm

    \begin{minipage}{0.7\linewidth}
      \begin{algorithm}[H]
        \caption{\textsc{Some Algo}($I$)}
        \SetKwInOut{Input}{input}
        \SetKwInOut{Output}{output}
        \SetCustomAlgoRuledWidth{1cm}
        \SetAlgoLined
        \DontPrintSemicolon
        \SetArgSty{textnormal}

        \Input{Some input description.}
        \Output{Some output description.} 

        \For{$i \leftarrow 1$ to $n$} {
          Do something \;
        }
      \end{algorithm}
    \end{minipage}

    Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm Some text describing the algorithm

    \begin{minipage}[h]{0.9\linewidth}
      \begin{algorithm}[H]
        \caption{\textsc{Some Algo}($I$)}
        \SetKwInOut{Input}{input}
        \SetKwInOut{Output}{output}
        \SetCustomAlgoRuledWidth{1cm}
        \SetAlgoLined
        \DontPrintSemicolon
        \SetArgSty{textnormal}

        \Input{Some input description.}
        \Output{Some output description.} 

        \For{$i \leftarrow 1$ to $n$} {
          Do something \;
        }
      \end{algorithm}
    \end{minipage}
  \end{enumerate}
\end{enumerate}

\end{document}

相关内容