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:
(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
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?
This is the MWE:
Explanation
To match your first two requests, I've redefined the behavior of
for
to have anend
statement by adding the lines:To match your last request, it suffices to add the line:
which redefines the font for keywords to be
\textrm
instead of\textbf
.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 optionplain
as in the above MWE:If you want the algorithm to behave as a figure, there is no need to load the
algorithm
package, simply insert thealgorithmic
environment inside afigure
, i.e. replace the lineswith
and you will have
Addendum
This is the complete implementation of the algorithm in the figure:
There is the need to define a new command
\ForEach
: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
and use it as follows (
amsmath
is needed for the command\text
):