1

We are used to \left and \right operators, each followed by delimiters, among others, \{\}[]().\lvert\rvert. My question is: is it possible to generate a shortcut command \lr such that \lr{\{, \}, \frac{a}{b}} gives the same output as \left\{ \frac{a}{b} \right\} ?

Best regards,

Bruno Peixoto

1
  • 4
    Not the syntax you suggest but see \DeclarePairedDelimiter from mathtools (which will also correct the horizontal space issues from\left\right) Dec 14, 2022 at 12:29

2 Answers 2

3

Not sure what this would be useful for…

\documentclass{article}

\NewDocumentCommand{\lr}{>{\SplitArgument{2}{,}}m}{%
  \dolr#1%
}
\NewDocumentCommand{\dolr}{mmm}{%
  \left#1 #3 \right#2%
}

\begin{document}

\[
\lr{\{,\},\frac{a}{b}}
+
\lr{(,),\frac{a}{b}}
+
\lr{[,],\frac{a}{b}}
\]

\end{document}

With \SplitArgument{2}{,} the given argument is passed to the internal macro as three braced groups, so we can define \dolr with three arguments as needed.

enter image description here

Perhaps more interesting would be a much more flexible key-value interface. The size can be manually specified via big or similar; for automatic sizing. use *. The shape of the delimiters is specified with a syntax similar to how amsmath does for matrices:

  • p for parentheses
  • b for brackets
  • B for braces
  • v for single bars
  • V for double bars
  • a for angle brackets

If you have nonstandard delimiters you can always do left=<del> and right=<del>.

Inside the mandatory argument of \lr you can use | which will produce a vertical bar that extends like the outer fences.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mleftright}

\ExplSyntaxOn
\NewDocumentCommand{\lr}{O{}m}
 {
  \group_begin:
  \keys_set:nn { peixoto/lr } { size, left=., right=.,  #1 }
  \char_set_active_eq:nN { `| } \__peixote_lr_del:
  \char_set_mathcode:nn { `| } { "8000 }
  \peixoto_lr:n { #2 }
  \group_end:
 }

\keys_define:nn { peixoto/lr }
 {
  left .tl_set:N = \l__peixoto_lr_leftdel_tl,
  right .tl_set:N = \l__peixoto_lr_rightdel_tl,
  lp .meta:n = { left=( },
  rp .meta:n = { right=) },
  p .meta:n = { lp,rp },
  lb .meta:n = { left=[ },
  rb .meta:n = { right=] },
  b .meta:n = { lb,rb },
  lB .meta:n = { left=\{ },
  rB .meta:n = { right=\} },
  B .meta:n = { lB,rB },
  lv .meta:n = { left=\lvert },
  rv .meta:n = { right=\rvert },
  v .meta:n = { lv,rv },
  lV .meta:n = { left=\lVert },
  rV .meta:n = { right=\rVert },
  V .meta:n = { lV,rV },
  la .meta:n = { left=\langle },
  ra .meta:n = { right=\rangle },
  a .meta:n = { la,ra },
  size .code:n =
    \tl_set:Nx \l__peixoto_leftsize_tl  { \exp_not:c { __peixoto_lr_#1l: } }
    \tl_set:Nx \l__peixoto_midsize_tl   { \exp_not:c { __peixoto_lr_#1m: } }
    \tl_set:Nx \l__peixoto_rightsize_tl { \exp_not:c { __peixoto_lr_#1r: } },
  size .default:n = {},
  big .meta:n = { size = big },
  Big .meta:n = { size = Big },
  bigg .meta:n = { size = bigg },
  Bigg .meta:n = { size = Bigg },
  * .code:n =
    \tl_set:Nn \l__peixoto_leftsize_tl { \mleft }
    \tl_set:Nn \l__peixoto_midsize_tl { \;\middle\vert\; }
    \tl_set:Nn \l__peixoto_rightsize_tl { \mright },
}
\tl_new:N \l__peixoto_leftsize_tl
\tl_new:N \l__peixoto_rightsize_tl

\cs_new_protected:Nn \peixoto_lr:n
 {
  \l__peixoto_leftsize_tl\l__peixoto_lr_leftdel_tl
  #1
  \l__peixoto_rightsize_tl\l__peixoto_lr_rightdel_tl
}

\cs_new:Nn \__peixoto_lr_l: {}
\cs_new:Nn \__peixoto_lr_m: {\mid}
\cs_new:Nn \__peixoto_lr_r: {}
\clist_map_inline:nn { big,Big,bigg,Bigg }
 {
  \cs_new_eq:cc {__peixoto_lr_#1l:} {#1l}
  \cs_new_protected:cn {__peixoto_lr_#1m:} {\use:c{#1m}\vert}
  \cs_new_eq:cc {__peixoto_lr_#1r:} {#1r}
 }

\cs_new_protected:Nn \__peixote_lr_del: { \tl_use:N \l__peixoto_midsize_tl }

\ExplSyntaxOff

% example shorthands
\NewDocumentCommand{\lrp}{O{}m}{\lr[p,#1]{#2}}
\NewDocumentCommand{\bra}{O{}m}{\lr[la,rv,#1]{#2}}
\NewDocumentCommand{\ket}{O{}m}{\lr[lv,ra,#1]{#2}}
\NewDocumentCommand{\braket}{O{}m}{\lr[a,#1]{#2}}

\begin{document}

\[
\lr[B,*]{\frac{a}{b}}
+
\lr[p,Big]{\frac{a}{b}}
+
\lr[b,bigg]{\frac{a}{b}}
+
\lr[v,*]{\frac{a}{b}}
+
\lr[V,*]{\frac{a}{b}}
+
\lr[a,*]{\frac{a}{b}}
+
\lr[la,rv,Big]{\frac{a}{b}}
\]

\[
\lr[p]{a+b}^2
\quad
\lr[b,big]{a\lr[p]{b+c}^2-1}^3
\]

\[
\ket{x}\bra{y}+\lrp[big]{a+b}+\braket{a | b}
\]

\[
\ket[big]{x}\bra[Big]{y}+\braket[bigg]{a | b}
\]

\end{document}

enter image description here

31
  • Just a mathematician solves a problem that an engineer asks "What for?" first.
    – Bruno Lobo
    Dec 14, 2022 at 17:49
  • Bravo, bravíssimo!
    – Bruno Lobo
    Dec 14, 2022 at 17:50
  • Suggestion: I see entry possibilities: 1. \lr[group_string]{content}; 2; \lr[group_string, size_string]{content}; 3. \lr[left_group_string, right_group_string]{content}; 4. \lr[left_group_string, right_group_string, size]{content};
    – Bruno Lobo
    Dec 14, 2022 at 17:59
  • @BrunoPeixoto Yes, that's the idea. You can specify left and right separately with lx and ry, or together with a single letter. Maybe you use \llbracket and \rrbracket, so you can specify \lr[left=\llbracket,right=\rrbracket]{formula}. It's possible to add new shorthands, just follow the pattern.
    – egreg
    Dec 14, 2022 at 18:09
  • @BrunoPeixoto I added a couple of examples of how you can use it in the real world.
    – egreg
    Dec 14, 2022 at 18:21
2

It is possible with native commands declaration

\documentclass{article}
\newcommand{\lr}[3]{\left#1 #3 \right#2}

\begin{document}
    $\lr{\{} {\}} {\frac{a}{b}}$
\end{document}

Explanation:

the number 3 in [3] indicates that 3 arguments are required for this command. Then, every # indicates the position of each argument.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .