3

I have a macro \lip defined with xparse which takes several arguments.

I would like to define some macro \slip which applies macro \eset after macro \lip.

Is there a way to define \slip without defining a new macro with as many arguments as \lip and passing them to this latter command?

\documentclass{article}

\usepackage{xparse}

\newcommand{\eset}[1]{\mathchoice{\left \{#1\right \}}{\{#1\}}{\{#1\}}{\{#1\}}}

\ExplSyntaxOn
\NewDocumentCommand \lip { D||{A} m D||{B} m } {
  #1^#2, #3^#4
}
\ExplSyntaxOff


% How can I define slip without the xparse boilerplate?
\ExplSyntaxOn
\NewDocumentCommand \slip {D||{A} m D||{B} m } {
  \eset{\lip |#1| {#2} |#3| {#4}}
}
\ExplSyntaxOff

\begin{document}

\[\lip |a| {1} |b| {2}\]

\[\slip |a| {1} |b| {2}\]

\end{document}
  • 1
    Why are you using \ExplSyntaxOn...\ExplSyntaxOff at all there? –  Mar 24 '17 at 13:57
  • Is there any reason why not \def\lip|#1|#2|#3|#4{#1^#2, #3^#4}, then \def\slip|#1|#2|#3|#4{\eset{#1^#2, #3^#4}}? – Steven B. Segletes Mar 24 '17 at 14:02
  • 1
    Well this is a MWE, the complete \lip command is way more complicated than that :p – Fabian Pijcke Mar 24 '17 at 14:05
  • @FabianPijcke: Are you open to \slip{ |a| {1} |b| {2}}\]? –  Mar 24 '17 at 14:09
  • @StevenB.Segletes, the \slip definition is fine except that the optional arguments (those between vertical bars) are no longer optional in slip. – Fabian Pijcke Mar 24 '17 at 14:10
  • @ChristianHupfer I would then prefer the long definition of slip, I prefer to bloat my preamble rather than my document, but this is still a nice idea :-) – Fabian Pijcke Mar 24 '17 at 14:12
  • @FabianPijcke: Well, I don't say it is impossible but if you want to maintain the optional arguments to exist for \slip there's not much that can be done other than repeat the definition. –  Mar 24 '17 at 14:16

1 Answers1

3

Apart from the doubts about \eset (it's wrong to begin with, in my opinion), you can incorporate it in \lip:

\documentclass{article}

\usepackage{xparse}

\newcommand{\eset}[1]{\mathchoice{\left \{#1\right \}}{\{#1\}}{\{#1\}}{\{#1\}}}

\ExplSyntaxOn
\NewDocumentCommand \lip {s D||{A} m D||{B} m }
 {
  \IfBooleanTF{#1}
   { \eset{#2^#3, #4^#5} }
   { #2^#3, #4^#5 }
 }
\NewDocumentCommand \slip {} { \lip* }
\ExplSyntaxOff

\begin{document}

\[\lip |a| {1} |b| {2}\]

\[\slip |a| {1} |b| {2}\]

\end{document}

Avoid using a wealth of optional arguments: you'll lose yourself in trying to remember them.

In this particular case I see no connection between the syntax and the output, so a simpler

\NewDocumentCommand \lip {s O{A} m O{B} m }
 {
  \IfBooleanTF{#1}
   { \eset{#2^#3, #4^#5} }
   { #2^#3, #4^#5 }
 }

with calls \lip{x}{y}, \lip[u]{x}{y}, \lip{x}[v]{y} or \lip[u]{x}[v]{y} would do better.

Why is the definition of \eset wrong? Because it's generally wrong to apply \left and \right indiscriminately.

If you want to accommodate other variants, you can use a slightly different strategy:

\documentclass{article}

\usepackage{xparse}

\newcommand{\eset}[1]{\mathchoice{\left \{#1\right \}}{\{#1\}}{\{#1\}}{\{#1\}}}
\newcommand{\bset}[1]{[#1]}

\ExplSyntaxOn
\NewDocumentCommand \lip {o D||{A} m D||{B} m }
 {
  \IfNoValueTF{#1}
   { #2^#3, #4^#5 }
   { #1{#2^#3, #4^#5} }
 }
\NewDocumentCommand \slip {} { \lip[\eset] }
\NewDocumentCommand \blip {} { \lip[\bset] }
\ExplSyntaxOff

\begin{document}

\[\lip |a| {1} |b| {2}\]

\[\slip |a| {1} |b| {2}\]

\[\blip |a| {1} |b| {2}\]

\end{document}

enter image description here

egreg
  • 1,121,712
  • In the lip definition, each argument between vertical bars is directly related to the mandatory argument it is followed by, so I don't think it is problematic (which is why I avoided the standard square brackets notation). Besides I had several variations in mind (slip, Slip, rlip, Rlip, etc.), so your star notation, which I happily agree on its usefulness, is not exactly what I want :-( Thank you for your suggestion anyway, it is relevant to the question and deserves upvotes! – Fabian Pijcke Mar 24 '17 at 15:07