Purpose
I want a function that surrounds an expression in \left
and \right
brackets, where the shape of the bracket is given as a parameter, e.g.
\brackets[(][]]{\frac{1}{2}} % should return \left(\frac{1}{2}\right]
I'd also like the function to detect the shape of the opening bracket and supply the matching closing bracket unless told otherwise.
Background
If you want the gory details, I have actually made such a function, and it looks like this:
\usepackage{amsmath,xargs,xifthen}
\newcommandx{\brackets}[3][1=r, 2=default]{
\ifthenelse{\equal{#1}{r}}{ % r for round bracket
\ifthenelse{\equal{#2}{default}}{\left(#3\right)}{}
\ifthenelse{\equal{#2}{r}}{\left(#3\right)}{}
\ifthenelse{\equal{#2}{s}}{\left(#3\right]}{}
\ifthenelse{\equal{#2}{c}}{\left(#3\right\}}{}
\ifthenelse{\equal{#2}{v}}{\left(#3\right|}{}
\ifthenelse{\equal{#2}{}}{\left(#3\right.}{}
}{}
\ifthenelse{\equal{#1}{s}}{ % s for square bracket
\ifthenelse{\equal{#2}{default}}{\left[#3\right]}{}
\ifthenelse{\equal{#2}{r}}{\left[#3\right)}{}
\ifthenelse{\equal{#2}{s}}{\left[#3\right]}{}
\ifthenelse{\equal{#2}{c}}{\left[#3\right\}}{}
\ifthenelse{\equal{#2}{v}}{\left[#3\right|}{}
\ifthenelse{\equal{#2}{}}{\left[#3\right.}{}
}{}
\ifthenelse{\equal{#1}{c}}{ % c for curly brace
\ifthenelse{\equal{#2}{default}}{\left\{#3\right\}}{}
\ifthenelse{\equal{#2}{r}}{\left\{#3\right)}{}
\ifthenelse{\equal{#2}{s}}{\left\{#3\right]}{}
\ifthenelse{\equal{#2}{c}}{\left\{#3\right\}}{}
\ifthenelse{\equal{#2}{v}}{\left\{#3\right|}{}
\ifthenelse{\equal{#2}{}}{\left\{#3\right.}{}
}{}
\ifthenelse{\equal{#1}{v}}{ % v for vertical bar
\ifthenelse{\equal{#2}{default}}{\left|#3\right|}{}
\ifthenelse{\equal{#2}{r}}{\left|#3\right)}{}
\ifthenelse{\equal{#2}{s}}{\left|#3\right]}{}
\ifthenelse{\equal{#2}{c}}{\left|#3\right\}}{}
\ifthenelse{\equal{#2}{v}}{\left|#3\right|}{}
\ifthenelse{\equal{#2}{}}{\left|#3\right.}{}
}{}
\ifthenelse{\equal{#1}{}}{
\ifthenelse{\equal{#2}{default}}{#3}{}
\ifthenelse{\equal{#2}{r}}{\left.#3\right)}{}
\ifthenelse{\equal{#2}{s}}{\left.#3\right]}{}
\ifthenelse{\equal{#2}{c}}{\left.#3\right\}}{}
\ifthenelse{\equal{#2}{v}}{\left.#3\right|}{}
\ifthenelse{\equal{#2}{}}{\left.#3\right.}{}
}{}
}
I suspect this could be much shorter.
Problem
I seem to run into difficulty when I try to write an auxiliary function to detect the opening bracket and supply the matched closing bracket:
\newcommand{\partner}[1]{%
\ifthenelse{\equal{#1}{.}}{.}{}%
\ifthenelse{\equal{#1}{(}}{)}{}%
\ifthenelse{\equal{#1}{)}}{(}{}%
\ifthenelse{\equal{#1}{[}}{]}{}%
\ifthenelse{\equal{#1}{]}}{[}{}%
\ifthenelse{\equal{#1}{\{}}{\}}{}%
\ifthenelse{\equal{#1}{\}}}{\{}{}%
}
There seems to be an awkward interaction between \left
/\right
and ifthenelse
.
Minimal example of problem
\documentclass{article}
\usepackage{amsmath,xifthen}
\newcommand{\funcA}{
)
}
\newcommand{\funcB}{
\ifthenelse{1=1}{)}{}
}
\begin{document}
\begin{align*}
\csname left\endcsname(\frac{1}{2}\csname right\endcsname\funcA \\ % works
\csname left\endcsname(\frac{1}{2}\csname right\endcsname\funcB % doesn't work
\end{align*}
\end{document}
\brackets[(][{]}]{\frac{1}{2}}
to keep the]
from messing up the optional argument parsing\csname...\endcsname
?