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

share|improve this question
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
up vote 9 down vote accepted

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

share|improve this answer
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

share|improve this answer

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)

share|improve this answer

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

share|improve this answer

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}{%






share|improve this answer

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:












share|improve this answer

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.

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.