19

After seeing the answer to this question I realized that I preferred the way of working of Juan A. Navarro's answer:

  • I.e., \DeclarePairedDelimiter{\parens}{\lparen}{\rparen} from mathtools is a great tool, it gives you
    • \parens{…} which doesn't adjust the height,
    • \parens*{…}, which automatically adjusts the height,
    • and i.e., \parens[\bigg]{…}, which result in the size you specified.
  • \newdelimcommand{\parens}{\lparen}{\rparen} from the answer I linked results in
    • \parens{…} which automatically adjusts the height,
    • \parens*{…}, which doesn't adjust the height,
    • and i.e., \parens[bigg]{…}, which result in the size you specified.

I prefer the second way of working than mathtools. But, the spacing you get is wrong (like if you were using \left( instead of \biggl(). I tried using \usepackage{mleftright} \mleftright before the code from Juan A. Navarro, but it didn't work. So my question is

How could I get the style of the answer I linked but with correct spacing (like mathtools)?

May be it is easier to exchange the behavior in mathtools starred and unstarred (plus the bigg instead of \bigg), or may be it's easier to rewrite Juan A. Navarro's code.

EDIT: Well, reading I got here, and then here. Were the last absolutely answers my question except that the starred version doesn't work.

Here is the code:

\documentclass{scrartcl}
\usepackage{mathtools}
\usepackage{etextools}
\usepackage{ifthen}

\makeatletter
    \newcommand{\DeclarePairedDelimiterCase}[2]{%
        \newcommand#1[1][]{%
            \ifthenelse{\equal{##1}{normal}}%
            {#2}%
            {%
                \ifthenelse{\equal{##1}{big}\OR\equal{##1}{Big}\OR\equal{##1}{bigg}\OR\equal{##1}{Bigg}}%
                {\expandnext{#2[}{\csname##1\endcsname}]}%
                {#2*}%        % standard case using \left and \right
            }%
        }%
    }
    \newcommand{\DeclarePairedDelimiterY}[4][Temp]{%
        \expandafter\DeclarePairedDelimiter\csname#2#1\endcsname{#3}{#4}%
        \expandnext{\expandnext{\DeclarePairedDelimiterCase}{\csname#2\endcsname}}{\csname#2#1\endcsname}%
    }
    \newcommand{\DeclarePairedDelimiterXY}[6][Temp]{%
        \expandafter\DeclarePairedDelimiterX\csname#2#1\endcsname[#3]{#4}{#5}{#6}%
        \expandnext{\expandnext{\DeclarePairedDelimiterCase}{\csname#2\endcsname}}{\csname#2#1\endcsname}%
    }
\makeatother
\DeclarePairedDelimiterY{set}{\lbrace}{\rbrace}
\DeclarePairedDelimiter{\pa}{\lparen}{\rparen}

\begin{document}

\begin{align*}
    \liminf \set*{\sum}
\end{align*}

\end{document}

And then I get lim inf{*}∑ instead of what I want lim inf{∑}.

EDIT2: I realized that the one I used before had some extra options I do need:

  • \parens{…} and \parens[auto]{…} should be autosize.
  • \parens*{…} and \parens[base]{…} should be the basic ones (without scaling).
  • The rest as I described before \parens[bigg]{…}, etc. to manually scale.
Manuel
  • 27,118

2 Answers2

16

I continue to believe that one should use \left and \right where really needed and any sort of automatism for this is bad.

However, here's a simple and working implementation of your macro, that I call \xDeclarePairedDelimiter

\documentclass{scrartcl}
\usepackage{mleftright,xparse}

\NewDocumentCommand\xDeclarePairedDelimiter{mmm}
 {%
  \NewDocumentCommand#1{som}{%
   \IfNoValueTF{##2}
    {\IfBooleanTF{##1}{#2##3#3}{\mleft#2##3\mright#3}}
    {\mathopen{##2#2}##3\mathclose{##2#3}}%
  }%
 }
\xDeclarePairedDelimiter{\set}{\lbrace}{\rbrace}

\begin{document}

\begin{align*}
&\liminf \set{\sum}\\
&\liminf \set*{\sum}\\
&\liminf \set[\big]{\sum}\\
&\liminf \set[\Big]{\sum}
\end{align*}

\end{document}

enter image description here

David Carlisle
  • 757,742
egreg
  • 1,121,712
  • 3
    Thanks. Could this work but with [big] or [Big] instead of your [\big] or [\Big]? By the way, the automatism helps if you take notes during lessons (where it doesn't matter if it's perfect or acceptable): much easier to write \set{…} all the time, and after all do a fast check in less than ten seconds looking for overheight delimiters than calculating all the time which height is appropriate. – Manuel Jan 18 '13 at 22:49
  • @Manuel Why? Please, don't use this macro. ;-) And if you really want to use big instead of \big, change \mathopen{##2#2}##3\mathclose{##2#3} into \mathopen{\csname##2\endcsname#2}##3\mathclose{\csname##2\endcsname#3} – egreg Jan 18 '13 at 22:53
  • @egreg I don't think this automation is bad, if it works. Do you see an example where the usage of this macro would lead to wrong typography? – mafp Jan 18 '13 at 23:14
  • 4
    One problem is promoting it to users who does not think much about what they are doing. I've edited lots of code where the author just plastered left right all over the place. (my tablet does not seem to have back ticks) I hardly ever use left right any more. It looks at better in a manus script if you have a clear strategy for how to scale things – daleif Jan 19 '13 at 12:51
  • @daleif I started using LaTeX because two (main) reasons: it's commands (the freedom to define personal commands easily), and because I need to typeset math. In the recent time I'm starting to care (a bit) about typography, but no so much. In my case, it's much easier to read the code and to write it if you find \set{…} instead of \{ … \}. And if it doesn't scale well, it's shorter to write \set[bigg]{…} than \biggl\{ … \biggr\} where both output exactly the same. I think. – Manuel Jan 19 '13 at 16:01
  • 3
    @daleif OK, too many unneeded \left and \right blur the code. But this is not happening here. Also, the spacing issues that the standard \left/\right has (think of \sin \left( x \right)) are avoided here. So I am genuinely interested in an example where the approach in the answer fails. Moreover, you have to manually check the output anyway: if you use plain (/) you have to look for too small parentheses, with \xDeclarePairedDelimiter you have to look for too large parentheses. The question is, what happens more often? – mafp Jan 19 '13 at 16:47
  • 3
    One example is in text based math, using left right ends up scaling too much in too many cases, scaling which than affects line spacing. I'd rather have people become more aware about how they scale fences and by how much – daleif Jan 19 '13 at 19:09
  • @daleif the main problem is that with this command you usually don't use \left/\right, because you end using i.e., \set[Big]{…}, a lot (I mean, manual commands). What you shouldn't like is commands like \newcommand\parens[1]{\left(#1\right)}, which is NOT the case. After all, \set[bigg]{…} seems shorter and more semantic than \biggl\{ … \biggr\}, at least for me. – Manuel Jan 20 '13 at 11:02
  • @egreg please consider providing a link for “I continue to believe that one should use \left and \right where really needed and any sort of automatism for this is bad”. The rationale is unclear to me (and, considering the comments, to some others as well). – Olivier Cailloux Apr 15 '20 at 10:05
  • 1
    @OlivierCailloux The main reason is that automatic sizing most often chooses too big delimiters. – egreg Apr 15 '20 at 10:11
1

Let me easily change the behaviour, without defining new commands like \xDeclarePairedDelimiter

\documentclass[12pt]{article}

\usepackage{mathtools}
\usepackage{etoolbox}


\DeclarePairedDelimiter{\avg}{\langle}{\rangle}

\newcommand\swapifbranches[3]{#1{#3}{#2}}
\makeatletter
\MHInternalSyntaxOn
\patchcmd{\DeclarePairedDelimiter}{\@ifstar}{\swapifbranches\@ifstar}{}{}
\MHInternalSyntaxOff
\makeatother

\DeclarePairedDelimiter{\mean}{\langle}{\rangle}


\begin{document}

$\avg{\displaystyle\sum_0^1}$  $\avg*{\displaystyle\sum_0^1}$

$\mean{\displaystyle\sum_0^1}$ $\mean*{\displaystyle\sum_0^1}$

\end{document}

the result