Math & Scienceauto {[( )]} brackets macro

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
cornail
Posts: 3
Joined: Fri May 08, 2009 7:44 pm

auto {[( )]} brackets macro

Post by cornail »

Hello,

I am not skilled in programming LaTeX macros, so I ask for your help.
I would like to have a bracket command, e.g. \mybr{ } that automatically
puts the right brackets when writing complex formulas with many level of brackets.
For example:

Code: Select all

$a * \mybr{1 + \exp \mybr{b * \mybr{x - y}}}$
would do the same thing as if I wrote

Code: Select all

$a * \left\{ 1 + \exp \left[ b * \left( x - y \right) \right] \right\}$
but if I remove the innermost brackets (\mybr), it would automatically change to

Code: Select all

$a * \left[ 1 + \exp \left( b * x - y \right) \right]$
.

I couldn't find a package to support this, and I am to unskilled to do it myself. Please help, if it is possible to do this.

Thank you very much!
Last edited by cornail on Wed May 20, 2009 11:42 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

auto {[( )]} brackets macro

Post by Juanjo »

Try the following code:

Code: Select all

\documentclass[a4paper,11pt]{article}

\usepackage{amsmath}

\newcommand{\PutDelim}[4]{%
  \ifcase#1 #3#2#4 \or \bigl#3#2\bigr#4 \or \Bigl#3#2\Bigr#4%
    \or \biggl#3#2\biggr#4 \or \Biggl#3#2\Biggr#4% 
  \else \left#3#2\right#4\fi}
  
\newcounter{DelimLevel}

\newcommand{\br}[2][5]{\addtocounter{DelimLevel}{-1}%
   \ifcase\value{DelimLevel}\PutDelim{#1}{#2}{(}{)}%
      \or\PutDelim{#1}{#2}{[}{]}\or\PutDelim{#1}{#2}{\{}{\}}%
      \or\PutDelim{#1}{#2}{(}{)}\or\PutDelim{#1}{#2}{[}{]}%
      \or\PutDelim{#1}{#2}{\{}{\}}\fi%
   \addtocounter{DelimLevel}{1}}

\newcommand{\setbr}[3][5]{\setcounter{DelimLevel}{#2}\br[#1]{#3}}

\begin{document}

$a * \setbr{3}{1 + \exp \br{b * \br{x - y}}}$

$a * \setbr{2}{1 + \exp \br{b * x - y}}$

\[ z+\setbr{3}{\br{1+\br{n+1}^2}^3-\br{a+\br{b+c}}^2} (c-d) \]

\[ z+\setbr{3}{\br[1]{1+\br{n+1}^2}^3-\br[1]{a+\br{b+c}}^2} (c-d) \]

\[ a_0 + x \setbr{5}{a_1 + x \br{a_2 + x \br{a_3 + x \br{a_4 + x \br{a_5 + a_6 x}}}}} \]

\[ a_0 + x \setbr[2]{5}{a_1 + x \br[1]{a_2 + x \br[1]{a_3 + x \br{a_4 + x \br{a_5 + a_6 x}}}}} \]

\end{document}
As you can see, the macro \br puts a pair of matching delimiters accordingly with its nesting level. The command \setbr also does that, but it has an additional argument to fix the number of nesting levels. It would be nice that such a number could be automatically detected, but I don't know how to do it. I need to fix it a priori. You can get up to six levels. If you need more, you should modify the definition of \br.

Both \br and \setbr have an optional argument to control the size of the delimiter. By default, \br{text} is equivalent to \left delimiter text \right delimiter. If the size automatically chosen by LaTeX is not right, you can add the optional argument: its value goes from 0 (smallest) to 4 (biggest).
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
cornail
Posts: 3
Joined: Fri May 08, 2009 7:44 pm

Re: auto {[( )]} brackets macro

Post by cornail »

Juanjo, you gave me invaluable help in my thesis work, thank you :)
Post Reply