
答え1
\N
シンボルを置き換えるように記述していただける場合は、次の解決策を提案できます。
\documentclass{article}
\usepackage{amsmath}
\newcommand{\macro}[2][]{
\def\N{1} #2
+
\def\N{2} #2
+\cdots+
\def\N{#1} #2
}
\begin{document}
\verb|\macro[N]{x^\N y^\N}| gives $\macro[N]{x^\N y^\N}$
\bigskip\verb|\macro[N]{x^\N y_\N}| gives $\macro[N]{x^\N y_\N}$
\bigskip\verb|\macro[M]{(x^\N y_\N)^2}| gives $\macro[M]{(x^\N y_\N)^2}$
\end{document}
答え2
補助マクロを定義して置換を行うことができます。
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\makesum}{O{N}m}
{
\akkapelli_makesum:nn { #1 } { #2 }
}
\tl_new:N \l__akkapelli_makesum_template_tl
\cs_generate_variant:Nn \cs_set_protected:Nn { NV }
\cs_new_protected:Nn \akkapelli_makesum:nn
{
\tl_set:Nn \l__akkapelli_makesum_template_tl { #2 }
\tl_replace_all:Nnn \l__akkapelli_makesum_template_tl { #1 } { {##1} }
\cs_set_protected:NV \__akkapelli_makesum_do:n \l__akkapelli_makesum_template_tl
\__akkapelli_makesum_do:n { 1 } + \__akkapelli_makesum_do:n { 2 } + \dots + \__akkapelli_makesum_do:n { #1 }
}
\ExplSyntaxOff
\begin{document}
$\makesum{x^Ny^N}$
$\makesum{x^Ny_N}$
$\makesum[M]{(x^M y_M)^2}$
\end{document}
オプション引数 (デフォルトN
) は 2 番目の引数で に置き換えられる#1
ため、それに基づいてマクロを定義して使用することができます。
#1
しかし、私は別のアプローチを好みます。 「変数部分」に使用する 2 番目の引数では次のようになります。
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\makesum}{O{N}m}
{
\akkapelli_makesum:nn { #1 } { #2 }
}
\cs_new_protected:Nn \akkapelli_makesum:nn
{
\cs_set_protected:Nn \__akkapelli_makesum_do:n { #2 }
\__akkapelli_makesum_do:n { 1 } + \__akkapelli_makesum_do:n { 2 } + \dots + \__akkapelli_makesum_do:n { #1 }
}
\ExplSyntaxOff
\begin{document}
$\makesum{x^#1y^#1}$
$\makesum{x^#1y_#1}$
$\makesum[M]{(x^#1 y_#1)^2}$
$\makesum[100]{(x^{#1} y_{#1})^2}$
\end{document}
最後のケースでは中括弧が必要であることに注意してください。
整数指数を使用する場合は、「小さい」ケースに対処する方が適切です。
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\makesum}{O{N}m}
{
\akkapelli_makesum:nn { #1 } { #2 }
}
\cs_new_protected:Nn \akkapelli_makesum:nn
{
\cs_set_protected:Nn \__akkapelli_makesum_do:n { #2 }
\regex_match:nnTF { \A [0-9]* \Z } { #1 }
{% numeric argument
\int_case:nnF { #1 }
{
{0}{0}
{1}{\__akkapelli_makesum_do:n { 1 }}
{2}{\__akkapelli_makesum_do:n { 1 } + \__akkapelli_makesum_do:n { 2 }}
{3}{\__akkapelli_makesum_do:n { 1 } + \__akkapelli_makesum_do:n { 2 } + \__akkapelli_makesum_do:n { 3 }}
}
{ \__akkapelli_makesum_generic:n { #1 } }
}
{ \__akkapelli_makesum_generic:n { #1 } }
}
\cs_new_protected:Nn \__akkapelli_makesum_generic:n
{
\__akkapelli_makesum_do:n { 1 } + \__akkapelli_makesum_do:n { 2 } + \dots + \__akkapelli_makesum_do:n { #1 }
}
\ExplSyntaxOff
\begin{document}
$\makesum{x^#1y^#1}$
$\makesum{x^#1y_#1}$
$\makesum[M]{(x^#1 y_#1)^2}$
$\makesum[1]{x^{#1} y_{#1}}$
$\makesum[2]{x^{#1} y_{#1}}$
$\makesum[3]{x^{#1} y_{#1}}$
$\makesum[4]{x^{#1} y_{#1}}$
$\makesum[100]{(x^{#1} y_{#1})^2}$
\end{document}
答え3
このソリューションは、純粋に (e-)TeX のソリューションです。 と同じアイデアを使用しますΟὖτις
が、入力が制御シーケンスである必要はありません (入力は文字である必要があります)。 オプション引数 の目的がわからないため、オプション引数 を削除しましたが、オプション引数 を使用するように意図されていた場合は、 をにegreg
変更するだけで済みます。オプション引数 はデフォルトで です。\def
\newcommand
N
\def\macro#1#2{{%
\everyeof={}% So nothing weird happens at the end of \scantokens
\catcode`#1=\active% Make the parameter an active character
\scantokens{% Retokenize the following code so that #1's catcode is changed
\def#1{1}#2\def#1{2}+#2% Print a_1+a_2
}%
+\cdots+#2% Print +...+a_N
}}
$$\macro{N}{x^Ny^N}$$
$$\macro{N}{x^Ny_N}$$
$$\macro{M}{(x^My_M)}$$
与えるもの: