11

Looking for a way to set the width of an algorithm environment, preferably document wide (as opposed to per-algorithm). Thought it should be fairly easy, but couldn't find a particularly good solution.

Found this one here (wrap an algorithm in a minipage in a float, which is relatively okay, but surely there's a simpler way)?

\documentclass{article}
\usepackage{algorithm}
\floatstyle{plain}
\newfloat{myalgo}{tbhp}{mya}

\newenvironment{Algorithm}[2][tbh]%
{\begin{myalgo}[#1]
\centering
\begin{minipage}{#2}
\begin{algorithm}[H]}%
{\end{algorithm}
\end{minipage}
\end{myalgo}}

\begin{document}
\begin{Algorithm}[t]{10cm}
\caption{Does work, though no nice solution.}
\end{Algorithm}
\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
badroit
  • 7,557

1 Answers1

12

\floatevery{algorithm}{\setlength\hsize{10cm}} would limit the algorithm environment to a width of 10cm, but would not center it. (For additional centering one needs to re-define internal macros of the float package which does not sound like an elegant solution to me.)

So I'm afraid, the solution you have found seems to be quite good and if you would like to have the width setting global you could simply change the definition of Algorithm, e.g.:


\documentclass{article}
\usepackage{algorithm}
\floatstyle{plain}
\newfloat{myalgo}{tbhp}{mya}

\newenvironment{Algorithm}[1][tbh]%
{\begin{myalgo}[#1]
\centering
\begin{minipage}{10cm}
\begin{algorithm}[H]}%
{\end{algorithm}
\end{minipage}
\end{myalgo}}

\begin{document}
\begin{Algorithm}[t]
\caption{Does work, though no nice solution.}
\end{Algorithm}
\end{document}