27

I have stumbled across various pseudo code packages but can't seem to find that's simple and eye-candy and practical.

What I'm looking for:

  • Visually attractive structure (eye-candy) ( easy to look at -- understandable )
  • Numbered
  • Colored (optional)
  • Title on top
  • Practical and easy to use

The one I'm using now is

\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}

which outputs

enter image description here

However, I do not really like how the code is presented here.

Razor
  • 2,691
  • 6
    We might be able to help you better if you state what you do not (and what you do) like about the code. “Visually attractive structure (eye-candy)” is very subjective. Some like it fancy, some like it serious. – Qrrbrbirlbel Apr 28 '13 at 01:54
  • @Qrrbrbirlbel can you give a rough of example of what you mean by fancy or serious ? – Razor Apr 28 '13 at 02:00
  • @Qrrbrbirlbel I updated question. – Razor Apr 28 '13 at 02:11
  • I give a number of examples in Print programs with its proper syntax as discussion of the possibilities. Not sure whether it's a duplicate. – Werner Apr 28 '13 at 03:24
  • I've removed the [tag:texshop] tag, since it is not relevant to the question. TeXShop is an editor, and your question is about packages. – Alan Munn Apr 28 '13 at 15:07
  • @AlanMunn wouldn't it matter what type of edit I'm using ? – Razor Apr 28 '13 at 15:30
  • @Razor No. There are virtually no packages that depend on your editing environment, or even your OS. Only if a package calls an external tool is there sometimes an issue with your editor. – Alan Munn Apr 28 '13 at 15:52
  • @AlanMunn Thanks for the info, didn't know that :D – Razor Apr 28 '13 at 15:56
  • Pseudocode is supposed to describe what an algorithm does in a language-agnostic way. I find the algorithm2e package very good for typesetting pseudocode. – jub0bs Apr 30 '13 at 11:41
  • Related: http://tex.stackexchange.com/questions/172981/pseudo-code-with-vertical-line – Steven B. Segletes Dec 18 '14 at 14:42
  • Attention: The algorithm2e package cannot split your pseudo-code across multiple pages! See https://tex.stackexchange.com/questions/18949/algorithm2e-split-over-several-pages The listings package can. – Christopher K. Oct 11 '19 at 14:10

2 Answers2

26

Probably the user who asked this question is not interested in my answer anymore. But I was in the same quest didn't find any good-looking algorithm package (for my taste).
Since I'm a fan of the Listings package I followed Ruben's suggestion and created my own environment. Features:

  • higlights my own specific keywords (but a predefined language can be used to that end)
  • When defined a caption, it appears as "Algorithm x.y: the caption" where x is the number of the chapter and y is the number of the algorithm (but this is easily changed if chapter-level is not required)

Here follows the code to go in the preamble!

\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)
    }
}
{}

Now you can use as simple as follows:

\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}

Typeset result

Hope you enjoy as much as I did :P

Adriaan
  • 3,675
  • 2
  • 21
  • 41
pnpo
  • 361
4

You may try out listings, which in the first place provides solutions to typeset program code instead of pseudo code. Although i think that it could work out. For instance i used it in a numerical mathematics lecture to typeset Matlab code. Since Matlab is very generic and close to Pseudo, this could fit to your problem:

\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}

Now you just have to define a float environment alias of figure (packages: float, newfloat, i.e.) or use the caption package to fix that the caption of your algorithm is named "figure 1:...". With the presented solution there is only one drawback: Keywords like while,if,end,... are marked everywhere in the text, also if not needed; like in line 1 of the code. Maybe someone here knows how to work around this. In the meantime i will look this up too.

Troy
  • 13,741
Ruben
  • 13,448