我想用 latex 编写算法。我不想使用例如下面给出的代码:
\begin{algorithm}
\caption{Counting mismatches between two packed \DNA{} strings
\label{alg:packed-dna-hamming}}
\begin{algorithmic}[1]
\Require{$x$ and $y$ are packed \DNA{} strings of equal length $n$}
\Statex
\Function{Distance}{$x, y$}
\Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or}
\Let{$\delta$}{$0$}
\For{$i \gets 1 \textrm{ to } n$}
\If{$z_i \neq 0$}
\Let{$\delta$}{$\delta + 1$}
\EndIf
\EndFor
\State \Return{$\delta$}
\EndFunction
\end{algorithmic}
\end{algorithm}
上述代码的问题在于它是伪代码。但我的算法仅由英文句子组成。有没有更好的方法来编写此类算法?
答案1
如果您只有一个显示事件/步骤序列的项目列表,则使用常规列表(如enumerate
或itemize
)就足够了:
\documentclass{article}
\usepackage{lipsum}% Just for this example
\begin{document}
Consider the following structured approach:
\begin{enumerate}
\item This is the first sentence that contains a description of
what is important to do.
\item There is more in this sentence.
\item \lipsum[1]
\item A final sentence describing a warp-up of what should be
performed to complete the process you're want to discuss.
\end{enumerate}
\end{document}
algorithmicx
也可以按照您建议的方式使用 - 句子:
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{lipsum}% Just for this example
\begin{document}
\begin{algorithm}
\caption{This is an algorithm}
\begin{algorithmic}[1]
\State This is the first sentence that contains a description of
what is important to do.
\State There is more in this sentence.
\State \lipsum[1]
\State A final sentence describing a warp-up of what should be
performed to complete the process you're want to discuss.
\end{algorithmic}
\end{algorithm}
\end{document}
algorithm2e
也是一种选择:
\documentclass{article}
\usepackage{algorithm2e}
\DontPrintSemicolon
\LinesNumbered
\usepackage{lipsum}% Just for this example
\begin{document}
\begin{algorithm}
\caption{This is an algorithm}
This is the first sentence that contains a description of
what is important to do.\;
There is more in this sentence.\;
\lipsum*[1]\;
A final sentence describing a warp-up of what should be
performed to complete the process you're want to discuss.\;
\end{algorithm}
\end{document}