[Tex/LaTex] Algorithm as figure and without italic and bold formatting

algorithmicxalgorithmsformatting

I want to include an algorithm in my latex document, however without printing most of it in italic and all the keywords in bold font like it's done by default by e.g. the algorithmicx package. I like this simple style:

enter image description here

(screenshot of a part of page 3 of http://research.microsoft.com/pubs/68869/naacl2k-proc-rev.pdf)

The only thing I'd like to add to this style are line numbers. Can anyone help me, how I get the formatting of the screenshot and the line numbers? Thanks 🙂

So, here is what I have until now:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{algpascal}
\begin{document}


\alglanguage{pascal}
\begin{algorithm}
\caption{Paull's algorithm}
\begin{algorithmic}[1]
\State Assign an ordering $A_{1}, ..., A_{n}$ to the nonterminals of the grammar.
\For{i = 1}{n}
\Begin
    \For{j = 1}{i-1}
    \Begin
    \State for each production of the form $A_{i} \rightarrow A_{j} \alpha$
    \End

\End

\end{algorithmic}
\end{algorithm}
\end{document}

This ends up as
enter image description here

Based on this I want the following changes:

  • do and begin shall be on the same line
  • end shall be vertically aligned to it's associated for (see first screenshot of this post).
  • Integration as a figure or at least without a black border and with a caption below the algorithm would be prefered
  • bold formatting for keywords should be turned off

Best Answer

Is this what you are trying to achieve?

enter image description here

This is the MWE:

\documentclass{article}
\usepackage[plain]{algorithm}
\usepackage{algpascal}

\begin{document}

\algrenewcommand\textkeyword{\textrm}
\algdef{SE}{For}{End}[2]{%
  \textkeyword{for} \(#1\) \textkeyword{to} \(#2\) \textkeyword{do begin}}{%
  \textkeyword{end}}

\begin{algorithm}
\begin{algorithmic}[1]
\State Assign an ordering $A_{1}, ..., A_{n}$ to the nonterminals of the grammar.
\For{i = 1}{n}
    \For{j = 1}{i-1}
    \State for each production of the form $A_{i} \rightarrow A_{j} \alpha$
    \End
\End
\end{algorithmic}
\caption{Paull's algorithm}
\end{algorithm}

\end{document}  

Explanation

  1. To match your first two requests, I've redefined the behavior of for to have an end statement by adding the lines:

    \algdef{SE}{For}{End}[2]{%
      \textkeyword{for} \(#1\) \textkeyword{to} \(#2\) \textkeyword{do begin}}{%
      \textkeyword{end}}
    
  2. To match your last request, it suffices to add the line:

    \algrenewcommand\textkeyword{\textrm}
    

    which redefines the font for keywords to be \textrm instead of \textbf.

  3. In regards of your 3rd request, there are two ways.

    • If you want the algorithm to behave as an algorithm, simply load the algorithm package with the option plain as in the above MWE:

      \usepackage[plain]{algorithm}
      
    • If you want the algorithm to behave as a figure, there is no need to load the algorithm package, simply insert the algorithmic environment inside a figure, i.e. replace the lines

      \begin{algorithm}
      \begin{algorithmic}[1]
      ...
      \end{algorithmic}
      \caption{Paull's algorithm}
      \end{algorithm}
      

      with

      \begin{figure}
      \begin{algorithmic}[1]
      ...
      \end{algorithmic}
      \caption{Paull's algorithm}
      \end{figure}
      

      and you will have

    enter image description here

Addendum

This is the complete implementation of the algorithm in the figure:

\documentclass{article}
\usepackage[plain]{algorithm}
\usepackage{algpascal}

\begin{document}

\algrenewcommand\textkeyword{\textrm}
\algdef{SE}{For}{End}[2]{%
  \textkeyword{for} \(#1\) \textkeyword{to} \(#2\) \textkeyword{do begin}}{%
  \textkeyword{end}}
\algdef{SE}{ForEach}{End}[1]{%
  \textkeyword{for each} #1 \textkeyword{do begin}}{%
  \textkeyword{end}}

\begin{algorithm}
\begin{algorithmic}[1]
\State Assign an ordering $A_{1}, \dots, A_{n}$ to the nonterminals of the grammar.
\For{i := 1}{n}
    \For{j := 1}{i-1}
        \ForEach{production of the form $A_{i} \rightarrow A_{j} \alpha$}
            \State remove $A_{i} \rightarrow A_{j} \alpha$ from the grammar
            \ForEach{production of the form $A_{j} \rightarrow \beta$}
                \State add $A_{i} \rightarrow \beta\alpha$ to the grammar
            \End
        \End
    \End
    \State transform the $A_{i}$-productions to eliminate direct left recursion
\End
\end{algorithmic}
\caption{Paull's algorithm}
\end{algorithm}

\end{document}  

enter image description here

There is the need to define a new command \ForEach:

\algdef{SE}{ForEach}{End}[1]{%
  \textkeyword{for each} #1 \textkeyword{do begin}}{%
  \textkeyword{end}}

Note that I've defined \ForEach so to take one "text" argument, because it seemed to me the best way to define it.

If you want it to take a "math" argument, then define it as

\algdef{SE}{ForEach}{End}[1]{%
  \textkeyword{for each} \(#1\) \textkeyword{do begin}}{%
  \textkeyword{end}}

and use it as follows (amsmath is needed for the command \text):

\ForEach{\text{production of the form }A_{i} \rightarrow A_{j} \alpha}
Related Question