
답변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}
선택적 인수(default N
)는 두 번째 인수에서 로 대체되므로 #1
이를 기반으로 매크로를 정의하여 사용할 수 있습니다.
그러나 나는 다른 접근 방식을 선호합니다. 두 번째 인수에서는 #1
"변수 부분"에 사용합니다.
\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 솔루션입니다. 와 동일한 개념을 사용 Οὖτις
하지만 입력이 제어 시퀀스일 필요는 없습니다(입력은 문자여야 함). 선택적 인수의 목적을 이해하지 못하기 때문에 제거했지만, 의도한 대로 사용했다면 를 선택적 인수가 기본적으로 인 a로 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)}$$
다음을 제공합니다: