2

Is it possible to define a command which defines a delimiter which alternates round, square, brackets brackets in the formulas?

The command would count if it has other 'mydelimiter' within itselft and alternate for better readability.

$x = \left\mydelimiter \left\mydelimiter  a+b \right\mydelimiter *y \right\mydelimiter / z $

would produce x = [(a+b)*y] / z

Thanks for your help.

  • would be much easier if you changed the syntax to, say $x = \\myleft \myleft a+b \myright *y \myright / z $ (left and \right are delicate beasts) – David Carlisle Jun 04 '19 at 15:59
  • 1
    however the supplied expression would look better without \left and \right https://tex.stackexchange.com/questions/173717/is-it-ever-bad-to-use-left-and-right – David Carlisle Jun 04 '19 at 16:00

3 Answers3

2

The nesting count would not be the hard part with the suggested syntax but arranging for \mydelimiter to swap between open and close delimiter depending on being prefixed by \left or \right would probably involve redefining the \left and \right primitives and have bad consequences somewhere..

So here is the same idea with a modified syntax that is simpler to use and a lot simpler to implement.

As noted in comments using \left \right in these cases is not always optimal and introduces additional horizontal space even when the delimiters are not enlarged.

enter image description here

\documentclass{article}

\def\ldelim{[}
\def\rdelim{]}
\def\zlefts{\left[\def\zright{\right]}\let\zleft\zleftr}
\def\zleftr{\left(\def\zright{\right)}\let\zleft\zlefts}

\let\zleft\zlefts


\begin{document}

$x = \zleft \zleft  a+b \zright * y \zright / z $


$x = [(  a+b ) * y ] / z $
\end{document}
David Carlisle
  • 757,742
2

The enparen package can automatically alternate the delimiters:

\documentclass{article} 
\usepackage{enparen} 
\begin{document}
$x = \enparen{\enparen{ a+b } * y } / z $
 \end{document}

enter image description here

2

A slightly different approach than David's allows you to cycle through more than two types of parentheses and also works with \left and \right. (Do consider the warnings regarding the use of \left and \right, though. The mleftright package may be interesting regarding this.)

\documentclass{article}

\makeatletter
    \newcount\mydelim@level
    \def\@ldlm#1{%
        \ifcase\numexpr #1\relax
            (%
        \or [%
        \or \{%
        \else
            \@ldlm{#1-3}%
        \fi
    }
    \def\@rdlm#1{%
        \ifcase\numexpr #1\relax
            \GenericWarning{}{LaTeX Warning: You used a \string\rdlm with no matching \string\ldlm!}{}{}%
        \or )%
        \or ]%
        \or \}%
        \else
            \@rdlm{#1-3}%
        \fi
    }
    \def\ldlm{%
        \@ldlm{\mydelim@level}%
        \global\advance\mydelim@level 1\relax
    }
    \def\rdlm{%
        \@rdlm{\mydelim@level}%
        \global\advance\mydelim@level -1\relax
    }

\begin{document}

\[
    \left\ldlm\left\ldlm\left\ldlm\left\ldlm
    \sum x_i
    \right\rdlm\right\rdlm\right\rdlm\right\rdlm
\]

\[
    \ldlm\ldlm\ldlm\ldlm x\rdlm\rdlm\rdlm\rdlm
\]

\end{document}

MWE output

schtandard
  • 14,892
  • thk for your answer, it is possible for the most inner bracket to always be a round bracket? i.e. whether i have 2, 3, ... N brackets, they are always selected in the order '(', '[', '{', '(' from most inner bracket to most outter bracket so that the pattern to identify bracket hierarchy is the same. – stackoverflower Feb 15 '22 at 10:36
  • @stackoverflower You could, but you would have to use the aux file and compile twice, as TeX can't know how many brackets you will nest at the start. Search around for how to do this or ask a new question. (I won't have time to look at this myself in the near future.) – schtandard Feb 16 '22 at 18:54