5

I would like to define my own macro that accepts an optional parameter such as \big, \Big, etc that apply that if it is specified, otherwise apply the auto resizing \left, \right pair.

So, how do I get the following to work for the case where the optional parameter is specified.

I see that if I change the syntax to accept only \bracc[big]{1, 2} instead of \bracc[\big]{1, 2} (i.e., remove the \), then I can use

\csname#1\endcsname\{#2\csname#1\endcsname\}

which works fine but I would rather be able to specify \big (i.e, with the \) instead.

Code:

\documentclass{article}
\usepackage{xstring}
\usepackage{xparse}

\NewDocumentCommand{\bracc}{O{} m}{% \IfEq{#1}{}{% \left{#2\right}% }{% #1{#2#1}% }% }%

\begin{document} $\bracc{1, 2}$
%$\bracc[\big]{1, 2}$% How to get this case to work? \end{document}

Peter Grill
  • 223,288
  • Of course only after composing the question do I find a solution. So am leaving this here in case someone else finds it useful. I will post an answer if one does not show up in a while. – Peter Grill Feb 25 '13 at 19:41
  • 1
  • I don't see the need for xstring here: an o-type argument and \IfNoValueTF{#1}{<without option>}{<with option>} should suffice, shouldn't it? – cgnieder Feb 25 '13 at 19:57
  • @Werner: Thanks. I need that for another problem of mine. – Peter Grill Feb 25 '13 at 19:58
  • @cgnieder: Yeah that was the solution I came up with just after composing the question. You can post it an answer if you want -- don't think there is going to be anything easier than that. – Peter Grill Feb 25 '13 at 19:59
  • Will this work in aligned environments with a \\\ in the argument? – Matthew Leingang Feb 26 '13 at 12:36
  • @MatthewLeingang: I can't think of any use case where I would want to allow for a double back slash as a parameter. Do you have one in mind? – Peter Grill Feb 26 '13 at 18:44
  • @PeterGrill: The only time I use \biggg etc. is when I have to match delimeters in an aligned environment where the beginning delimiter is on one line and the ending delimiter is on the following line (something really long within the delimiters). That's exactly where \left( ... \right) won't work. – Matthew Leingang Feb 27 '13 at 02:45

1 Answers1

7

You don't need any packages for this, also you should (effectively) use \bigl and \bigr if \big is specified:

\documentclass{article}


\newcommand\bracc[2][\left]{%
  \ifx\left#1\else\mathopen\fi#1\{%
  #2%
  \ifx\left#1\right\else\mathclose\expandafter#1\fi\}%
}%


\begin{document}
    $\bracc{1, 2}$  

    $\bracc[\big]{1, 2}$% How to get this case to work?
\end{document}
David Carlisle
  • 757,742