I have been trying to write a macro where I can specify the number of terms of a series and it would then print:


It is important for my work that I can print any number of terms. I have tried but loops are beyond my current understanding of TeX.

Thanks in advance for the help.

  • 1
    Should the ... be also printed like shown or all terms without the dots? – Martin Scharrer Apr 1 '11 at 15:12
  • @Martin with ... please. Thanks for your answers. – mathspasha Apr 1 '11 at 18:29

This is a slightly different solution to that offered by Martin generalizing the problem. In order to provide some flexibility, I would first define a "setup" macro that you can call as:


The reason, why I would generalize it is that you might not only want "n-series", but you may want to define other type of series.

This setup command would create automaticaly a command called \<symbol>series that will typeset the required number of terms. The process of creating a command uses \csname..\endcsname. You may wish to look it up further in the TeXbook or Tex by Topic.

It is also possible to develop the macro further for more difficult series, by changing the definition of \Z in the MWE below.

\parindent=0pt \parskip=6pt

  \expandafter\xdef\csname #2\endcsname{}
    \expandafter\xdef\csname #2\endcsname{\temp\Z}
    \advance\ct by1
    \ifnum \the\ct<#1
 %% add the ldots by redefining the command again
% Define some series
% call the series
The \texttt{nseries} \[\nseries\]
The \texttt{tseries} \[\tseries\]
The \texttt{kseries} \[\kseries\]
The \texttt{\(\zeta\)-series} \[\zetaseries\]

Sample output,

enter image description here

  • that should probably be \dots not \ldots as the line dots a not suitable for a continuation of binary operations – daleif Apr 1 '11 at 15:05
  • @daleif It shouldn't really make a difference as in the LaTeX kernel \ldots is defined as \let\ldots\dots. I guess it would have been better to use \mathellipsis. – Yiannis Lazarides Apr 1 '11 at 15:53
  • using \cdots is another option. – Gonzalo Medina Apr 1 '11 at 15:59
  • @Gonzalo My bad should be \cdots, edited the code. – Yiannis Lazarides Apr 1 '11 at 17:35




enter image description here


You could do it using a recursive expanding macro like:



\newcommand*{\series}[2]{% #1 = variable, #2 = max. number of seriess

\newcommand*{\seriesx}[4]{% #1 = current index, #2 = max index, #3 = variable, #4 = \fi
    #4% = \fi
    \ifnum#1>#2 \else









The recursive macro \termx reads the trailing \fi as last argument and inserts it at the beginning to avoid a deep nesting of conditionals. It works without it, but is better with it. There are also other ways to do that.

(Updated to include +...+X_n part; renamed macro)


With foreach and pgfkeys


var/.code    =  \def\ser@var{#1},
op/.code     =  \def\ser@op{#1},
nb/.code     =  \def\ser@tot{#1}, 
indice/.code =  \def\ser@in{#1} 


\foreach \indice in {1,...,\ser@tot}{%
\endgroup } 


\item display math
 \[  \series  \]
\item inline math :   $\series$ 
\item option \texttt{nb=10} and \texttt{nb=1}

   $ \series[nb=6]  $ and $  \series[nb=1]   $
\item option \texttt{indice=$i$} 
     \[  \series[nb=10,indice=i]   \] 
\item option \texttt{var=$k^2$} 
     \[  \series[nb=10,var=k^2]   \]
\item define a serie 

\[ \ttenseries \] 

\item option \texttt{op}
 \[  \series[op=\times]  \] 


enter image description here


Here a solution which uses a \foreach loop. I'm still not sure about was the OP exactly wants, but to follow the other answers I added the trailing +...+n_n part. Simple remove the \dots+{#1}_{n}% line if this isn't wanted.



    \foreach \n in {1,...,#2}{%







Another solution, this one using \whiledo from the ifthen package:

EDIT: I added another definition to take care of series with not all the terms explicitly exhibited:













This is not a timely answer, and I see there are already two (!) pgffor answers, one of them also using pgfkeys, but I want to suggest a more compact alternative construction along those lines.


 /sequence/.is family, /sequence,
 default/.style = {
  start at/.initial = 1,
  through/.initial = 1,
  index/.initial = n,
  variable/.initial = a,
 utility/end sequence/.code = {${}\dots+\pgfkeysalso{variable}_{\pgfkeysalso{index}}$},
 utility/loop/.code = {$\pgfkeysalso{variable}_{#1}+$},
 utility/do prefix/.style =
  {utility/loop/.list =
   {\pgfkeysvalueof{/sequence/start at},...,\pgfkeysvalueof{/sequence/through}}

 \pgfkeys{/sequence, default, #1, utility/do prefix, utility/end sequence}%


 \Sequence[through = 3]

 \Sequence[index = m]

 \Sequence[variable = x]

 \Sequence[start at = 0]

 \Sequence[start at = -2, through = 3, variable = A^2, index = k]

I use the pgfkeys handler .list to do the looping; this avoids having to write any explicit code at all. For the unfamiliar: <key>/.list = {<list of values>} simply calls <key> = <value> repeatedly for each <value> in <list of values>, and uses the \foreach syntax for ranges (actually, it uses \foreach internally).

Some experimentation with \pgfkeysalso and \pgfkeysvalueof showed that just writing \pgfkeysalso{variable} (for example) was sufficient to get the value of /sequence/variable typeset, but not in an expandable way. \pgfkeysvalueof is expandable, and the list in \foreach needs to be expandable, hence the awkward change of style there.

My \Sequence also takes key-value arguments rather than possibly-unclear braced arguments and provides a default that (in my professional opinion) most mathematicians would consider the reasonable one. Of course, you can just change default/.style to what you think is reasonable.

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.