# Writing sequence terms

What would be the best way to write out the terms of a sequence, somehow like this, but preferably aligned?

Is there a way to automatically generate the terms from an equation i.e. with n² for sequence (3)?

If so, how could one write the relation between the terms and their indexes like so:

• You can generate the sequences automatically as TeX is a programming language. Google something like “TeX compute sequence” and see what you get. – Zoxume Mar 14 '16 at 13:58

The following is for automated generation. It wasn't clear what "aligned" was supposed to mean. Thus I have applied various methods.

\documentclass[a4paper]{article}
\usepackage[hscale=0.8]{geometry}
\usepackage{amsmath}

\usepackage{xintexpr}

\usepackage{array}

\makeatletter
\let\gobble\@gobble
\makeatother

\begin{document}

\begin{align}
\xinttheexpr seq(2^n, n=0..15)\relax, \dots\\
%
\xinttheexpr seq(2n+1, n=2..17)\relax, \dots\\
%
\xinttheexpr seq(n^2, n=1..16)\relax, \dots\\
%
\edef\L{\xinttheiiexpr seq(subs((n=m^2)?{m}{-n}, m=sqrt(n)), n=1..16)\relax}%
\xintFor #1 in \L\do{\ifnum #1<0 \sqrt{\gobble #1}\else #1\fi, }\dots\\
%
\edef\L{\xinttheexpr seq(n/(n+1), n=1..16)\relax}%
\xintFor #1 in \L\do{\xintFrac {#1}, }\dots\\
%
\xinttheexpr rseq(2; @-2, i=1..15)\relax, \dots\\
1%
\edef\L{\xinttheexpr seq(1/n, n=1..16)\relax}%
\xintFor #1 in \L\do {\xintFrac{#1}, }\dots\\
%
\edef\L{\xinttheexpr seq(n, n=1..16)\relax}%
\xintFor #1 in \L\do{\ifodd #1 #1\else\xintFrac{1/#1}\fi, }\dots\\
%
\edef\L{\xinttheexpr seq((1/n,-1/n),n=1..[2]..16)\relax}%
\xintFor #1 in \L\do{\xintSignedFrac{#1}, }\dots\\
%
\edef\L{\xinttheexpr seq((1/n,(n+1)/(n+2)), n=1..[2]..16)\relax}%
\xintFor #1 in \L\do{\xintFrac{#1}, }\dots\\
%
\edef\L{\xinttheexpr seq(x, x=1..12)\relax}%
\xintFor #1 in \L\do
{\begin{smallmatrix}
\xinttheexpr 2^(#1-1)\relax\\\uparrow\\ #1
\end{smallmatrix}\;}%
\begin{smallmatrix}
\dots\\ \\\dots
\end{smallmatrix}\;%
\begin{smallmatrix}
2^{n-1}&\dots&\text{ (terms of the sequence)}\hfill\\
\uparrow &&\\
n&\dots&\text{ (position numbers of the terms)}
\end{smallmatrix}\\
%
\edef\L{\xinttheexpr rseq(1; @+i^2, i=2..16)\relax}%
\edef\M{\xinttheexpr seq(reduce(n(n+1)(2n+1)/6), n=1..16)\relax}%
\edef\N{\xinttheexpr seq(n, n=1..16)\relax}%
\def\arraystretch{1.5}%
\begin{array}{*{17}c}
\xintFor #1 in \L\do{#1&}\sum_{i=1}^n i^2\\
\xintFor #1 in \M\do{#1&}\frac{n(n+1)(2n+1)}{6}\\
\xintFor #1 in \N\do{#1&}n\\
\end{array}
\end{align}

\end{document}


The \xintFor is used only when some formatting is needed, for example printing fractions as A/B is not enough one wants \frac{A}{B}, or when one wants to replace the commas by tab stops for alignment.

Technical note: \xintFor works with comma separated values; the list may be encapsulated in a macro, but this macro is expanded only once. Hence, for simplicity we first construct the list by an \edef (we could have use a \romannumeral-0 prefix rather).

Second technical note: the list (4) of square roots uses the trick to prefix the number with a sign if it is not found to be a perfect square. Then the formatting checks for this sign.

The list numbered (10) in OP seems to have an issue. I don't understand the 4/4. A 4/5 fits well with the rest, though.

Use the align environment provided by the amsmath package:

Numbered:

\begin{align} % or \begin{aligned} 1, && 2, && 4, && 8, && 16, && 32, && 64, && 128, && \ldots \\ 1, && \sqrt2, && \sqrt3, && 2, && \sqrt5, && \sqrt6, && \sqrt7, && 2\sqrt2, && \ldots \end{align} % or \end{aligned}


Not numbered:

\begin{align*} % or \begin{aligned} 1, && 2, && 4, && 8, && 16, && 32, && 64, && 128, && \ldots \\ 1, && \sqrt2, && \sqrt3, && 2, && \sqrt5, && \sqrt6, && \sqrt7, && 2\sqrt2, && \ldots \end{align*} % or \end{aligned}


The align environment replaces $...$ and equivalent. If you still want $...$, you can use the aligned environment inside it.

The double &s are necessary to have the good alignment. Actually, there is a blank column between each number (columns alternates with right and left alignment and we skip one kind of alignment.) If you want, you can put the commas in the blank column instead of right after the numbers.

If you want the arrows, just add them directly like the numbers:

\begin{align*} % or \begin{aligned} 1 &,& 2 &,& 4 &,& 8 &,& 16 &,& 32 &,& \ldots && 2^{n-1} && \ldots & \quad\text{(terms of the sequence)} \\ \uparrow && \uparrow && \uparrow && \uparrow && \uparrow && \uparrow && && \uparrow \\ 1 &,& 2 &,& 3 &,& 4 &,& 5 &,& 6 &,& \ldots && n && \ldots & \quad\text{(position numbers of the terms)} \end{align*} % or \end{aligned}


Feel free to check the amsmath documentation to learn more about the align environment.

Here is the result of the three codes:

• Wouldn't alignat environment be better suited for this? – Aditya Mar 14 '16 at 14:23
• @Aditya I tried to do something with alignat but I can’t get something. Could you be more explicit ore write your own answer? – Zoxume Mar 14 '16 at 15:52

(I write a new answer as I present a different approach.)

It is possible to see your examples as tables or, more precisely in math-mode, an array. Especially the last one in my opinion:

Hence, using the array environment, you can control more precisely and more easily the alignment of each column. Here is how you can reproduce the relation between the terms and their indexes:

$\begin{array}{cccccccccl} 1 & 2 & 4 & 8 & 16 & 32 & \ldots & 2^{n-1} & \ldots & \text{...} \\ \uparrow & \uparrow & \uparrow & \uparrow & \uparrow & \uparrow & & \uparrow & & \\ 1 & 2 & 3 & 4 & 5 & 6 & \ldots & n & \ldots & \text{...} \end{array}$


Here I used c-type columns but to copy the picture above you will need l`-type columns.