4

I want to use \dss[\Sigma][P][Q][R] or \dss[\Sigma]{P,Q,R} or \dss{\Sigma,P,Q,R} to generate

\[
\iint_{\Sigma} P\mathrm{d}y\mathrm{d}z+Q\mathrm{d}z\mathrm{d}x+R\mathrm{d}x\mathrm{d}y
\]

\documentclass{article}
%\usepackage{xparse,expl3}
\usepackage{amsmath}
%\ExplSyntaxOn
\NewDocumentCommand{\dss}{O{\Sigma}ooo}{\iint_{#1}%
    \IfValueT{#2}{#2\,\mathrm{d}y\mathrm{d}z}%
    \IfValueT{#3}{+#3\,\mathrm{d}z\mathrm{d}x}%
    \IfValueT{#4}{+#4\,\mathrm{d}x\mathrm{d}y}%
}
%\ExplSyntaxOff
\begin{document}
$\dss[D][(x+y+z)][xy][(x-y-z)]$

$\dss[D][][xy][(x-y-z)]$

$\dss[D][][][x-y-z]$

$\dss[D][(x+y+z)][][]$

%or $\dss{D,x+y+z,xy,x-y-z}$.
\end{document}

But if we only have Q and R, then there is no + sign with Q. However, there are many situations and I don't know how to deal with it.

\dss[D][][][x-y-z] indicates P and Q empty but actually those are T under the test of \IfValueTF. But I can't use \dss[D][x-y-z] since x-y-z will presents P instead of R.

6
  • 4
    ooo would be for the syntax \dss[P][Q][R] but it's usually not a good idea to have adjacent o arguments as it means the first two are not really optional if you want to use the third. Commented 23 hours ago
  • 1
    I'm confused about what you think you were trying to do. Even if it worked, it looks like \IfBooleanTF{#2}{ if true }{ if false } would only print #3 and #4 if there had been no previous arguments. Do you want them to show up if they're given?
    – Teepeemm
    Commented 23 hours ago
  • @DavidCarlisle Because of there is 8 or 7 situation of the double integral, so I think it is more easy to use latex3 code to define the command. And if we would use \dss{P,Q,R} we must deal with the parameter list. Is it L3?
    – HelenBurns
    Commented 23 hours ago
  • I want all of them show up if we have P,Q and R. So I mustn't put the Q,R code to the if false part of P. It is definitely wrong.
    – HelenBurns
    Commented 23 hours ago
  • 1
    A clarification question: Should P\mathrm{d}y\mathrm{d}z +Q\mathrm{d}z\mathrm{d}x +R\mathrm{d}x\mathrm{d}y be encased in a pair of parentheses?
    – Mico
    Commented 23 hours ago

2 Answers 2

3

I suggest a key-value approach.

\documentclass{article}
\usepackage{amsmath}

% swap the comments if you really want an upright d
\newcommand{\diff}{\mathop{}\!d}
%\newcommand{\diff}{\mathop{}\!\mathrm{d}}

\ExplSyntaxOn
\NewDocumentCommand{\dss}{O{\Sigma}m}
 {
  \iint\sb{#1}%
  \helen_dss:n {#2}
 }

\keys_define:nn { helen/dss }
 {
  P .tl_set:N = \l_helen_dss_p_tl,
  Q .tl_set:N = \l_helen_dss_q_tl,
  R .tl_set:N = \l_helen_dss_r_tl,
 }
\bool_new:N \l__helen_dss_check_bool

\cs_new_protected:Nn \helen_dss:n
 {
  \group_begin:
  % populate the token lists
  \keys_set:nn { helen/dss } {#1}
  % add the parts
  \bool_set_true:N \l__helen_dss_check_bool
  \tl_if_blank:VTF \l_helen_dss_p_tl
   {% no P part
    \bool_set_false:N \l__helen_dss_check_bool
   }
   {% P part
    \tl_use:N \l_helen_dss_p_tl \diff y \diff z
   }
  \tl_if_blank:VF \l_helen_dss_q_tl
   {
    \bool_if:NT \l__helen_dss_check_bool
     {
      \tl_if_eq:enF { \tl_head:N \l_helen_dss_q_tl } { - } { + }
     }
    \tl_use:N \l_helen_dss_q_tl \diff z \diff x
    \bool_set_true:N \l__helen_dss_check_bool
   }
  \tl_if_blank:VF \l_helen_dss_r_tl
   {
    \bool_if:NT \l__helen_dss_check_bool
     {
      \tl_if_eq:enF { \tl_head:N \l_helen_dss_r_tl } { - } { + }
     }
    \tl_use:N \l_helen_dss_r_tl \diff x \diff y
   }
  \group_end:
 }

\ExplSyntaxOff

\begin{document}

\begin{gather}
\dss{P=(x+y+z),Q=xy,R=(x-y-z)}    % 1
\\
\dss{P=(x+y+z),Q=-xy,R=(x-y-z)}   % 2
\\
\dss{P=(x+y+z),R=(x-y-z)}         % 3
\\
\dss{Q=xy,R=(x-y-z)}              % 4
\\
\dss[D]{R=(x-y-z)}                % 5
\\
\dss{P=(x+y+z)}                   % 6
\\
\dss{Q=(x+y+z),R=-xy}             % 7
\end{gather}

\end{document}

Care has been taken not to add a spurious + sign when one of the trailing parts starts with a minus sign.

output

Sorry, but I can't stand the upright d for the differential. 😉 In case you prefer it, just choose the second definition of \diff. Use \diff whenever you have a differential in your document. This way you can switch to the alternative typesetting by just changing the definition.

1
  • thanks, I forgot the situation of - sign.
    – HelenBurns
    Commented 16 hours ago
4

As noted in the comments, consecutive optional arguments mean that if you want later, then you must have the earlier, and they're no longer really optional. So I'm going to make them mandatory. I'm also going to object to the syntax \dss{\Sigma,P,Q,R}, because the \Sigma is different than the other arguments. So we'll end up with a signature \dss[\Sigma]{P}{Q}{R}.

I'm going to use expl3 after all, because its seq (and clist) have an easy way to join several values with something between, which is what you're needing with the +.

\documentclass{article}
\usepackage{amsmath}
\newcommand{\dss}[4][\Sigma]{\iint_{#1}\plusJoinedDiffs{#2}{#3}{#4}}
\ExplSyntaxOn
\seq_new:N \l__helenburns_args_seq
\cs_new:Npn \plusJoinedDiffs #1#2#3 {
  \seq_clear:N \l__helenburns_args_seq
  \IfBlankF{#1}
    { \seq_put_right:Nn \l__helenburns_args_seq {#1\,\mathrm{d}y\mathrm{d}z} }
  \IfBlankF{#2}
    { \seq_put_right:Nn \l__helenburns_args_seq {#2\,\mathrm{d}z\mathrm{d}x} }
  \IfBlankF{#3}
    { \seq_put_right:Nn \l__helenburns_args_seq {#3\,\mathrm{d}x\mathrm{d}y} }
  \seq_use:Nn \l__helenburns_args_seq {+}
}
\ExplSyntaxOff

\begin{document}
\begin{gather*}
\dss[D]{(x+y+z)}{xy}{(x-y-z)}\\
\dss[D]{}{xy}{(x-y-z)}\\
\dss[D]{}{}{x-y-z}\\
\dss[D]{(x+y+z)}{}{}
\end{gather*}
\end{document}

code output

(You could split the input in {P,Q,R}, but then you'd also need to have an input of {,,R}, which I don't like.)

6
  • \dss[D]{}{xy}{(x-y-z)} supposed without dydz in the output.
    – HelenBurns
    Commented 20 hours ago
  • 1
    Oh right. I had to use \IfBlankF instead of \IfValueT.
    – Teepeemm
    Commented 19 hours ago
  • It is works but why plus sign add to the seq at each parameter.
    – HelenBurns
    Commented 19 hours ago
  • I'm not quite understanding you. The \IfValueT calls puts the summands into the seq. The \seq_use:Nn step joins all the pieces of the seq with + between them.
    – Teepeemm
    Commented 17 hours ago
  • 1
    According to the answer by egreg, we must aslo consider the minus sign.
    – HelenBurns
    Commented 6 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.