10

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
12
+50

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}
  • wow, thank you so much for that detailed reply! Unfortunately there is one problem with this: multiple lines/states in a for loop won't work. I've added an example at the end of my original post. Would be glad if you could have a look at this, thanks! – stefan.at.wpf Aug 18 '13 at 9:28
  • @stefan.at.wpf You're welcome! – karlkoeller Aug 18 '13 at 10:00
  • thank you karl, I solved it. I am just wondering, if it's possible to have a "for each" where the item (fo each item in items) isn't printed in italic (while in a "for" loop the "i" should remain printed in italic). reconfiguring textrm would change it everywhere. maybe you can help me with this instead, would be great :-) – stefan.at.wpf Aug 19 '13 at 19:43
  • @stefan.at.wpf Yes, I'll help you, but please wait for tomorrow. – karlkoeller Aug 19 '13 at 19:45
  • yes, of course :-) – stefan.at.wpf Aug 19 '13 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.