动机
我正在编写一个自定义\Paragraph
命令,旨在用于align
-type 环境中。
代码
这就是它。它目前的表现符合预期,但看看问题以下。
\documentclass{article}
\usepackage{xparse,amsmath}
\NewDocumentCommand{\Aligned}{O{t} m}{
\begin{aligned}[#1]#2\end{aligned}
}
\ExplSyntaxOn
% define keys
\keys_define:nn { Paragraph }{
beforeparagraph .tl_set:N = \l__Paragraph_beforeparagraph_tl,
beforehead .tl_set:N = \l__Paragraph_beforehead_tl,
width .tl_set:N = \l__Paragraph_width_tl,
afterhead .tl_set:N = \l__Paragraph_afterhead_tl,
beforebody .tl_set:N = \l__Paragraph_beforebody_tl,
bodyindent .tl_set:N = \l__Paragraph_bodyindent_tl,
afterparagraph .tl_set:N = \l__Paragraph_afterparagraph_tl,
}
\cs_new_protected:Nn \Paragraph_beforeparagraph:n{#1}
\cs_new_protected:Nn \Paragraph_beforehead:n{#1}
\cs_new_protected:Nn \Paragraph_width:n{#1}
\cs_new_protected:Nn \Paragraph_afterhead:n{#1}
\cs_new_protected:Nn \Paragraph_beforebody:n{#1}
\cs_new_protected:Nn \Paragraph_bodyindent:n{#1}
\cs_new_protected:Nn \Paragraph_afterparagraph:n{#1}
\cs_generate_variant:Nn \__Paragraph_build:nnnnnnnnn {VVVnVVVnV}
% auxiliary function to avoid expansion problems and do some of the formatting
\cs_new_protected:Nn \__Paragraph_build:nnnnnnnnn {
\Paragraph_beforeparagraph:n { #1 }
\Aligned{
\tl_if_empty:nF { #4 }{
\Paragraph_beforehead:n { #2 }
\tl_if_empty:nTF { #3 }{\text{#3}}{\parbox{#3}{#4}}
\Paragraph_afterhead:n { #5 }
}
\tl_if_empty:nF { #8 }{
\Paragraph_beforebody:n { #6 }
\Paragraph_bodyindent:n { \hspace{#7} }
{ \Aligned{#8} }
}
}
\Paragraph_afterparagraph:n { #9 }
}
% formatting
\NewDocumentCommand{\Paragraph}{
O{
beforeparagraph={&},
beforehead={&},
width=10cm,
afterhead={\\},
beforebody={&},
bodyindent=1em,
afterparagraph={\\}
} m m
}{
\keys_set:nn { Paragraph } { #1 } % populate keys
% format paragraph
\__Paragraph_build:VVVnVVVnV
\l__Paragraph_beforeparagraph_tl
\l__Paragraph_beforehead_tl
\l__Paragraph_width_tl
{ #2 }
\l__Paragraph_afterhead_tl
\l__Paragraph_beforebody_tl
\l__Paragraph_bodyindent_tl
{ #3 }
\l__Paragraph_afterparagraph_tl
}
\ExplSyntaxOff
问题
如何才能改变其中一个值而不改变所有值?
\begin{document}
\begin{align*}
\Paragraph{the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog}{
2^2 + 3^2
& = 4 + 9 \\
& = 13
} % example with default options
\Paragraph[width=8cm]{the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog}{1+1=2} % should overwrite only width; actually overwrites all optional parameters
\end{align*}
\end{document}
答案1
由于不能在的主体周围使用\group_begin:
和,因此该方法是在执行时明确给出所需的初始值。\group_end:
\Paragraph
\keys_set:nn
\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\Aligned}{O{t} m}
{
\begin{aligned}[#1]#2\end{aligned}
}
% define keys
\keys_define:nn { Paragraph }
{
beforeparagraph .tl_set:N = \l__Paragraph_beforeparagraph_tl,
beforehead .tl_set:N = \l__Paragraph_beforehead_tl,
width .tl_set:N = \l__Paragraph_width_tl,
afterhead .tl_set:N = \l__Paragraph_afterhead_tl,
beforebody .tl_set:N = \l__Paragraph_beforebody_tl,
bodyindent .tl_set:N = \l__Paragraph_bodyindent_tl,
afterparagraph .tl_set:N = \l__Paragraph_afterparagraph_tl,
}
\cs_new_protected:Nn \Paragraph_beforeparagraph:n{#1}
\cs_new_protected:Nn \Paragraph_beforehead:n{#1}
\cs_new_protected:Nn \Paragraph_width:n{#1}
\cs_new_protected:Nn \Paragraph_afterhead:n{#1}
\cs_new_protected:Nn \Paragraph_beforebody:n{#1}
\cs_new_protected:Nn \Paragraph_bodyindent:n{#1}
\cs_new_protected:Nn \Paragraph_afterparagraph:n{#1}
\cs_generate_variant:Nn \__Paragraph_build:nnnnnnnnn {VVVnVVVnV}
% auxiliary function to avoid expansion problems and do some of the formatting
\cs_new_protected:Nn \__Paragraph_build:nnnnnnnnn
{
\Paragraph_beforeparagraph:n { #1 }
\Aligned
{
\tl_if_empty:nF { #4 }
{
\Paragraph_beforehead:n { #2 }
\tl_if_empty:nTF { #3 }{\text{#3}}{\parbox{#3}{#4}}
\Paragraph_afterhead:n { #5 }
}
\tl_if_empty:nF { #8 }
{
\Paragraph_beforebody:n { #6 }
\Paragraph_bodyindent:n { \hspace{#7} }
{ \Aligned{#8} }
}
}
\Paragraph_afterparagraph:n { #9 }
}
% formatting
\NewDocumentCommand{\Paragraph}{ O{} m m }
{
\keys_set:nn { Paragraph }
{
beforeparagraph = {&},
beforehead = {&},
width = 10cm,
afterhead = {\\},
beforebody = {&},
bodyindent = 1em,
afterparagraph = {\\},
#1
} % populate keys
% format paragraph
\__Paragraph_build:VVVnVVVnV
\l__Paragraph_beforeparagraph_tl
\l__Paragraph_beforehead_tl
\l__Paragraph_width_tl
{ #2 }
\l__Paragraph_afterhead_tl
\l__Paragraph_beforebody_tl
\l__Paragraph_bodyindent_tl
{ #3 }
\l__Paragraph_afterparagraph_tl
}
\ExplSyntaxOff
\begin{document}
\begin{align*}
\Paragraph{
the quick brown fox jumps over the lazy dog the quick
brown fox jumps over the lazy dog}{
2^2 + 3^2
& = 4 + 9 \\
& = 13
}
\Paragraph[width=8cm]{
the quick brown fox jumps over the lazy dog
the quick brown fox jumps over the lazy dog}{1+1=2}
\end{align*}
\end{document}