3

I'd like to reuse mathtools DeclarePairedDelimiter in a foreach loop manner, but I have never done any low-level (La)TeXing before. Here is my code:

\documentclass{article}

\usepackage{mathtools}
\usepackage{expl3}

\ExplSyntaxOn
    \clist_map_inline:nn{angle,brace,brack,paren,vert,Vert}
    {
        \expandafter\DeclarePairedDelimiter
        \csname lr#1A\endcsname
        {{\csname l#1\endcsname}}
        {{\csname r#1\endcsname}}

        \expandafter\DeclarePairedDelimiter
        \csname lr#1B\endcsname
        {\csname l#1\endcsname}
        {\csname r#1\endcsname}
    }
\ExplSyntaxOff

\begin{document}
\[
    \lrparenA{\frac{x}{y}}\quad
    \lrparenA*{\frac{x}{y}}\quad% <- doesn't work
    \lrparenA[\Bigg]{\frac{x}{y}}\quad
    \lrparenB{\frac{x}{y}}\quad
    \lrparenB*{\frac{x}{y}}\quad
    \lrparenB[\Bigg]{\frac{x}{y}}% <- doesn't work
\]
\end{document}

I don't know why the first solution doesn't work with the star version and why the second one doesnt't work with optional parameter.

Is there any general approach to reusing implementation in the way I tried to do it? Let's say I'd like to reuse NewDocumentCommand from xparse e.g.

\NewDocumentCommand{\I}{E{_^}{{a+}{\alpha}}m}{
    {\mathrm{I}_{#1}^{#2}#3}
}

where \I will be changed to \<some letter> and \mathrm{I} to \mathrm{<some letter>}?

Maybe I have to use pgffor?

1

I suggest a different approach with expl3, without mixing classical programming with it:

\documentclass{article}

\usepackage{mathtools}
\usepackage{xparse} % also loads expl3

\ExplSyntaxOn
\NewDocumentCommand{\definedelimiters}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    \serafiel_definedelimiters:ccc { lr##1 } { l##1 } { r##1 }
   }
 }
\cs_new_protected:Nn \serafiel_definedelimiters:NNN
 {
  \DeclarePairedDelimiter #1 #2 #3
 }
\cs_generate_variant:Nn \serafiel_definedelimiters:NNN { ccc }
\ExplSyntaxOff

\definedelimiters{angle,brace,brack,paren,vert,Vert}

\begin{document}
\[
    \lrparen{\frac{x}{y}}\quad
    \lrparen*{\frac{x}{y}}\quad% <- doesn't work
    \lrparen[\Bigg]{\frac{x}{y}}\quad
\]
\end{document}

The problem with your definition is the explicit use of \csname and also with the additional braces.

enter image description here

I will say nothing about the second request, because it's quite unclear. Ask a more precise (different) question: as it stands, the definition of \I is not working.

  • Could you point me the best place to start expl3 learning? Your answers are always very good and this one works like a charm, but I can't accept it without full understanding. – Serafiel Oct 31 '18 at 16:16
  • @Serafiel texdoc interface3 and studying examples you find on the site. – egreg Oct 31 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.