设置算法环境的宽度(最好是文档宽度)

设置算法环境的宽度(最好是文档宽度)

寻找一种设置algorithm环境宽度的方法,最好是文档宽度(而不是每个算法的宽度)。我认为这应该相当容易,但找不到特别好的解决方案。

找到了这个这里(将算法包装在浮点数的小页面中,这相对来说还可以,但肯定有更简单的方法)?

\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}

答案1

\floatevery{algorithm}{\setlength\hsize{10cm}}会将算法环境的宽度限制为 10cm,但不会使其居中。(如果要实现额外的居中,需要重新定义 float 包的内部宏,这对我来说听起来不是一个优雅的解决方案。)

所以恐怕您找到的解决方案似乎相当不错,如果您希望将宽度设置全局化,您可以简单地更改算法的定义,例如:


\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}

相关内容