Motivation
I'm trying to make a customised paragraph
command. I'd like it to have several arguments, some optional and some mandatory. An inconvenience with previous versions has been accessing optional arguments by position, like
\Paragraph[][remember to specify default value here][][is this the right one?]{body of text}
To avoid this, I'd like to implement key-value parameters, as in, say, tikz
. I'd also like to have default values specified in an optional argument in the definition.
What I have so far
Note
I expect the top line of the finished command to look like this:
\NewDocumentCommand{\Paragraph}{O{before={&}, after={\\}} m}{
I've got this optional syntax to work without the ampersand, or with the ampersand but no second key-value pair, but in the code below it was useful for explanatory purposes to use a second mandatory argument instead of the optional argument.
End of note
I've cannibalised @egreg's answer to this question to produce the following. I have the expl3
guide open, but haven't studied it thorougly.
\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
% define the keys
\keys_define:nn { Paragraph }{
before .tl_set:N = \l__Paragraph_before_tl,
after .tl_set:N = \l__Paragraph_after_tl,
}
% formatting
\NewDocumentCommand{\Paragraph}{m m}{
\keys_set:nn { Paragraph } { #1 } % populate the keys
% format the paragraph
\Paragraph_before:V \l__Paragraph_before_tl
#2
\Paragraph_after:V \l__Paragraph_after_tl
}
\cs_new_protected:Nn \Paragraph_before:n{
#1
}
\cs_new_protected:Nn \Paragraph_after:n{
#1
}
\cs_generate_variant:Nn \Paragraph_before:n {V}
\cs_generate_variant:Nn \Paragraph_after:n {V}
\ExplSyntaxOff
\begin{document}
\begin{align*}
& above \\
\Paragraph{before={&}}{text 1} \\ % before works singly
& \Paragraph{after={\\}}{text 2} % after works singly
& \Paragraph{before=hi~, after={\\}}{text 3} % before and after together, without ampersand, works
\Paragraph{before={&}, after={~hey}}{text 4} \\ % before and after together, with ampersand, doesn't work
& below
\end{align*}
\end{document}
Problem
The command is intended for use inside align
-type environments. As shown in the notes on the code, ampersand seems to break something.