55

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.

ngramsky
  • 2,363
  • 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 05:34
  • Possibly you are looking for this: https://riptutorial.com/latex/example/28657/loops---repeating-things – Nasir Haniff Jun 02 '21 at 02:34

5 Answers5

76

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

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithm}
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \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$
      \EndWhile\label{euclidendwhile}
      \For{\texttt{<some condition>}}
        \State \texttt{<do stuff>}
      \EndFor
      \State \Return $b$\Comment{The gcd is b}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{document}

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.

Joeytje50
  • 205
Werner
  • 603,163
  • 1
    For line \State \textbf{return} $b$\Comment{The gcd is b}, is there a reason you did not use the \Return keyword? – ZaydH May 03 '19 at 04:26
  • 1
    @ZaydH: There is no \Return defined by default. – Werner May 03 '19 at 04:58
  • 1
    maybe a dumb question -- but could you please explain more? When I take the code you provided and change \textbf{return} to \Return, it compiles for me. Is this going to be system specific? (I am running a very recent version of MacTex) I am sure there is something I am missing and would like to understand more. – ZaydH May 03 '19 at 05:08
  • 1
    @ZaydH: If you look at the algorithmicx documentation you'll see that I just copied the code from the first example listed. I briefly looked at the code of algpseudocode.sty and could see a \Result; I may have missed it. If it works now, that's fine. – Werner May 03 '19 at 05:13
  • 2
    @ZaydH: Just saw that \Return is defined within algpseudocode.sty. – Werner May 03 '19 at 15:41
  • Ok thank you for the clarification. Given your score, I figured I was missing something. Appreciate the extra check. – ZaydH May 03 '19 at 21:37
  • Thanks a lot! Time savior) – Mohamad Al Mdfaa May 17 '22 at 09:30
16

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.

egreg
  • 1,121,712
11
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}

\begin{document}

\begin{algorithm}[H] 
\caption{Sum of Array Elements}
\label{alg:loop}
\begin{algorithmic}[1]
\Require{$A_{1} \dots A_{N}$} 
\Ensure{$Sum$ (sum of values in the array)}
\Statex
\Function{Loop}{$A[\;]$}
  \State {$Sum$ $\gets$ {$0$}}
    \State {$N$ $\gets$ {$length(A)$}}
    \For{$k \gets 1$ to $N$}                    
        \State {$Sum$ $\gets$ {$Sum + A_{k}$}}
    \EndFor
    \State \Return {$Sum$}
\EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

1

With the package algorithm2e, we can rewrite the example provided above like the following in LaTeX:

\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}

\begin{algorithm}[H] \SetKwInOut{Input}{input}\SetKwInOut{Output}{output} \Input{$A_{1} \dots A_{N}$} \Output{$Sum$ (sum of values in the array)} \DontPrintSemicolon \caption{Sum of Array Elements} \label{alg:loop} \SetKwFunction{FLoop}{Loop} \SetKwProg{Fn}{Function}{}{} \BlankLine \Fn{\FLoop{$A[;]$}}{ \State {$Sum$ $\gets$ {$0$}} ; \State {$N$ $\gets$ {$length(A)$}} ; \For{$k \gets 1$ to $N$} { \State {$Sum$ $\gets$ {$Sum + A_{k}$}} } \KwRet {$Sum$} } \end{algorithm}

\end{document}

with the following output:

enter image description here

0

To the example given by Werner above. It did not work for me until I included braces as if I'm coding. Like this:

\For{\texttt{<some condition>}} {
        \State \texttt{<do stuff>}
      \EndFor
}
Werner
  • 603,163
Ntate
  • 11
  • 2
    The syntax of algorithmicx doesn't provide a second argument for \For. It only takes one argument. If you're using it this way, you're probably mixing algorithm2e's syntax with algorithmicx's constructions. Don't mix them - use one or the other. – Werner Mar 27 '18 at 22:29