TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from stackoverflow.com Sep 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
up vote 4 down vote accepted

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:

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

This results in

rendering of the above LaTeX snippet

share|improve this answer
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.

1 y=0
2 for i = n downto 0
3     y = a_i + x * y

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

share|improve this answer

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)


\State $y \gets 0$
\For{$i \gets n, 0$}
  \State $y \gets a_i + xy$
\caption{Example 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.

   frame=single, rulecolor=\color{blue}, framerule=0.5mm, framesep=1mm, 
   numbers=left, numbersep=8pt, baselinestretch=1, xleftmargin=5mm, 

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

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)

share|improve this answer

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

listings output

or the algorithms package

algorithms output


    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%

    downto, for%

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

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:


\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{algorithmic}[1]% Number every line
\FOR{$i = n \downto 0$}
    \STATE $y = a_i + x * y$

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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