TeX - LaTeX Asked by mjc on August 12, 2021
Motivation
I’m working on a custom Paragraph
command, intended for use inside align
-type environments.
Code
Here it is. It currently performs as desired, but see Problem below.
documentclass{article}
usepackage{xparse,amsmath}
NewDocumentCommand{Aligned}{O{t} m}{
begin{aligned}[#1]#2end{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
Problem
How can I change one of the values without changing all of them?
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}
Since you cannot use group_begin:
and group_end:
around the body of Paragraph
, the method is to explicitly give the initial wanted values when you do keys_set:nn
.
documentclass{article}
usepackage{xparse,amsmath}
ExplSyntaxOn
NewDocumentCommand{Aligned}{O{t} m}
{
begin{aligned}[#1]#2end{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}
Correct answer by egreg on August 12, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP