\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}
First of all many of us (if not even most of us) don't have LyX so better offer a LaTeX code instead. Second, better give a minimal example code, because usually reducing the code to it's minimum is the very first step when analyzing a LaTeX problem.
An reasonable example code showing the problem would be something like this:
\documentclass{scrbook}
\usepackage{float}
\usepackage{caption}
\usepackage[dvipsnames,svgnames,x11names,table]{xcolor}
\usepackage{algorithm}
% Use a hyphen for captions, and make links give a bit of space.
\DeclareCaptionFormat{algorithm}{\vspace{-1ex}\colorbox[HTML]{A6BFF2}{%
\parbox[c][1.75em][c]{\textwidth}{\hspace{0.25em}#1#2#3}}}
\captionsetup[algorithm]{format=algorithm}
\begin{document}
\begin{algorithm}
\caption{Blah\ldots}
A
\end{algorithm}
\end{document}
But now let's take a look at your problem: The extra spaces are typeset by the "ruled" float style offered by the float package. One can change this by defining an own float style and making the algorithm environment using this one instead of the original one. I did that by copying the "ruled" style and modifying it:
% Define own float style called "algorithm"
\makeatletter
\newcommand\fs@algorithm{%
\let\@fs@capt\floatc@algorithm
\def\@fs@pre{\hrule height.8pt depth0pt\relax}% \kern2pt removed
\def\@fs@mid{\hrule\kern2pt}% \kern2pt removed
\def\@fs@post{\kern2pt\hrule\relax}%
\let\@fs@iftopcapt\iftrue}
\makeatother
% Make the algorithm environment use the algorithm float style
\floatstyle{algorithm}
\restylefloat{algorithm}
So in total we have:
(Please note that I have removed the extra \vspace
of yours.)
\documentclass{scrbook}
\usepackage{float}
\usepackage{caption}
\usepackage[dvipsnames,svgnames,x11names,table]{xcolor}
\usepackage{algorithm}
% Define own float style called "algorithm"
\makeatletter
\newcommand\fs@algorithm{%
\let\@fs@capt\floatc@algorithm
\def\@fs@pre{\hrule height.8pt depth0pt\relax}% \kern2pt removed
\def\@fs@mid{\hrule\kern2pt}% \kern2pt removed
\def\@fs@post{\kern2pt\hrule\relax}%
\let\@fs@iftopcapt\iftrue}
\makeatother
% Make the algorithm environment use the algorithm float style
\floatstyle{algorithm}
\restylefloat{algorithm}
% Use a hyphen for captions, and make links give a bit of space.
\DeclareCaptionFormat{algorithm}{\colorbox[HTML]{A6BFF2}{%
\parbox[c][1.75em][c]{\textwidth}{\hspace{0.25em}#1#2#3}}}
\captionsetup[algorithm]{format=algorithm}
\begin{document}
\begin{algorithm}
\caption{Blah\ldots}
A
\end{algorithm}
\end{document}
Please note that this code still contains 2x \kern2pt
, one before and one after the algorithm body. If you don't like this extra vertical space, too, just remove them.
Best Answer
The
algorithm
float is defined usingfloat
'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 thefloat
package's[H]
float specification:Of course, using a
minipage
requires one to use\begin{algorithm}[H]
(to avoid the floating behaviour). If you still want a floatingalgorithm
, you can use the following work-around:We place the non-float
algorithm
inside a floatingfigure
without using a\caption
for thefigure
.