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

I want to write a 'for' statement, but unsure how to do this with LaTeX. What I am trying to do is:

for k = 1, k++, while k < i

What is the proper (or at least a decent way) to write this in LaTeX?

Let me re-phrase:

I do not want to actually create a loop, I just want to print a line that will be the text of the beginning of a for-loop. I'm unsure how to do this.

share|improve this question
We want to help you, but unsure how to help you as your statement is difficult to be parsed. Please provide a more detailed illustration. – kiss my armpit May 22 '12 at 5:34
up vote 6 down vote accepted

If you just want to print the statement, then something like

\text{for $k = 1$, $k{+}{+}$, while $k < i$

will set it in a displayed format (remember to call \usepackage{amsmath} in the preamble). If it's as an item in an enumerate, then

\item for $k = 1$, $k{+}{+}$, while $k < i$

is sufficient.

The only subtle point is to enclose the + symbols between braces, in order to avoid undesired spacings.

share|improve this answer

If you're interested in typesetting algorithmic code, there are a number of choices. You can use a pseudocode environment algpseudocode offered by algorithmicx. Here's a short example from the algorithmicx documentation (with a pseudocode for loop added):

enter image description here

\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
  \caption{Euclid’s algorithm}\label{euclid}
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \For{\texttt{<some condition>}}
        \State \texttt{<do stuff>}
      \State \textbf{return} $b$\Comment{The gcd is b}

The algorithms bundle supplied the algorithm floating environment.

Alternatively, the de-facto program typesetting package is listings. The examples, usage and language support is vast. An abundance of examples is contained within the listings documentation.

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.