我在论文中使用algpseudocode
该包提供的环境。我将伪代码包装在该包提供的环境中。algorithmicx
algorithmic
algorithms
问题是算法占据了整个文本宽度,这使其解析起来有点困难(特别是注释与相应行相距太远)。此外,对于排版带有一两个数学变量的小型算法,使用整个文本宽度是没有必要的。
我希望在序言中留出一处地方,将论文中 12 种算法的宽度设置为自定义宽度,例如0.75\textwidth
,以便轻松更改。这看起来像是自定义环境的候选。
为了调整宽度,我使用了一个 minipage,如果我放弃算法的浮动功能(这很好),它就可以工作。但是,算法在页面中排版为左侧对齐。要使其居中,该\makebox[\textwidth][c]{ ...}
构造不适用于自定义环境。
以下是 MWE 来说明这个问题
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}
\newcommand{\tempcaption}{}% stores the caption
\newcommand{\templabel}{}% stores the label
\newenvironment{customalgo}[2]
{%
\begin{minipage}{0.7\textwidth}
\begin{algorithm}[H]
\centering
\gdef\tempcaption{#1}% store the caption so we can use it later
\gdef\templabel{#2}% store the label so we can use it later
\begin{algorithmic}[1]
}%
{%
\end{algorithmic}
\caption{\tempcaption}% use the stored caption
\label{\templabel}
\end{algorithm}
\bigskip
\end{minipage}
}%
%%%%%% End of Preamble%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\textbf{Needs centering}\par
\begin{customalgo}{Computing a Sum - needs centering}{alg:algosumnonmakebox}
\Procedure{SUM}{ $\{x\}$}
\State $y\gets0$
\For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
\State $y\gets y+x(i)$ \Comment{Summing up.}
\EndFor
\State \textbf{return} $y$
\EndProcedure
\end{customalgo}
\textbf{Centered with makebox}\par
\noindent\makebox[\textwidth][c]{%
\begin{customalgo}{Computing a Sum}{alg:algosum}
\Procedure{SUM}{ $\{x\}$}
\State $y\gets0$
\For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
\State $y\gets y+x(i)$ \Comment{Summing up.}
\EndFor
\State \textbf{return} $y$
\EndProcedure
\end{customalgo}
}
\end{document}
产生
问题是,如何实现所有算法的中心化,而无需手动将每个算法中心化?
答案1
对我来说,下面提到的代码运行完美。请查看附件的屏幕截图。
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}
\newcommand{\tempcaption}{}% stores the caption
\newcommand{\templabel}{}% stores the label
\newenvironment{customalgo}[2]
{%
\begin{minipage}{0.8\textwidth}
\centering
\begin{algorithm}[H]
\gdef\tempcaption{#1}% store the caption so we can use it later
\gdef\templabel{#2}% store the label so we can use it later
\begin{algorithmic}[1]
}%
{%
\end{algorithmic}
\caption{\tempcaption}% use the stored caption
\label{\templabel}
\end{algorithm}
\bigskip
\end{minipage}
}%
%%%%%% End of Preamble%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\textbf{Needs centering}
\begin{center}
\begin{customalgo}{Computing a Sum - needs centering {alg:algosumnonmakebox}
\Procedure{SUM}{ $\{x\}$}
\State $y\gets0$
\For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
\State $y\gets y+x(i)$ \Comment{Summing up.}
\EndFor
\State \textbf{return} $y$
\EndProcedure
\end{customalgo}
\end{center}
\par \textbf{Centered with makebox}
%\noindent\makebox[\textwidth][c]{%
%\qquad
\begin{center}
\begin{customalgo}{Computing a Sum}{alg:algosum}
\Procedure{SUM}{ $\{x\}$}
\State $y\gets0$
\For{$i \gets 1 : N^{x}$} \Comment{Time series $\{x\}$ has length $N^{x}$}
\State $y\gets y+x(i)$ \Comment{Summing up.}
\EndFor
\State \textbf{return} $y$
\EndProcedure
\end{customalgo}
%}
\end{center}
\end{document}