Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Many of the algorithms I have with the algorithm2e package do not extend across the page width. If I use the package's boxruled or ruled options, the result is a box or rule that extends the pagewidth with a lot of whitespace on the right side.

I have changed the width of the algorithm itself using \setlength{\algomargin}{2in} but this does not have an effect on the width of the rules. The effect is to 'center' the algorithm code, but leave the environment itself the width of the page.

Is there a way to change that without having to squeeze it into a minipage?

UPDATE: Minimal example:

\documentclass[10pt]{article}
\usepackage[ruled]{algorithm2e}
\begin{document}
\setlength{\algomargin}{2in}
\begin{algorithm}[t]
\caption{NaiveSelect}\label{algo:naive-option}
Some alg step \;
\end{algorithm}
\end{document}
share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Based on @lockstep answer, I've created a wrapper environment which allows the minipage width to be specified as an argument.

  %adds minipage to inside of algorithms
  \newlength{\tightalgowidth}
  \newlength{\tightalgoremainder}

  % uncomment to use centered floating algorithms
  \newenvironment{tightalgo}[2][]
  {
     \setlength{\tightalgowidth}{#2}
     \setlength{\tightalgoremainder}{\linewidth-\tightalgowidth}
     \begin{algorithm}[#1] }
  { \end{algorithm} }

  \makeatletter
  \patchcmd{\@algocf@start}{%
    \begin{lrbox}{\algocf@algobox}%
  }{%
    \rule{0.5\tightalgoremainder}{\z@}%
    \begin{lrbox}{\algocf@algobox}%
    \begin{minipage}{\tightalgowidth}%
  }{}{}
  \patchcmd{\@algocf@finish}{%
    \end{lrbox}%
  }{%
    \end{minipage}%
    \end{lrbox}%
  }{}{}
  \makeatother

Use it like this

  \begin{tightalgo}[ht]{0.5\linewidth}
  \caption{NaiveSelect}\label{algo:naive-option}
  Some alg step \;
  Some other \;
  \end{tightalgo}

For this result

enter image description here

share|improve this answer
add comment

In my solution, I use the etoolbox package to patch \algocf@start and \algocf@finish which are executed at the beginning resp. the end of the internal algocf environment. I add a minipage of 0.6\textwidth. An invisible rule with a length of 0.2\textwidth is used to center the minipage.

\documentclass[10pt]{article}
\usepackage[ruled]{algorithm2e}
\usepackage{etoolbox}
\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}
% \setlength{\algomargin}{2in}
\begin{algorithm}[t]
\caption{NaiveSelect}\label{algo:naive-option}
Some alg step \;
\end{algorithm}
\end{document}

enter image description here

share|improve this answer
    
This is a useful work around. I tried and failed to create an alternate algorithm environment using this method which takes width as a parameter (instead of begin fixed for all algorithms). Would you happen to know how this can be done? –  cheshirekow Jan 31 '13 at 16:30
    
@cheshirekow One could replace the hardcoded values "0.6" and "0.2" in my workaround with macros and redefife those macros mid-document. For better solutions please ask a follow-up question; note that the algorithm2e package in general is very hard to patch. –  lockstep Jan 31 '13 at 16:35
    
Got it. Added an answer with the result. Thanks ;) –  cheshirekow Jan 31 '13 at 16:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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