TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way of smartly nesting parentheses and square brackets (in text mode), so that LaTeX detects nesting relations and typesets them accordingly? I'm thinking in something like csquotes does with quotation marks. Something like:

\enparent{My outer layer \enparent{my inner lever \enparent{my innermost level}}}

with the result being:

(My outer layer [my inner level {my innermost level}])

I wonder if there is already an out-of-the-box solution for that.

share|improve this question
[Note] I posted a follow up question on this: Very smart parentheses? – Tobi Oct 8 '14 at 15:12
up vote 22 down vote accepted

A package? The following simple macro does it:


\def\smart#1{\ifcase\smcount(\or[\or\{\else TOO DEEP!\fi%
\advance\smcount by1 #1\ifcase\smcount\or)\or]\or\}\else TOO DEEP!\fi%
\advance\smcount by-1 }


\smart{Ala \smart{ma \smart {kota}}}


(Or, if you wish, not \smart but \enparent). Usage:

\smart{Ala \smart{ma \smart {kota}}}

enter image description here

share|improve this answer
Nice try, but I get: (My outer layer [my inner lever {my innermost level}}]. – NVaughan Apr 25 '13 at 2:38
I get: (Ala [ma {kota}}]. Any ideas? – NVaughan Apr 25 '13 at 2:48
@NVaughan Fixed. The final space disappeared during copying. – Przemysław Scherwentke Apr 25 '13 at 2:49
Cool! Thank you. – NVaughan Apr 25 '13 at 2:54
Do you really need all that extra white space in the image output? – azetina Apr 25 '13 at 19:35

For comparison I am posting a ConTeXt solution.

Such a macro already exists, albeit for quotes. One of the good things about ConTeXt is that a feature is never defined in a one-off basis. For example, instead of defining a macro for quotations that changes the quote symbol depending on the level of nesting, ConTeXt defines a generic delimitedtext mechanism and quotation is a special case of delimited text. The desired parenthesis macro is also a delimited text where the left and right symbol change depending on the level of nesting. So, use delimited text to define this macro as follows:

\definedelimitedtext[parenthesis]   [location=text]
\setupdelimitedtext [parenthesis:1] [left={(}, right={)}]
\setupdelimitedtext [parenthesis:2] [left={[}, right={]}]
\setupdelimitedtext [parenthesis:3] [left={\{},right={\}}]

This can be used as

\parenthesis{My outer layer \parenthesis{my inner layer \parenthesis{my innermost layer}}}

which gives

enter image description here

share|improve this answer
How I envy the ConTeXt interface! – Gonzalo Medina Apr 25 '13 at 4:38

Here is an approach which is somewhat more modular than alexurba's approach, which accomodates indefinite levels of nesting.



\enparen{one \enparen{two \enparen{three \enparen{four} levels} levels} level}


sample code for <code>\enparen</code>

share|improve this answer
Shouldn't the second \makeatletter be a \makeatother? If not, why not? – Pål GD Apr 25 '13 at 17:38
@PålGD: yes indeed, fixed. – Niel de Beaudrap Apr 25 '13 at 17:39
Nice one also. Cheers! – NVaughan Apr 25 '13 at 18:38

Maybe nested definitions:




\enparent{My outer layer \enparent{my inner lever \enparent{my innermost level}}}\\
\enparent{My outer layer \enparent{my inner lever \enparent{my innermost level}}}


enter image description here

share|improve this answer
Nice solution also. Thanks! – NVaughan Apr 25 '13 at 2:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.