Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

 n0+n1+n2+n3...+nn

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
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
add comment

7 Answers 7

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:

  \series[5]{nseries}{n}

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.

\documentclass{article}
\usepackage{amsmath}
\parindent=0pt \parskip=6pt
\begin{document}
\newcount\ct

\newcommand\series[3][3]{%
  \ct=0
  \expandafter\xdef\csname #2\endcsname{}
  \def\temp{\csname#2\endcsname}
  \loop
    \def\Z{#3_{\the\ct}+}
    \expandafter\xdef\csname #2\endcsname{\temp\Z}
    \advance\ct by1
    \ifnum \the\ct<#1
  \repeat
 %% add the ldots by redefining the command again
 \expandafter\xdef\csname#2\endcsname{\temp\cdots+#3_n}
}
% Define some series
\series[8]{kseries}{{\text{k}}t^2}
\series[5]{nseries}{n}
\series[5]{tseries}{t}
\series{zetaseries}{\zeta}
% call the series
The \texttt{nseries} \[\nseries\]
The \texttt{tseries} \[\tseries\]
The \texttt{kseries} \[\kseries\]
The \texttt{\(\zeta\)-series} \[\zetaseries\]
\end{document}

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
add comment
\documentclass{article}
\usepackage{pstricks}
\def\series#1{%
  \ifcase#1{n0+}\or{n0+n1+}\else
  \psforeach{\nA}{0,1,..,#1}{n\nA+}\fi
  \ldots+nn}
\begin{document}
\series{0}

$\series{1}$

$\series{11}$

\end{document}

enter image description here

share|improve this answer
add comment

You could do it using a recursive expanding macro like:

\documentclass{article}

\usepackage{amsmath}

\newcommand*{\series}[2]{% #1 = variable, #2 = max. number of seriess
    \ensuremath{%
       \iftrue\seriesx{0}{#2}{#1}\fi
       \dots+{#1}_{n}%
    }%
}

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

\begin{document}

\series{n}{10}

\series{n}{22}

\series{n}{0}

\series{n}{-1}

\series{n}{1}

\series{m}{5}

\end{document}

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
add comment

With foreach and pgfkeys

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{fourier}   
\usepackage{pgffor,pgfkeys}

\makeatletter
\pgfkeys{%
series/.cd,
var/.code    =  \def\ser@var{#1},
op/.code     =  \def\ser@op{#1},
nb/.code     =  \def\ser@tot{#1}, 
indice/.code =  \def\ser@in{#1} 
} 

\def\series{\@ifnextchar[{\@series}{\@series[]}} 

\def\@series[#1]{%
\begingroup 
\pgfkeys{/series/.cd,
var=x,
op=+,
nb=2,
indice=n}    
\pgfqkeys{/series}{#1} 
\foreach \indice in {1,...,\ser@tot}{%
\ser@var_{\indice}\ser@op}%
\dots\ser@op\ser@var_{\ser@in} 
\endgroup } 
\makeatother

\begin{document}

\begin{enumerate}
\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 
\def\ttenseries{\series[var=t,nb=10]} 

\[ \ttenseries \] 

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

\end{enumerate}
\end{document}

enter image description here

share|improve this answer
add comment

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.

\documentclass{article}

\usepackage{pgffor}

\newcommand*{\series}[2]{%
    {#1}_0%
    \ifnum#2>0
    \foreach \n in {1,...,#2}{%
        +{#1}_{\n}%
    }%
    \fi
    +\dots+{#1}_{n}%
}

\begin{document}

$\series{n}{6}$

$\series{m}{8}$

$\series{n}{0}$

$\series{m}{1}$

\end{document}
share|improve this answer
add comment

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:

\documentclass{article}
\newcounter{mycount}
\usepackage{amsmath}
\usepackage{ifthen}

\newcommand*\Series[2][n]{%
  \setcounter{mycount}{0}%
  \whiledo{\themycount<#2}
    {\ensuremath{#1_{\themycount}+{}}\stepcounter{mycount}}%
  \ensuremath{#1_{\themycount}}
}

\newcommand*\SeriesT[3][n]{%
  \setcounter{mycount}{0}%
  \whiledo{\themycount<#2}
    {\ensuremath{#1_{\themycount}+{}}\stepcounter{mycount}}%
  \ensuremath{#1_{\themycount}+\cdots+#1_{#3}}
}

\begin{document}

$\Series{5}$

$\Series[m]{10}$

$\Series[t^2]{7}$

$\SeriesT{7}{k}$

$\SeriesT[m]{3}{n}$

$\SeriesT[t^2]{1}{l}$

\end{document}

share|improve this answer
add comment

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.

\documentclass{article}
\usepackage{pgfkeys,pgffor,amsmath}

\pgfkeys{
 /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}}
  },
}

\newcommand\Sequence[1][]{%
 \pgfkeys{/sequence, default, #1, utility/do prefix, utility/end sequence}%
}

\begin{document}
 \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]
\end{document}

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
add comment

Your Answer

 
discard

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.