最好看的伪代码包是哪一个?

最好看的伪代码包是哪一个?

我偶然发现了各种伪代码包,但似乎找不到简单、美观又实用的。

我正在寻找:

  • 视觉上有吸引力的结构(赏心悦目)(易于观察 - 易于理解)
  • 编号
  • 彩色(可选)
  • 标题在顶部
  • 实用且易于使用

我现在用的是

\documentclass[12pt,a4paper]{report}
\usepackage{algorithm2e} %for psuedo code
\usepackage[lmargin=3.81cm,tmargin=2.54cm,rmargin=2.54cm,bmargin=2.52cm]{geometry}
\begin{document}
\begin{algorithm}[H] %or another one check
 \caption{How to write algorithms}
     \SetAlgoLined
     \KwData{this text}
     \KwResult{how to write algorithm with \LaTeX2e }
     initialization\;
     \While{not at end of this document}{
      read current\;
      \eIf{understand}{
       go to next section\;
       current section becomes this one\;
       }{
       go back to the beginning of current section\;
      }
     }

\end{algorithm}
\end{document}

输出

在此处输入图片描述

然而,我不太喜欢这里代码的呈现方式。

答案1

提出这个问题的用户可能对我的答案不再感兴趣。但我在同样的任务中没有找到任何好看的算法包(符合我的口味)。
因为我是房源包我按照 Ruben 的建议创建了自己的环境。特点:

  • 突出显示我自己的特定关键字(但可以使用预定义的语言来实现此目的)
  • 当定义标题时,它显示为“算法xy:标题”,其中 x 是章节编号,y 是算法编号(但如果不需要章节级别,则可以轻松更改)

以下是序言中的代码!

\usepackage{color}
\usepackage{listings}
\usepackage{caption}

\newcounter{nalg}[chapter] % defines algorithm counter for chapter-level
\renewcommand{\thenalg}{\thechapter .\arabic{nalg}} %defines appearance of the algorithm counter
\DeclareCaptionLabelFormat{algocaption}{Algorithm \thenalg} % defines a new caption label as Algorithm x.y

\lstnewenvironment{algorithm}[1][] %defines the algorithm listing environment
{   
    \refstepcounter{nalg} %increments algorithm number
    \captionsetup{labelformat=algocaption,labelsep=colon} %defines the caption setup for: it ises label format as the declared caption label above and makes label and caption text to be separated by a ':'
    \lstset{ %this is the stype
        mathescape=true,
        frame=tB,
        numbers=left, 
        numberstyle=\tiny,
        basicstyle=\scriptsize, 
        keywordstyle=\color{black}\bfseries\em,
        keywords={,input, output, return, datatype, function, in, if, else, foreach, while, begin, end, } %add the keywords you want, or load a language as Rubens explains in his comment above.
        numbers=left,
        xleftmargin=.04\textwidth,
        #1 % this is to add specific settings to an usage of this environment (for instnce, the caption and referable label)
    }
}
{}

现在您可以像下面这样简单使用:

\begin{algorithm}[caption={Integer division.}, label={alg1}]
 input: int N, int D
 output: int
 begin
   res $\gets$ 0
   while N $\geq$ D 
     N $\gets$ N - D
     res $\gets$ res + 1      
   end
   return res
 end       
\end{algorithm}

排版结果

希望你和我一样享受这个旅程 :P

答案2

您可以尝试使用 listings,它首先提供了排版程序代码而不是伪代码的解决方案。虽然我认为它可以奏效。例如,我在数值数学讲座中使用它来排版 Matlab 代码。由于 Matlab 非常通用并且接近伪代码,因此这可能适合您的问题:

\documentclass[12pt,a4paper]{report}
\usepackage[lmargin=3.81cm,tmargin=2.54cm,rmargin=2.54cm,bmargin=2.52cm]{geometry}
\usepackage{color}
\definecolor{darkblue}{rgb}{0,0,.75}
\usepackage{listings} %iclude code in your document

\lstloadlanguages{Matlab} %use listings with Matlab for Pseudocode
\lstnewenvironment{PseudoCode}[1][]
{\lstset{language=Matlab,basicstyle=\scriptsize, keywordstyle=\color{darkblue},numbers=left,xleftmargin=.04\textwidth,#1}}
{}

\begin{document}
\begin{figure}[h]
\caption{How to write algorithms}
\textbf{Data:} this text\\
\textbf{Result:} how to write algorithm with \LaTeX2e\\
initialization;
\begin{PseudoCode}
while not at end of this document do
    read current;
    if understand
        go to next section;
        current section becomes this one;
    else
        go back to the beginning of current section;
    end
end
\end{PseudoCode}
\end{figure}
\end{document}

现在您只需定义一个 float 环境的 figure 别名(包:float、newfloat 等)或使用 caption 包来修复算法的标题名为“figure 1:...”的问题。使用所提供的解决方案只有一个缺点:关键字如 while、if、end、... 在文本中随处可见,即使不需要也是如此;例如在代码的第 1 行。也许这里有人知道如何解决这个问题。与此同时,我也会查找这个问题。

相关内容