我正在编写一个算法,其中输入有两行。我首先尝试使用itemize
环境,后来用 代替,algorithmic
以获得统一的外观和感觉。问题是,实际算法中的计数器现在从 3 开始。为了解决这个问题,我把它放在另一行中algorithmic
(这些行标记为% Comment out this line
)。这解决了编号问题,但增加了缩进问题(现在看起来这些是下面的行输出)。
我有这个最小的工作示例:
\documentclass{article}
\usepackage{algorithm}
%\usepackage{algorithmic}
\PassOptionsToPackage{noend}{algpseudocode}
\usepackage{algpseudocode}%
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%
\begin{document}
\begin{algorithm}
\caption{Caption}\label{label}
\begin{algorithmic}[1]
\Input
{\begin{algorithmic}[1]
\item Something in line 1
\item Something in line 2
\end{algorithmic}
}
\Output {Suppose something is in here}
\begin{algorithmic}[1] % Comment out this line
\State Here goes the actual algorithm
\State Now the next step
\State This step, and we are done
\State \Return {Here you go}
\end{algorithmic} % Comment out this line
\end{algorithmic}
\end{algorithm}
\end{document}
问题(全部关于缩进):
- 注意输入缩进不一致(第 2 行向左移动了一点)。我该如何修复?
- 在给定当前版本的情况下,如何减少实际算法中步骤的缩进?
- (替代 2,至少回答一个就足够了)。如果注释掉标记的行,问题 2 中的缩进就会消失。在这种情况下,我如何将算法编号重置回 1?
答案1
以下是两个建议。其中一个可能适合您的需求:
\documentclass{article}
\usepackage{algorithm}
%\usepackage{algorithmic}
\PassOptionsToPackage{noend}{algpseudocode}
\usepackage{algpseudocode}%
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%
\begin{document}
\begin{algorithm}
\caption{Caption}\label{label}
\begin{algorithmic}[1]
\Input Something in line 1
Something in line 2
\Output {Suppose something is in here}
\State Here goes the actual algorithm
\State Now the next step
\State This step, and we are done
\State \Return {Here you go}
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\caption{Caption}\label{label}
\begin{algorithmic}[1]
\Input
\item Something in line 1
\item Something in line 2 \setcounter{ALG@line}{0}
\Output {Suppose something is in here}
\State Here goes the actual algorithm
\State Now the next step
\State This step, and we are done
\State \Return {Here you go}
\end{algorithmic}
\end{algorithm}
\end{document}