3

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

5
  • 3
    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.
    – Roland
    Oct 28 at 0:38
  • 1
    Is this question python or TeX related?
    – Roland
    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
    – Debush
    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
5

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}

output

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

You can do that with very basic means.

\documentclass{article}
\newcommand{\ArithmeticAdd}[2]{\edef\myindex{#1}%
\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}
\ArithmeticAdd{12}{7}
\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}
\tikzmath{function arithmeticadd(\x,\y) {int \k;
     for \k in {\x,...,\the\numexpr\x+\y}
     {print{\k};}; 
  };
  arithmeticadd(12,7);
  }
\end{document}
1
  • 1
    Simple and elegant without expl3.... I like your style :)
    – JeT
    Oct 30 at 0:12
9

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

enter image description here

\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}
8

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

enter image description here

\documentclass{article}
\newcommand\ArithmeticAdd[2]{%
   \directlua{ for j = #2 , #1+#2-1 do tex.print ( j ) end }}
\begin{document}
\ArithmeticAdd{8}{10}
\end{document}
1
  • 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
6

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

enter image description here

\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}
6

A fully expandable expl3 version:

\documentclass{article}

\ExplSyntaxOn
\NewExpandableDocumentCommand {\arithmeticadd} { mm }
 {
  \debush_arithmetic_add:nn {#1} {#2}
 }
% #1: start number
% #2: number of repetitions
\cs_new:Nn \debush_arithmetic_add:nn
 {
  \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}

\arithmeticadd{10}{7}

\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.

enter image description here

2
  • 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
  • 1
    @DavidPurton I tested with l3benchmark and the present code is indeed more efficient than yours.
    – egreg
    Oct 30 at 8:05
4

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}

enter image description here

1
  • 1
    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 ?
    – Debush
    Oct 28 at 0:50
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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