![以算法宽度调整为目的而定义的自定义环境为中心](https://linux22.com/image/376394/%E4%BB%A5%E7%AE%97%E6%B3%95%E5%AE%BD%E5%BA%A6%E8%B0%83%E6%95%B4%E4%B8%BA%E7%9B%AE%E7%9A%84%E8%80%8C%E5%AE%9A%E4%B9%89%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E4%B8%BA%E4%B8%AD%E5%BF%83.png)
我在论文中使用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}