3

I have this code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[portuguese, ruled, linesnumbered, boxruled]{algorithm2e}

\begin{document}
\begin{algorithm}[H]
    \SetAlgoLined
    \Entrada{Parâmetros}
    \Saida{$P_t$}
    \Inicio{
      $t \leftarrow$ GeraNovaPopulação(); \\
      Avalia($P_t$); \\
      \Enqto{\textbf{\textit{não}} CriterioParadaAtingido()}{
        $Q_t \leftarrow$ Variação($P_t$); \\
        Avalia($Q_t$); \\
        $P_{t + 1} \leftarrow$ Atualiza($Q_t$, $P_t$); \\
        $t \leftarrow t + 1$; \\
      }
    }
    \caption{Pseudocódigo de uma MOEA.\label{algo:moea-pseudocode}}
    \end{algorithm}}
\end{document}

And it produces the following output:

Output from the code

What I want to know if it is possible to decrease the width of the horizontal line, i.e, shrink it to the maximum.

EDIT:

The desired output, as posted in the comments by @Werner and mentioned by @marmot is:

Desired Output

4
  • 1
    @marmot Yes! Sorry to not be totally clear! Feb 6, 2019 at 1:45
  • So you want it to look like this?
    – Werner
    Feb 6, 2019 at 1:45
  • @Werner Yes! Exactly this way. Feb 6, 2019 at 1:47
  • 1
    If you want a solution for this specific example, just wrap the algorithm into \begin{minipage}{9cm} and \end{minipage}. As for the general case, I have no clue. (One could probably use the tikzmark library to measure the length of the lines, but this will lead to a very convoluted proposal, at least when I try to spell it out.)
    – user121799
    Feb 6, 2019 at 4:49

1 Answer 1

2

The algorithm2e package completely manages its own floating mechanism and therefore doesn't have the regular handles or hooks one would associate with a float. As such, the simplest way to achieve what you're after is to wrap the algorithm inside a centered minipage where you specify the block width:

enter image description here

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage[portuguese, ruled, linesnumbered, boxruled]{algorithm2e}

\begin{document}

\begin{center}
  \begin{minipage}{.725\linewidth}
    \begin{algorithm}[H]
      \SetAlgoLined
      \Entrada{Parâmetros}
      \Saida{$P_t$}
      \Inicio{
        $t \leftarrow$ GeraNovaPopulação(); \\
        Avalia($P_t$); \\
        \Enqto{\textbf{\textit{não}} CriterioParadaAtingido()}{
          $Q_t \leftarrow$ Variação($P_t$); \\
          Avalia($Q_t$); \\
          $P_{t + 1} \leftarrow$ Atualiza($Q_t$, $P_t$); \\
          $t \leftarrow t + 1$; \\
        }
      }
      \caption{Pseudocódigo de uma MOEA.\label{algo:moea-pseudocode}}
    \end{algorithm}
  \end{minipage}
\end{center}

\end{document}

The value of 0.725\linewidth is what you can tweak to suit your needs.

The above approach relies on the fact that your algorithm is set to stay [H]ere (where it's placed inside the text). If you want it to float but be constrained, you'll have to resort to something like this:

\begin{figure}[<float spec>]
  \centering
  \begin{minipage}{.725\linewidth}
    \begin{algorithm}[H]
      % <your algorithm>
    \end{algorithm}
  \end{minipage}
\end{figure}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .