我正在使用该包编写代码algorithm
。但是,我想放置较小的边缘。我使用了minipage
、 框,但没有用。有人能给我一个建议吗?
我有这个
\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}
结果
但我想要
答案1
浮点数algorithm
的定义如下float
的\newfloat
。因此,整个浮动都用于可能的重组(包括 的放置\caption
,这取决于样式)。这也影响调整浮动宽度的可能性。
解决这个问题的一种方法是将浮点数放在 a 内部minipage
,使其不浮动。这由float
包的[H]
浮点规范支持:
\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}
当然,使用minipage
需要一个用来\begin{algorithm}[H]
避免浮动行为。如果你仍然想要一个浮动的algorithm
,你可以使用以下解决方法:
\begin{figure}[htb]
\centering
\begin{minipage}{.7\linewidth}
\begin{algorithm}[H]
% <your algorithm>
\end{algorithm}
\end{minipage}
\end{figure}
我们将非浮动元素放在algorithm
浮动元素内figure
没有使用\caption
为figure
。
答案2
以下解决方案对我有用:
\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}