# How do I print an increasing value with iteration in LaTeX?

For example, I want to print all values x + 1 with n repetition

In Python it would be:

def arithmetic_add_1_(n,x):
for k in range(n):
print(x)
x = x+1
print(x)


with n -> repetitions and x -> start number

• Welcome to TeX.SE. Please provide a MWE to clarify your problem and make yourself familiar on how to ask a question. Asking a well formulated question with enough details will increase your changes to get a good answer. Oct 28 at 0:38
• Is this question python or TeX related? Oct 28 at 0:49
• My question is related to TeX, I know how to do it in Python or other languages but I would like to know how to do it in LaTex Oct 28 at 0:53
• You can do this kind of thing in a variety of ways using expl3. Have a look at the manual which you can get by running texdoc interface3. If no one else gets there before me, I'll give an answer. Oct 28 at 0:55
• As a side comment, I would rather discourage you from doing programming of this kind in LaTeX as long as you don't have a high level of understanging TeX. It is definitely possible, but (La)TeX is a rather unforgiving as a programming language. Oct 29 at 11:01

Here's one way to do it in expl3:

\documentclass{article}

\ExplSyntaxOn
% #1: start number
% #2: number of repetitions
\cs_new_protected:Nn \debush_arithmetic_incr:nn
{
\int_set:Nn \l_tmpa_int {#1}
\int_do_while:nNnn { \l_tmpa_int } < { #1 + #2 }
{
\int_use:N \l_tmpa_int
\c_space_tl
\int_incr:N \l_tmpa_int
}
}
\NewDocumentCommand { \ArithmeticIncr } { m m }
{
\debush_arithmetic_incr:nn {#1} {#2}
}
\ExplSyntaxOff

\begin{document}
\ArithmeticIncr{10}{8}
\end{document}


• Since your function performs assignments, it should be defined with \cs_new_protected:Nn Oct 29 at 16:05

You can do that with very basic means.

\documentclass{article}
\loop
\myindex\par% replace \par by whatever allows you to separate the integers
\edef\myindex{\the\numexpr\myindex+1}%
\ifnum\myindex<\numexpr#1+#2+1\relax
\repeat}
\begin{document}
\end{document}


One could also work with counters (but there could be problems with the name space if some other routine uses this counter), and there is also tikzmath which supports a syntax that is closer to your code.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
for \k in {\x,...,\the\numexpr\x+\y}
{print{\k};};
};
}
\end{document}

• Simple and elegant without expl3.... I like your style :)
– JeT
Oct 30 at 0:12

An answer not using any pre-existing loop macro but rather simply recursing on the main print function

\documentclass{article}

\def\iter#1#2{#1\ifnum#1<#2\relax, \afterfi\iter{\the\numexpr#1+1\relax}{#2}\fi}
\def\afterfi#1\fi{\fi#1}
\begin{document}

\iter{6}{15}

\end{document}


Since you're familiar with Python, you may find the following, LuaLaTeX-based solution interesting.

\documentclass{article}
\directlua{ for j = #2 , #1+#2-1 do tex.print ( j ) end }}
\begin{document}
\end{document}

• By the way, this solution is fully expandable. Of course, whatever is in arguments 1 and 2 has to expand to an integer in order for this approach to generate meaningful results.
– Mico
Oct 30 at 9:21

You can utilize the pgffor package. See more in pgfmanual, Section 88 Repeating Things: The Foreach Statement.

\documentclass{article}
\usepackage{pgffor}
\begin{document}
This is a list of numbers:
\foreach \i in {6,7,...,30}{\i, }
this is the alphabetic list
\foreach \i in {a,...,z}{\i, }
and this is the alphabetic list in capital
\foreach \i in {A,C,...,Z}{\i, }
\end{document}


A fully expandable expl3 version:

\documentclass{article}

\ExplSyntaxOn
{
}
% #1: start number
% #2: number of repetitions
{
\int_eval:n { #1 }
\int_step_function:nnN { #1+1 } { #1+#2 } \__debush_arithmetic_print:n
}
\cs_new:Nn \__debush_arithmetic_print:n { ~#1 }
\ExplSyntaxOff

\begin{document}

\end{document}


This only adds a space between the numbers, because the first item is typeset by itself and the loop starts from the following one.

• Nice. I have never noticed these functions in the manual before. Maybe I only read up to §20.6 Integer Expression Loops… Am I right in guessing that this is more efficient than my answer (as well as being expandable)? Oct 30 at 7:09
• @DavidPurton I tested with l3benchmark and the present code is indeed more efficient than yours. Oct 30 at 8:05

I think what you re looking for ist \foreach.

Do you mean something like this:

\documentclass[border=1cm]{standalone}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \n in {10}{
\foreach \x in {3,...,\n}
{
\ifnum \x < \n
\draw (\x,0) node {\x};
\fi
\draw (\n,0) node {\n};
}
}
\end{tikzpicture}
\end{document}


• It's kind of this idea but I would like to do this with a custom start value like in my python example so how could I do this ? Oct 28 at 0:50

Another TeX solution, using the original syntax OP specified

\def\enuminterval#1#2{%
#2 \ifnum#1=0 \else
\expandafter\enuminterval
\expandafter{\the\numexpr#1-1\expandafter}\expandafter{\the\numexpr#2+1\expandafter}%
\fi
}

\enuminterval{10}{8}


Output: 8 9 10 11 12 13 14 15 16 17 18