我正在使用該包編寫程式碼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]
float 規範所支持的:
\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
沒有使用 a\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}