3

I am very new to using LaTeX to write equation. And I am now learning how to create my own command. I am going to write a command \Listn to help me to do this:

\Listn{\sumup}{(#1)}{+}
$\sumup{n}$\\
\Listn{\sumupaf}{a_{#1}f(x_{#1})}{\times}
$\sumupaf{k+1}$

will give me

first

second

Then I try to create \Listn

\providecommand{\Define}[2]{\providecommand{#1}{}\renewcommand{#1}[1]{#2}}
\providecommand{\List}[3]{{#1{1}}#2{#1{2}}#2\cdots#2{#1{#3}}}
\providecommand{\Listn}[4]{\Define\f{#2}\Define{#1}{\List{\f}{#3}{#4}}}

But the fourth argument must be filled manually, like this:

\Listn{\sumup}{(#1)}{+}{#1}
$\sumup{n}$\\
\Listn{\sumupaf}{a_{#1}f(x_{#1})}{\times}{#1}
$\sumupaf{k+1}$

If I want to eliminate the fourth argument, I would like to have

\providecommand{\argument}{#1}

which is impossible.

Are there any method to overcome this?

2
  • 1
    Welcome to TeX.sx! Please make sure that all images are uploaded using the official stackexchange interface, i.e. the image icon on top of the text field (shortcut: CTRL+G). This ensures that all images are always accessible and do not expire. As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. Nov 26, 2011 at 14:41
  • It would be better to state what you want to achieve and your question explicitly. At the moment people have to read both from the code, which isn't that simple. Also the title is irritating. The term 'expansion' means something different in TeX. Nov 26, 2011 at 14:44

2 Answers 2

4

I think you don't really need the first argument, that is \sumup or \sumupaf:

\documentclass[a4paper]{article}
\newcommand\Listn[3]{\def\next##1{#1}\next{1}#2\next{2}#2\cdots#2\next{#3}}

\begin{document}
$\Listn{(#1)}{+}{n}$

$\Listn{a_{#1}f(x_{#1})}{\times}{k+1}$
\end{document}

Here \next is a "scratch control sequence" that will be reused and redefined without problems.

The first argument to \Listn is the "structure" of the factors, the second is the operation, and the third is the last index.

enter image description here

If you really need to give a name to the structures for subsequent usage, you can say

\newcommand\Listn[4][\listnscratch]{%
  \gdef#1##1{#2}#1{1}#3#1{2}#3\cdots#3#1{#4}}

and then $\Listn[\sumup]{(#1)}{+}{n}$ would give the same result as before, and you'll be allowed to say \sumup{3} thereon to get "(3)".

1

When you are defining a macro within another macro, the nested macro must use ##n to access its nth parameter, as #n refers to the nth parameter of the parent macro. So, you simply need to change \Listn macro to use {##1} as the last parameter allows you to eliminate the 4th parameter:

\providecommand{\Listn}[3]{\Define\f{#2}\Define{#1}{\List{\f}{#3}{##1}}}

enter image description here

\documentclass{article}

\providecommand{\Define}[2]{\providecommand{#1}{}\renewcommand{#1}[1]{#2}}
\providecommand{\List}[3]{{#1{1}}#2{#1{2}}#2\cdots#2{#1{#3}}}
\providecommand{\Listn}[3]{\Define\f{#2}\Define{#1}{\List{\f}{#3}{##1}}}

\begin{document}
\Listn{\sumup}{(#1)}{+}
$\sumup{n}$

\Listn{\sumupaf}{a_{#1}f(x_{#1})}{\times}
$\sumupaf{k+1}$
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .