# Subscript after \middle is positioned too high

I Have this code :

\left [ 1 - \frac{ \sum_{1}^{n}\left ( \frac {\sum\left ( P(O_k)) \right )}
{\sum R_j} \middle|_i \times C_i \right )}{CT}  \right ] \times 100


Which gives me this output :

But I want it to be as follows :

• Eek, this behaviour is quite counterintuitive. See for the (good) answers on how to cure THAT. – jk - Reinstate Monica Oct 27 '16 at 23:21
• Your title does not really say what this question is about. – Fritz Oct 28 '16 at 7:17
• @Fritz you're right, but this title is the best that I could think of. – Bilal Oct 28 '16 at 9:27
• @Bilal: How about this? "Subscript after \middle is positioned too high" – Fritz Oct 28 '16 at 11:33
• @Fritz, I changed it, thank you for your suggestion ! – Bilal Oct 29 '16 at 17:37

\middle basically acts like a closing \right immediately followed by a new \left, so the subscript here doesn't work properly. However, you can use a manually sized middle bar instead:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
$\left [ 1 - \frac{ \sum_{1}^{n}\left( \frac {\sum\left ( P(O_k) \right )} {\sum R_j} \biggm|_i \times C_i \right)}{CT} \right ] \times 100$
\end{document}


Another version using \biggl( ... \Bigm|_i ... \biggr) gives a slightly smaller bar as seen in your original image:

• Use \biggm rather than \biggr..\biggl. Moreover, \big commands don't require a left/right counterpart. – Werner Oct 27 '16 at 16:54
• Thanks, I didn't know about the \bigm variants. This is probably the best simple solution here, so I applied the changes accordingly – siracusa Oct 27 '16 at 17:12

The following example replaces \middle by \right and \left and used \vphantom to get the same sizes. The example also removes a possibly redundant layer of parentheses.

The first line uses \middle for comparison. The third line adds guesses for the summation variables.

\documentclass{article}
\newcommand*{\LeftMiddleIndexRight}[6]{%
\left#1#2\vphantom{#5}\right#3_{#4}\kern-\nulldelimiterspace
\left.#5\vphantom{#2}\right#6%
}
\begin{document}
$\left [ 1 - \frac{ \sum_{1}^{n}\left ( \frac {\sum P(O_k))} {\sum R_j} \middle|_i \times C_i \right )}{CT} \right ] \times 100$
$\left [ 1 - \frac{ \sum_{1}^{n} \LeftMiddleIndexRight ( {\frac {\sum P(O_k))}{\sum R_j}} |{i} {\!{}\times C_i} ) }{CT} \right ] \times 100$
$\left [ 1 - \frac{ \sum\limits_{i=1}^{n} \LeftMiddleIndexRight ( {\frac {\sum_k P(O_k))}{\sum_j R_j}} |{i} {\!{}\times C_i} ) }{CT} \right ] \times 100$
\end{document}


Remarks:

• \left<delimiter> and \middle<delimiter> do not support a subscript.

• In the original case, the index acts on an empty atom, which serves as left operand to the binary operator \times. Therefore, TeX adds a horizontal space around \times. Inside \LeftMiddleIndexRight, the index is put to the previous close atom. Then an opening invisible atom follows and \times has no operand on its left side and looses the binary operator property. Therefore an explicit empty atom {} is inserted to the left. \! reduces the space on the left of the operator a little for cosmetic reasons.

This might be improved in several aspects, but it is a starting point:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{mleftright}

\NewDocumentCommand{\COND}{>{\SplitArgument{1}{\given}}m}{\doCOND#1}
\NewDocumentCommand{\doCOND}{mm}{%
\IfNoValueTF{#2}
{\mleft(#1\mright)}
{\mleft(#1\vphantom{{}#2}\;\mright|\maybesubscript#2
\mleft.\kern-\nulldelimiterspace\vphantom{#1{}#2}\mright)}%
}
\NewDocumentCommand{\maybesubscript}{k_}{%
\IfValueT{#1}{_#1}\;%
}
\begin{document}

$\COND{ \frac {\sum P(O_k)}{\sum R_j} \given_i \times C_i }$
$\mleft[1-\frac{1}{CT}\mleft(\sum_{i=1}^n \COND{\frac {\sum P(O_k)}{\sum R_j} \given_i \times C_i} \mright)\mright]\times 100$

\end{document}


• +1 for placing the CT denominator term in front of the long numerator. :-) – Mico Oct 29 '16 at 18:01

The question started off an interesting, proficient, and valuable brainstorm (no irony intended—really!) on how to affix a subscript to a \middle delimiter; but I was wondering whether the intended semantics of the formula couldn’t rather be something along the lines of the following:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}
% \usepackage{mleftright}

\newcommand*{\ShowLists}{%
\begingroup
\showboxdepth = 100
\tracingonline = 1
\showlists
\endgroup
}

\begin{document}

The formula:
$\left[ 1 - \frac { \sum_{1}^{n} \Biggl( \left. \frac {\sum \bigl(P(O_{k})\bigr)} {\sum R_{j}} \right|_{i} \times C_{i} \Biggr) } {CT} \right] \times 100 % \ShowLists$

\end{document}


That is, with the inner fraction, flanked on the right by an “evaluate at”-like vertical bar, that acts as the first operand of the \times binary operator. In this case, the Inner atom generated by the \left. ... \right| construction would be perfectly adequate to the context, as it would be affixing the _{i}` subscript to it.

If you compare the spacing in the output of the above code

and in the picture that the OP included in the question to show the result (s)he is trying to attain, it seems to me that they agree better than they do in other answers.

• This must be the right interpretation of the formula – Andrew Swann Nov 2 '16 at 8:11