# Subscripts after a macro for \left( and \right) parentheses

I'm trying to come up with some macro for round parentheses that looks nicely in both inline and display modes. The constraints are:

• The macro should behave like \left( \right) in display mode
• It should behave like ordinary ( and ) in all other cases
• It should play well with subscripts

Here is what I've tried:

\documentclass{scrbook}
\usepackage{amsmath}

% commands for round parentheses
\newcommand{\rParA}[1]{\mathchoice{\left(#1\right)}{(#1)}{(#1)}{(#1)}}
\newcommand{\rParB}[1]{\left(#1\right)}

\begin{document}
\begin{minipage}[H]{0.5\linewidth}
Here is some process $\rParA{A^N_{\lfloor t / N \rfloor}}_t$.
Here is another process $\rParB{B^N_{\lfloor t / N \rfloor}}_t$.
Here is what they look like in display mode:
\begin{align*}
\rParA{A^N_{\lfloor t / N \rfloor}}_t
\rParB{B^N_{\lfloor t / N \rfloor}}_t
\end{align*}
\end{minipage}
\end{document}


This code contains two macros. The first one works with \mathchoice, the second one just inserts \left and \right everywhere. Here is the outcome:

The version A behaves as expected in inline-mode (the parentheses stay as short as possible, this is what I want), however, it breaks in display-mode (the subscript floats somewhere up in the sky, this is bad).

The version B occupies way to much space in inline-mode (the parentheses are too tall and influence line-spacing), but the subscript in display-mode is on it's correct position (this is good).

How can I get a macro that behaves like A in inline-mode but like B in display mode?

• Never ever use \left ... \right in the running text, they often become way too large. Jun 19, 2015 at 13:17
• Thanks daleif, that's a wonderful and precise rule that you've got here. Now, can you please express this rule as a macro? (this is what rParA does, the question is: how to make it work with subscripts). Jun 19, 2015 at 13:20
• There is no automatic rule for this. In many circumstances \left ... \right will also be wrong in display mode. Thus manual scaling is mostly better. A slightly more convenient interface can be made using \DeclarePairedDelimiter from mathtools. Jun 19, 2015 at 14:23

I'm not sure you want to do this or, better, I discourage you to do this: automatically applying \left and \right is always wrong.

\documentclass{scrbook}
\usepackage{amsmath}

% commands for round parentheses
\makeatletter
\DeclareRobustCommand{\rPar}[1]{%
\@ifnextchar_{\rPar@sb{#1}}{\rPar@nosb{#1}}%
}
\newcommand{\rPar@sb}[3]{%
% #1 is what we already have, #2 is _, #3 is the subscript
\mathchoice{\left(#1\right)_{#3}}{(#1)_{#3}}{(#1)_{#3}}{(#1)_{#3}}%
}
\newcommand{\rPar@nosb}[1]{%
\mathchoice{\left(#1\right)}{(#1)}{(#1)}{(#1)}%
}
\makeatother

\begin{document}
\begin{minipage}{0.5\linewidth}
Here is some process $\rPar{A^N_{\lfloor t / N \rfloor}}_t$.
Here is another process $\rPar{B^N_{\lfloor t / N \rfloor}}$.
Here is what they look like in display mode:
\begin{equation*}
\rPar{A^N_{\lfloor t / N \rfloor}}_t
\rPar{B^N_{\lfloor t / N \rfloor}}
\end{equation*}
\end{minipage}
\end{document}


• Thank you very much egreg for pointing to this neat technique, it does what I wanted. I added my own version that works with both sub- and superscripts below. How can "automatically doing" something possibly be "always wrong"? If I have some fixed rule, what is the advantage of applying it manually thousand times, instead of writing a macro once? What's with DRY? What's with the possibility to change every occurrence of a certain combination of symbols across the entire document by just tweaking a single macro? Jun 20, 2015 at 6:45

use something like:

\documentclass{scrbook}
\usepackage{amsmath}

% commands for round parentheses
\makeatletter
\newcommand\rParA[1]{\ifinalign@\mathchoice{\left(#1\right)}{(#1)}{(#1)}{(#1)}
\else(#1)\fi}
\newcommand\rParB[1]{\ifinalign@\left(#1\right)\else#1\fi}
\makeatother

\begin{document}
\begin{minipage}[H]{0.5\linewidth}
Here is some process $\rParA{A^N_{\lfloor t / N \rfloor}}_t$.
Here is another process $\rParB{{B^N_{\lfloor t / N \rfloor}}}_t$.
Here is what they look like in display mode:
\begin{align*}
\rParA{A^N_{\lfloor t / N \rfloor}}_t
\rParB{B^N_{\lfloor t / N \rfloor}}_t
\end{align*}
\end{minipage}
\end{document}

• Thanks Herbert, I've just tried to compile it, the only effect seems to be that parentheses around $B$ in text mode disappeared entirely. I'll take a closer look at \ifinalign (although I don't understand why it's supposed to compose better than \mathchoice...) Jun 19, 2015 at 13:36
• Your version B seems to point into the right direction, but it still does not quite work as expected, because it behaves weird if the parentheses themselves are used in subscripts inside of an align block... Jun 19, 2015 at 13:47
• there is also an \ifinner. Have a look into the code of amsmath.sty
– user2478
Jun 19, 2015 at 14:12

Here is a variation of what egreg proposed. It provides a reasonable default for round parentheses (at least in my humble opinion), and works with both sub- and superscripts immediately following those parentheses:

\documentclass{scrbook}
\usepackage{amsmath}

\makeatletter
% Automatically scaled round parentheses with
% subscripts and superscripts.
%
% Usage: \rPar{x}_b^t results either in
% (x)_b^t or \left(x\right)_b^t, depending on
% the math mode.
\newcommand{\rPar}[1]{
\@ifnextchar_{
\rPar@sb{#1}
}{
\@ifnextchar^{
\rPar@sp{#1}
}{
\rPar@choice{#1}{}{}
}
}
}
\newcommand{\rPar@choice}[3]{
\mathchoice
{\left(#1\right)_{#2}^{#3}}
{(#1)_{#2}^{#3}}
{(#1)_{#2}^{#3}}
{(#1)_{#2}^{#3}}
}
\newcommand{\rPar@sb}[3]{
% args: content, _, subscript
\@ifnextchar^{
\rPar@sb@sp{#1}{#3}
}{
\rPar@choice{#1}{#3}{}
}
}
\newcommand{\rPar@sp}[3]{
% args: content, ^, superscript
\@ifnextchar_{
\rPar@sp@sb{#1}{#3}
}{
\rPar@choice{#1}{}{#3}
}
}
\newcommand{\rPar@sb@sp}[4]{
% args: content, subscript, ^, superscript
\rPar@choice{#1}{#2}{#4}
}
\newcommand{\rPar@sp@sb}[4]{
% args: content, superscript, _, subscript
\rPar@choice{#1}{#4}{#2}
}
\makeatother

\begin{document}
\begin{minipage}[H]{0.5\linewidth}
Here is some process $\rPar{A^N_{\lfloor t / N \rfloor}}_t$.
Here is another process $\rPar{{B^N_{\lfloor t / N \rfloor}}}_t^s$.
Here is what they look like in display mode:
\begin{align*}
\rPar{A^N_{\lfloor t / N \rfloor}}_t

Explanation: the macro \@ifnextchar<c>{then}{else} scans the next character immediately following the macro, and if it is the same as c, then it passes c together with the immediately following token to then block, otherwise it expands to the else block. Introducing a few helper macros \rPar@sb, \rPar@sb@sp etc. and chaining everything together covers all possible combinations (nothing, only subscript, only superscript, sub-super, super-sub).
The final result is the \rPar macro that encloses it's argument in appropriately scaled round parentheses, and then adds sub- and superscripts at the right position.