我在使用该包将纯文本放入算法时遇到了一些麻烦algorithmic
。问题似乎只发生在“\WHILE”命令之后。我需要启动“while”循环,然后立即放置一段文本,其中包含对下一个子代码段的简要描述。
我目前在代码中插入文本如下:
\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}
\STATE line 4
\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}
它会在代码中产生我想要的文本类型,即一段没有与之关联的算法行号的文本。
因此,基于此,我认为应该采取以下措施:
\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}
\textit {describe the next step 1}
\STATE line 4
\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}
然而,这会产生错误:
line 12: Something's wrong--perhaps a missing \item. \STATE
line 12: Something's wrong--perhaps a missing \item. \STATE
..snip..
line 16: Something's wrong--perhaps a missing \item. \ENDWHILE
以及输出中的无意义的格式。
我能得到的最接近的结果是插入一个额外的行(我不想要),如下所示:
\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}
\STATE Inserted Text
\textit {describe the next step 1}
\STATE line 4
\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}
\STATE
或者在文本行上使用命令,为文本提供行号。
知道发生了什么事吗?当然,还有,治疗方法是什么?
在 Werner 回答之后(谢谢),我认为这可能有助于更好地澄清这一点。伪代码是大量数学的,因此无法立即清楚发生了什么。所以我想添加一行注释“描述第 4-6 行”,这可能会更清楚:
\documentclass{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE {test}
\STATE line 4
\STATE line 5
\STATE line 6
\textit {describe lines 7-9}
\STATE line 7
\STATE line 8
\STATE line 9
\textit {describe lines 10-12}
\STATE line 10
\STATE line 11
\STATE line 12
\ENDWHILE
\end{algorithmic}
\end{document}
答案1
在下面algorithmic
您可以添加评论\WHILE
与使用可选参数在同一行:
\documentclass{article}
\usepackage{algorithmic}
%\newcommand{\algorithmiccomment}[1]{\{#1\}}% Original definition
\renewcommand{\algorithmiccomment}[1]{\textit{#1}}% Updated definition
\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE [describe while condition] {test}
\STATE line 4
\textit {describe the next step 2}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}
请注意,注释的默认格式是通过\algorithmiccomment{<comment>}
设置{<comment>}
- 一对花括号内的注释{
... }
。上面我已将其重新格式化为\textit{<comment>}
。
以下最小示例基于上述建议,提供了带星号的版本\STATE*
。带星号的版本不打印任何行号:
\documentclass{article}
\usepackage{algorithmic,xpatch}
%\newcommand{\algorithmiccomment}[1]{\{#1\}}% Original definition
\renewcommand{\algorithmiccomment}[1]{\textit{#1}}% Updated definition
\makeatletter
\xpatchcmd{\algorithmic}% <cmd>
{\newcommand{\STATE}{\ALC@it}}% <search>
{\newcommand{\STATE}{\@ifstar\STATEstar\STATEnostar}}% <replace>
{}{}% <success><failure>
\newcommand{\STATEstar}{\item[]}
\newcommand{\STATEnostar}{\ALC@it}
\makeatother
\begin{document}
\begin{algorithmic}[1]
\STATE line 1
\STATE line 2
\WHILE [describe while condition] {test} \label{first}
\STATE* \textit {describe the item on line~\ref{first}}
\STATE line 4 \label{second}
\textit {describe step~\ref{second}}
\STATE line 5
\ENDWHILE
\end{algorithmic}
\end{document}