4

I am having a small issue where where the line

Let $s$ be the highest-ranked student in $h$'s preference list which $h$ has not yet requested to be a resident\

in my algorithm is wrapping a little too late. The boundary should be the same as the line under "Homework 1" provided by fancyhdr I believe. Below is that complete portion of me tex file. Could anyone assist me in getting that line to wrap earlier? If I force it with a newline then it will also give an incorrect line numbering for the algorithm environment so that quick fix isn't an option I am willing to settle with.

\begin{enumerate}
...
\item[\textbf{4.}] 
    Below is an algorithm to find a stable assignment of students to hospitals:

    \begin{algorithm}[H]
        \SetKwData{List}{list}
        \SetKwFunction{Remove}{Remove}
        \SetKwFunction{Insert}{Insert}
        \SetKwInOut{Input}{input}

        \Input{A set of hospitals $H$ where each $h \in H$ has a list of students $h.\List$ with a capacity equivalent to number of positions available \\ A set of students $S$}
        Initially $s$ is free for all $s \in S$ and $h.\List$ is empty for all $h \in H$. \\
        \While{there exists $h \in H$ such that $h.\List$ is not full}
        {
            Choose such a hospital $h$\\
            \While{$h.\List$ is not full}
            {
                Let $s$ be the highest-ranked student in $h$'s preference list which $h$ has not yet requested to be a resident\\
            }
        }
    \end{algorithm}
...
\end{enumerate}

1 Answer 1

4

According to the algorithm2e code, [H]-style algorithms are set as a minipage of width \hsize (in algorithm2e.sty):

%%% environment for {algorithm}[H]
\newenvironment{algocf@Here}{\noindent%
  \def\@captype{algocf}% if not defined, caption exit with an error
  \begin{minipage}{\hsize}%
}{%
  \end{minipage}%\par%
}%

Changing this to a minipage of width \linewidth saves the day. Use etoolbox to patch \algocf@Here:

enter image description here

\documentclass{article}
\usepackage{showframe,etoolbox}% http://ctan.org/pkg/{showframe,etoolbox}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e

\makeatletter
\patchcmd{\algocf@Here}{\hsize}{\linewidth}{}{}
\makeatother
\begin{document}
\begin{enumerate}
\item[\textbf{4.}] 
    Below is an algorithm to find a stable assignment of students to hospitals:

    \begin{algorithm}[H]
        \SetKwData{List}{list}
        \SetKwFunction{Remove}{Remove}
        \SetKwFunction{Insert}{Insert}
        \SetKwInOut{Input}{input}

        \Input{A set of hospitals~$H$ where each $h \in H$ has a list of students $h.\List$ with a capacity equivalent to number of positions available \\
          A set of students~$S$}
        Initially~$s$ is free for all $s \in S$ and $h.\List$ is empty for all $h \in H$\;
        \While{there exists $h \in H$ such that $h.\List$ is not full}
        {
            Choose such a hospital~$h$\;
            \While{$h.\List$ is not full}
            {
                Let~$s$ be the highest-ranked student in~$h$'s preference list which~$h$ has not yet requested to be a resident
            }
        }
    \end{algorithm}
\end{enumerate}
\end{document}

\patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>} replaces <search> in <cmd> with <replace> - in this case, \hsize with \linewidth. \linewidth gives a true representation of the width of a line inside a list (in your case, an enumerate). For a discussion on the difference in these lengths, see Difference between \textwidth, \linewidth and \hsize.

You must log in to answer this question.

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