Aligning pseudocode in LaTeX

My algorithms class is following strict guidelines for pseudocode. Let's say I have something like the following:

y = 0
for i = n downto 0
y = a_i + x * y


I want this to appear left-aligned on the page and have line numbering. The algorithmic package seems close to what I want, but it doesn't follow my class's pseudocode conventions. Is there a really simple way to hack this in to LaTeX?

I am just learning LaTeX, so I apologize if this is a simple question.

-

migrated from stackoverflow.comSep 12 '11 at 0:13

This question came from our site for professional and enthusiast programmers.

You might be interested in: Print programs with its proper syntax – Werner Sep 12 '11 at 4:18

If I were you and I was absolutely sure that the algorithmic package is not the way to go, I'd use something like this:

\begin{align*}
&y = 0 \\
&\text{for $i = n$ downto 0} \\
& \hspace{1cm} y = a_i + x * y
\end{align*}


This results in

-
So after trying the other packages listed here, I've found too many issues. This one seems to be the best for code and embedding some math (as I will need this later). Thanks for the answers, everyone. – Kizaru Sep 13 '11 at 15:23

I have never used an algorithm package in Latex, what I'd do is use the verbatim environment and manually indent the code in the text editor.

\begin{verbatim}
1 y=0
2 for i = n downto 0
3     y = a_i + x * y
\end{verbatim}


This will put exactly what you type in the environment in the output, including whitespace. If you need fancy Latex (like subscript and such) check the alltt package.

Info on Verbatim

-

Edit 2: Sorry, from your code excerpt and another answer, I somehow thought you wanted verbatim text.

I have had success using algorithmicx. It is customizable for your needs.

Documentation (PDF)

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{algorithm}
\begin{algorithmic}
\State $y \gets 0$
\For{$i \gets n, 0$}
\State $y \gets a_i + xy$
\EndFor
\end{algorithmic}
\caption{Example algorithm.}
\label{examplealgorithm}
\end{algorithm}


Edit 1: Looking through some of my old reports, here are two example customizations for the Verbatim environment. See the documentation PDF (link below) for more details.

\usepackage{fancyvrb}
\usepackage{color}
\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{fontsize=\small,
frame=single, rulecolor=\color{blue}, framerule=0.5mm, framesep=1mm,
xleftmargin=2mm}
%\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{fontsize=\scriptsize,
numbers=left, numbersep=8pt, baselinestretch=1, xleftmargin=5mm,
commandchars=\~\{\}}

\begin{Verbatim}
y = 0
for i = n downto 0
y = a_i + x * y
\end{Verbatim}


Original: I recommend the Verbatim environment from the fancyvrb package, not to be confused with the built-in verbatim environment. It does exactly what you need.

Documentation. (PDF)

-

Depending on exactly what you want you could use the listings package,

\documentclass{article}
\usepackage{listings}%
\usepackage{xcolor}

\lstset{%
backgroundcolor=\color{yellow},%
basicstyle=\small\ttfamily,%
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%
}%

\lstset{emph={%
downto, for%
},emphstyle={\color{red}\bfseries\underbar}%
}%

\begin{document}
\begin{lstlisting}
y = 0
for i = n downto 0
y = a_i + x * y
\end{lstlisting}
\end{document}


The first lstset above sets the options in terms of the background color, the font family and what kind of numbering to use.

Add any keywords you want highlighted in the second lstset, or comment this out if you don't want that. Or add additional lstset if you want different groups of words highlighted differently.

If you need the code to by typeset then you can use the algorithmic package as follows:

\documentclass{article}
\usepackage{algorithmic}

\newcommand{\downto}{\to}% Select what you want to use for this
\renewcommand{\algorithmicdo}{}% Since you don't want the DO at end of FOR
\begin{document}
\begin{algorithmic}[1]% Number every line
\FOR{$i = n \downto 0$}
\STATE $y = a_i + x * y$
\ENDFOR
\end{algorithmic}
\end{document}


If you don't want the end for you can use \renewcommand{\algorithmicendfor}{}

-

As the original poster doesn't mention what exactly are the problems with algorithmic ("it doesn't follow my class's pseudocode conventions"), it is hard to be more precise.

Trying to infer what may not be your conventions, it seems that you don't want the end if, end for, etc parts and that's what's the [noend] option (when loading the package, as in \usepackage[noend]{algorithmic}) is for: it lets you save vertical space, which is especially important for submitting papers with strict page limits.

I would recommend that you increase the default horizontal indentation from 1em to, say, 2em if you omit the end markers. You can use \algsetup{indent=2em} at any time or pass the indent=2em option when loading the package.

-