2

I am using the algorithms environment from algorithm2e in a modified way with reduced width (following the solution here), see the MWE below.

This generates an overfull box that I'd like to get rid of. How can I achieve this?

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage[ruled]{algorithm2e}
\SetCustomAlgoRuledWidth{0.6\textwidth}
\makeatletter
\patchcmd{\@algocf@start}{%
    \begin{lrbox}{\algocf@algobox}%
}{%
    \rule{0.2\textwidth}{\z@}%
    \begin{lrbox}{\algocf@algobox}%
    \begin{minipage}{0.6\textwidth}%
}{}{}
    \patchcmd{\@algocf@finish}{%
    \end{lrbox}%
}{%
    \end{minipage}%
    \end{lrbox}%
}{}{}
\makeatother

\begin{document} \lipsum[1] \begin{algorithm}[h] \caption{NaiveSelect}\label{algo:naive-option} Some alg step ; \end{algorithm} \lipsum[2] \end{document}

The logfile says Overfull \hbox (276.00105pt too wide) in paragraph at lines 24--27.

user279611
  • 95
  • 6

1 Answers1

3

You need to fix \algowidth.

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage[ruled]{algorithm2e}

\AtBeginDocument{% \SetCustomAlgoRuledWidth{0.6\textwidth}% \setlength{\algowidth}{0.8\textwidth}% }

\makeatletter \patchcmd{@algocf@start} {\begin{lrbox}{\algocf@algobox}} {% \hspace*{0.2\textwidth}% \begin{lrbox}{\algocf@algobox}% \begin{minipage}{0.6\textwidth}% } {}{} \patchcmd{@algocf@finish} {\end{lrbox}} {\end{minipage}\end{lrbox}} {}{} \makeatother

\begin{document}

\lipsum[1]

\begin{algorithm}[h] \caption{NaiveSelect}\label{algo:naive-option} Some alg step ; \end{algorithm}

\lipsum[2]

\end{document}

The value of \textwidth is known with certainty at begin document, so it's best to set parameters depending on it at that place.

Using \hspace*{0.2\textwidth} is simpler than a rule.

egreg
  • 1,121,712