2

I am having trouble aligning my algorithm with the text outside of it. Here is an example:

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

which produces the following: Algorithm not aligned with text surrounding it

In both cases, the text outside of the algorithm is not aligned horizontally (to the left) with the start of the algorithm. Could someone give me some advice about this?

1
  • 1
    Rather use geometry to set the page layout/geometry, instead of setting margin lengths manually.
    – Werner
    Apr 8 '20 at 19:02
3

You're doing it wrong. Instead of placing the algorithm inside a float and requesting it to stay in-place, just use the algorithm environment with the Here float specifier. If you want the algorithm to be narrower than the remainder of the text block, then you can wrap it inside a minipage (like you've already done).

\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}
2
  • +1: I laughed at the diplomatic "You're doing it wrong.". Apr 8 '20 at 19:05
  • Thank you! i wrapped it all in a figure environment hoping it would help with alignment but it really didn't. Clearly, less is more
    – coolboy124
    Apr 8 '20 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.