# Adjust width of algorithm float

I am writing a code using the algorithm package. However, I would like to put smaller edges. I used the minipage, boxes and did not work. Could someone give me a suggestion?

I have this

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{algorithm}
%========================
\begin{document}
\section*{Example Algorithm}
%----------------------
\begin{algorithm}
\caption{Penalty function}
\begin{itemize}
\item Let $\{c_k\}$, $k=1,2,\ldots$, be a sequence tending to infinity such that for each $k$, $0 \leq c_k < c_{k+1}$.
\item Define the function $$q(\mu_{k},x) = f(x) + \mu_{k}P(x).$$
\item For each k solve the problem
\begin{gather*}  minimize \ q(\mu_{k},x), \end{gather*}
obtaining a solution point $x_{k}$.
\end{itemize}
\end{algorithm}
%----------------------
\end{document}


which results

but, I want

• A solution for putting a floating environment into minipage is here and it works for this problem. You just have to add [H] behind \begin{algorithm} and it works in the minipage. Commented Jan 25, 2017 at 19:29

The algorithm float is defined using float's \newfloat. As a result, the entire float is consumed for possible restructuring (including the placement of the \caption, which depends on the style). This also affects the possibility of adjusting the width of the float.

One way around this would be to place the float inside a minipage and make it not float. This is supported by the float package's [H] float specification:

\documentclass{article}

\usepackage{amsmath,algorithm}

\begin{document}

\section*{Example Algorithm}

\begin{algorithm}[H]
\caption{Penalty function}
\begin{itemize}
\item Let $\{c_k\}$, $k = 1,2,\dots$, be a sequence tending to infinity such that for each~$k$, $0 \leq c_k < c_{k+1}$.
\item Define the function $$q(\mu_{k},x) = f(x) + \mu_k P(x).$$
\item For each k solve the problem
$\text{minimize } q(\mu_k,x),c$
obtaining a solution point~$x_k$.
\end{itemize}
\end{algorithm}

{\centering
\begin{minipage}{.7\linewidth}
\begin{algorithm}[H]
\caption{Penalty function}
\begin{itemize}
\item Let $\{c_k\}$, $k = 1,2,\dots$, be a sequence tending to infinity such that for each~$k$, $0 \leq c_k < c_{k+1}$.
\item Define the function $$q(\mu_{k},x) = f(x) + \mu_k P(x).$$
\item For each k solve the problem
$\text{minimize } q(\mu_k,x),c$
obtaining a solution point~$x_k$.
\end{itemize}
\end{algorithm}
\end{minipage}
\par
}

\end{document}


Of course, using a minipage requires one to use \begin{algorithm}[H] (to avoid the floating behaviour). If you still want a floating algorithm, you can use the following work-around:

\begin{figure}[htb]
\centering
\begin{minipage}{.7\linewidth}
\begin{algorithm}[H]
\end{algorithm}
\end{minipage}
\end{figure}


We place the non-float algorithm inside a floating figure without using a \caption for the figure.

• Thanks @Werner. I preferred to use the figure environment because this style is the best for my text.
– Lara
Commented Jan 25, 2017 at 22:57
• The second solution gives me an error: LaTeX Error: Not in outer par mode. Commented Sep 1, 2020 at 22:59
• A working (although not so pretty) floating solution is this: tex.stackexchange.com/a/132425/77953 Commented Oct 12, 2021 at 16:48
• @6005 just in case anyone's facing the same error, you're probably not suppressing your algorithm's floating with [H]. The second solution does work as long as you don't have floats inside the minipage. Commented Oct 24, 2022 at 11:40

The following solution worked for me:

\documentclass{article}

\usepackage{amsmath,algorithm}

\begin{document}

\begin{algorithm}
\centering
\begin{minipage}{0.8\linewidth}
\centering
\caption{%caption
}
\label{%label
}
\begin{algorithmic}[1]
% Your algorithm here
\end{algorithmic}
\end{minipage}
\end{algorithm}

\end{document}

• Have you tested it? This code produces an algorithm that is still taking up the full width of the line for the \caption; only the algorithmic part is spread out (image). Also, you need algorithmic or some other package to use the algorithmic environment.
– Werner
Commented May 24, 2022 at 19:48