[Tex/LaTex] Automatic parenthesis sizing with multiple sums and products using new lines

bracketssharelatex

I'm trying to use auto sizing of parentheses. Can you see why the below is creating brackets all of the same size? I've tried removing reference to the function \x, but this doesn't change anything. Is there a problem with using newlines and tabs to nicely display code like this?

Note: I'm using ShareLaTeX with no access to a LaTeX compiler atm.

\documentclass{article}
\usepackage{amsmath, amsthm, bm, amssymb}
% make bold x to the bracketed n (optional)
\newcommand{\x}[1][n]{\bm{x}^{\left(#1\right)}} 
\begin{document}
\begin{align*}
    \ell &= \prod_{n=1}^N \left[
        \sum_{k=1}^K \left[
            \pi_k \prod_{d=1}^D
                p_{kd}^{\x_d} \left(1-p_{kd}\right)^{1-\x_d}
        \right]
    \right]
\end{align*}
\end{document}

Best Answer

It is always desirable to use the controlled sizes offered by the ams package with sizes as: \big, \Big, \bigg, \Bigg. The reason here is that \left or \right only provides a single size bigger than a normal brace or bracket. For a comparison of the above sizes, see this picture, e.g.:

enter image description here

So, in your case you can do this as follows:

\documentclass{article}
\usepackage{amsmath, amsthm, bm, amssymb}
% make bold x to the bracketed n (optional)
\newcommand{\x}[1][n]{\bm{x}^{\left(#1\right)}} 
\begin{document}
\begin{align*}
    \ell &= \prod_{n=1}^N \Biggl[
        \sum_{k=1}^K \biggl[
            \pi_k \prod_{d=1}^D
                p_{kd}^{\x_d} \left(1-p_{kd}\right)^{1-\x_d}
        \biggr]
    \Biggr]
\end{align*}
\end{document}

Refer to the ams package documentation for more details:

The output then is proportional as this:

enter image description here

Related Question