# How can I make an algorithm environment wider than \linewidth or \textwidth?

I have already tried two things: \makebox[1.2\textwidth][c]{...} which did not work (perhaps missing \item). \redefinegeometry does not work well in this case beacause it’s not on a single page. MWE:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}


You could make algorithm unfloatable and box it with another floatable environment which adapts the width.

In the following, an invisible tcolorbox is used for this purpose. Adapt the grow to left by and grow to right by options for the width needed. Also, the float settings are done by the float option.

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{lipsum}

\usepackage[skins]{tcolorbox}

\begin{document}

\lipsum[1]

\begin{tcolorbox}[blanker,float=tbp,
grow to left by=1cm,grow to right by=1cm]
\begin{algorithm}[H]
\caption{Euclid's algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{tcolorbox}

\lipsum[1]

\end{document}


If there are many algorithm boxes to be adapted, you could use the following variant where an environment talgorithm is constructed. It takes a symmetric growth value as mandatory parameter and any tcolorbox option as optional parameter:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{lipsum}

\usepackage[skins]{tcolorbox}

\newtcolorbox{talgorithm}[2][]{%
blanker,float=tbp,grow to left by=#2,grow to right by=#2,
before upper={\begin{algorithm}[H]},
after upper={\end{algorithm}},
#1
}

\begin{document}

\lipsum[1]

\begin{talgorithm}{1cm}
\caption{Euclid's algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{talgorithm}

\lipsum[1]

\end{document}


The problem is that algorithm is a floating environment and cannot be boxed.

But you can tell it not to float using H as a floating specifier (\begin{algorithm}[H])

and use, for example, the fullwidth environment from the fullwidth package in this way

\begin{fullwidth}[width=\linewidth+2cm,leftmargin=-1cm,rightmargin=-1cm]
\end{fullwidth}


In this case you're decreasing both the left and the right margin by 1cm. Adjust the values to your needs.

Full example:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{fullwidth}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{fullwidth}[width=\linewidth+2cm,leftmargin=-1cm,rightmargin=-1cm]
\begin{algorithm}[H]
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d. of m and l}
\State $r\gets m\bmod l$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $m\gets l$
\State $l\gets r$
\State $r\gets m\bmod l$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $l$\Comment{The gcd is l}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{fullwidth}

\lipsum[1]

\end{document}


Output:

If you're comfortable to not let the algorithm float, then you can put it in a box that you can readjust to suit your needs:

\documentclass{article}

\usepackage{algorithm,lipsum,changepage}
\usepackage[noend]{algpseudocode}

\begin{document}

\lipsum[1]

\noindent\hspace*{-.1\textwidth}%
\begin{minipage}{1.2\textwidth}%
\begin{algorithm}[H]
\caption{Euclid’s algorithm}\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of $m$ and $l$}
\State $r \gets m \bmod l$
\While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
\State $m \gets l$
\State $l \gets r$
\State $r \gets m \bmod l$
\EndWhile
\State \textbf{return} $l$\Comment{The g.c.d.\ is $l$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{minipage}

\end{document}


For adjusting the width of the float in general, you can use the following preamble modifications:

\documentclass{article}

\usepackage{algorithm,lipsum,etoolbox}
\usepackage[noend]{algpseudocode}

\makeatletter
\newif\if@algorithm
\patchcmd{\@float@Hx}% <cmd>
{\@nodocument}% <search>
{\@nodocument%
\ifnum\pdfstrcmp{#1}{algorithm}=0 % Check whether using algorithm float
\@algorithmtrue% Inside algorithm environment
\setlength{\columnwidth}{\algorithmwidth}% Update column width
\fi}% <replace>
{}{}% <success><failure>
\renewcommand\float@makebox[1]{%
\hbox{%
% Adjust horizontally to give a centred look
\if@algorithm\hspace*{-.5\dimexpr\algorithmwidth-\textwidth}\fi%
\vbox{\hsize=#1 \@parboxrestore
\@fs@pre\@fs@iftopcapt
\ifvoid\@floatcapt\else\unvbox\@floatcapt\par\@fs@mid\fi
\unvbox\@currbox
\else\unvbox\@currbox
\ifvoid\@floatcapt\else\par\@fs@mid\unvbox\@floatcapt\fi
\fi\par\@fs@post\vskip\z@}}}

\makeatother

\newlength{\algorithmwidth}
\AtBeginDocument{\setlength{\algorithmwidth}{1.2\textwidth}}

\begin{document}

\lipsum[1]

\begin{algorithm}[t]
\caption{Euclid’s algorithm}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of $m$ and $l$}
\State $r \gets m \bmod l$
\While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
\State $m \gets l$
\State $l \gets r$
\State $r \gets m \bmod l$
\EndWhile
\State \textbf{return} $l$\Comment{The g.c.d.\ is $l$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\lipsum[2]

\setlength{\algorithmwidth}{.8\textwidth}

\begin{algorithm}[b]
\caption{Euclid’s algorithm}
\begin{algorithmic}[1]
\Procedure{Euclid}{$m,l$}\Comment{The g.c.d.\ of $m$ and $l$}
\State $r \gets m \bmod l$
\While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
\State $m \gets l$
\State $l \gets r$
\State $r \gets m \bmod l$
\EndWhile
\State \textbf{return} $l$\Comment{The g.c.d.\ is $l$}
\EndProcedure
\end{algorithmic}
\end{algorithm}

\lipsum[3]
\end{document}


The preamble modifications includes patches to the float builder that is specific to the algorithm environment. The width of the float box is adjusted to \algorithmwidth (which can be set/changed throughout the document), again only while using the algorithm float.

Since the treatment of traditional floats and (non-)floats using a [H]ERE placement is completely different, the above-mentioned methods are exclusive. If you wish to combine them, you'd have to combine both methods. A non-float-ish alternative to [H] would be [h!].