4

I am trying to declare a newcommand

\newcommand{\seq}[3]{#1_#2, #1_{#2 + 1} \cdots ,#1_#3}

What I want is basically to print the sequence $v_0, v_1, \cdots, v_N$ by calling \seq{v}{0}{N} but it is printing $v_0, v_{0+1}, \cdots, v_N$. Basically, I want to evaluate and use the result of expression #2 + 1.

How will I do that?

  • 2
    \newcommand{\seq}[3]{#1_#2, #1_{\the\numexpr#2 + 1\relax}, \cdots ,#1_#3} works if #2 is a number. It should probably be made more robust by checking if #2 is an integer first and print #2+1 otherwise. – moewe Mar 17 at 17:51
  • #2 is always a number, so it works. – chelsea Mar 17 at 17:53
  • Do you want it for integers only or also for other kinds of numbers, e.g. real numbers? – Ulrich Diez Mar 17 at 20:24
2

Your objective is straightforward to achieve if you can use LuaLaTeX.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}  
\newcommand{\seq}[3]{#1_{#2}, #1_{\directlua{tex.sprint(#2+1)}}, \dots ,#1_{#3}}
\begin{document}
$\seq{v}{0}{N}$
\end{document}
  • Note that I recommend using \dots (or \ldots), not \cdots. – Mico Mar 17 at 17:53
  • what's the difference between \dots and \cdots? – chelsea Mar 17 at 17:56
  • 2
    @chelsea - \cdots raises the dots above the baseline. For the problem at hand, \dots seems more appropriate from a typographic point of view. – Mico Mar 17 at 18:13
5

This checks whether #2 is an (unsigned) integer and in this case does the operation; otherwise it just appends +1.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\seq}{mmm}
 {
  \chelsea_seq:nnn { #1 } { #2 } { #3 }
 }

\cs_new_protected:Nn \chelsea_seq:nnn
 {
  #1\sb{#2}, % first item
  \regex_match:nnTF { \A [0-9]+ \Z } { #2 }
   {% #2 is a number
    #1\sb{ \int_eval:n { #2 + 1 } }
   }
   {% #2 is not a number
    #1\sb{ #2+1 }
   }
  ,\dots,#1\sb{#3}
 }

\ExplSyntaxOff

\begin{document}

$\seq{v}{0}{N}$

$\seq{v}{1}{N}$

$\seq{w}{n}{N}$

\end{document}

enter image description here

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.