# Nested bracket in breqn doesn't work

I tried to use the latest version of breqn package to break this equation; The result shows that it can't break the equation at Level/depth greater than 2 even by using \setkeys{breqn}{breakdepth={4}}, also the breaking at level/depth 2 is align right. Have you any explanation?

\documentclass{article}

\usepackage{breqn}
\setkeys{breqn}{breakdepth={4}}

\begin{document}

\begin{dmath}
R
\left\{tr\left[{P}_{k+1}^{H}\left({V}^{\ast\,\!}-{V}_{k+1}\right)+{Q}_{k+1}^{H}\left({W}^{\ast\,\!}-{W}_{k+1}\right)\right]\right\}
%level 1
=R
\left\{tr\left[
\left(  %level 3
{A}_{1}^{H}{R}_{k+1}{B}_{1}^{H}+{A}_{1}^{H}{R}_{k+1}{B}_{1}^{H}+{A}_{1}^{H}{R}_{k+1}{B}_{1}^{H}
+{\overline{A}}_{2}^{H}\overline{{R}_{k+1}}{\overline{B}}_{2}^{H}+{B}_{3}{R}_{k+1}^{H}{A}_{3}
+{\overline{A}}_{2}^{H}\overline{{R}_{k+1}}{\overline{B}}_{2}^{H}+{A}_{1}^{H}{R}_{k+1}
\right)
% level 2
+\left(  %level 3
{C}_{1}^{H}{R}_{k+1}{D}_{1}^{H}
+{\overline{C}}_{2}^{H}\overline{{R}_{k+1}}{\overline{D}}_{2}^{H}{D}_{3}{R}_{k+1}^{H}{C}_{3}
+\overline{{D}_{4}}{R}_{k+1}^{T}\overline{{C}_{4}}
+\dfrac{{\left\Vert{R}_{k+1}\right\Vert }^{2}}{{\left\Vert {R}_{k}\right\Vert}^{2}}{Q}_{k}
+\left({W}^{\ast\,\!}-{W}_{k+1}\right)+\left({W}^{\ast\,\!}-{W}_{k+1}\right)
\right)
%level 2
+\left({W}^{\ast\,\!}-{W}_{k+1}\right)+\left({W}^{\ast\,\!}-{W}_{k+1}\right)
\right]\right\}
%level 2
=R
\left\{tr\left[ %level 2
{R}_{k+1}^{H}{A}_{1}\left({V}^{\ast\,\!}-{V}_{k+1}\right){B}_{1}
+{R}_{k+1}^{T}{\overline{A}}_{2}\left({V}^{\ast\,\!}-{V}_{k+1}\right){\overline{B}}_{2}
+{R}_{k+1}{B}_{3}^{H}\left({V}^{\ast\,\!}-{V}_{k+1}\right){A}_{3}^{H}
+\overline{{R}_{k+1}}{\overline{{B}_{4}}}^{H}\left({V}^{\ast\,\!}-{V}_{k+1}\right){\overline{{A}_{4}}}^{H}
+{R}_{k+1}^{H}{C}_{1}\left({W}^{\ast\,\!}-{W}_{k+1}\right){D}_{1}+{R}_{k+1}^{T}{\overline{C}}_{2}\left({W}^{\ast\,\!}-{W}_{k+1}\right){\overline{D}}_{2}
+{R}_{k+1}{D}_{3}^{H}\left({W}^{\ast\,\!}-{W}_{k+1}\right){C}_{3}^{H}+\overline{{R}_{k+1}}{\overline{{D}_{4}}}^{H}\left({W}^{\ast\,\!}-{W}_{k+1}\right){\overline{{C}_{4}}}^{H}
+\left(\dfrac{{\left\Vert{R}_{k+1}\right\Vert }^{2}}{{\left\Vert {R}_{k}\right\Vert}^{2}}{P}_{k}^{H}\left({V}^{\ast\,\!}-{V}_{k+1}\right)\,\,+{Q}_{k}^{H}\left({W}^{\ast\,\!}-{W}_{k+1}\right)\right)
\right]\right\}
\label{eq12}
\end{dmath}

\end{document}

-
–  Peter Grill Dec 19 '12 at 12:41

Hmm looks like breakdepth doesn't work as advertised (but breqn is one complicated package and Michael is sadly no longer with us). This is one possible workaround

\documentclass{article}

\usepackage{breqn}
\setkeys{breqn}{breakdepth=4}
\def\Left#1{\mathopen{\big#1}}
\def\Right#1{\mathclose{\big#1}}

\def\overbar#1{\overline{\strut#1}}
\def\tr{\mathop{\mathrm{tr}}}

\begin{document}

\begin{dmath}
R
\Left\{\tr\Left[{P}_{k+1}^{H}\Left({V}^{\ast\,\!}-{V}_{k+1}\Right)+{Q}_{k+1}^{H}\Left({W}^{\ast\,\!}-{W}_{k+1}\Right)\Right]\Right\}
%level 1
=R
\Left\{\tr\Left[
\Left(  %level 3
{A}_{1}^{H}{R}_{k+1}{B}_{1}^{H}+{A}_{1}^{H}{R}_{k+1}{B}_{1}^{H}+{A}_{1}^{H}{R}_{k+1}{B}_{1}^{H}
+{\overbar{A}}_{2}^{H}\overbar{{R}_{k+1}}{\overbar{B}}_{2}^{H}+{B}_{3}{R}_{k+1}^{H}{A}_{3}
+{\overbar{A}}_{2}^{H}\overbar{{R}_{k+1}}{\overbar{B}}_{2}^{H}+{A}_{1}^{H}{R}_{k+1}
\Right)
% level 2
+\Left(  %level 3
{C}_{1}^{H}{R}_{k+1}{D}_{1}^{H}
+{\overbar{C}}_{2}^{H}\overbar{{R}_{k+1}}{\overbar{D}}_{2}^{H}{D}_{3}{R}_{k+1}^{H}{C}_{333}
+\overbar{{D}_{4}}{R}_{k+1}^{T}\overbar{{C}_{4}}
+\dfrac{{\Left\Vert{R}_{k+1}\Right\Vert }^{2}}{{\Left\Vert {R}_{k}\Right\Vert}^{2}}{Q}_{k}
+\Left({W}^{\ast\,\!}-{W}_{k+1}\Right)+\Left({W}^{\ast\,\!}-{W}_{k+1}\Right)
\Right)
%level 2
+\Left({W}^{\ast\,\!}-{W}_{k+1}\Right)+\Left({W}^{\ast\,\!}-{W}_{k+1}\Right)
\Right]\Right\}
%level 2
=R
\Left\{\tr\Left[ %level 2
{R}_{k+1}^{H}{A}_{1}\Left({V}^{\ast\,\!}-{V}_{k+1}\Right){B}_{1}
+{R}_{k+1}^{T}{\overbar{A}}_{2}\Left({V}^{\ast\,\!}-{V}_{k+1}\Right){\overbar{B}}_{2}
+{R}_{k+1}{B}_{3}^{H}\Left({V}^{\ast\,\!}-{V}_{k+1}\Right){A}_{3}^{H}
+\overbar{{R}_{k+1}}{\overbar{{B}_{4}}}^{H}\Left({V}^{\ast\,\!}-{V}_{k+1}\Right){\overbar{{A}_{4}}}^{H}
+{R}_{k+1}^{H}{C}_{1}\Left({W}^{\ast\,\!}-{W}_{k+1}\Right){D}_{1}+{R}_{k+1}^{T}{\overbar{C}}_{2}\Left({W}^{\ast\,\!}-{W}_{k+1}\Right){\overbar{D}}_{2}
+{R}_{k+1}{D}_{3}^{H}\Left({W}^{\ast\,\!}-{W}_{k+1}\Right){C}_{3}^{H}+\overbar{{R}_{k+1}}{\overbar{{D}_{4}}}^{H}\Left({W}^{\ast\,\!}-{W}_{k+1}\Right){\overbar{{C}_{4}}}^{H}
+\Left(\dfrac{{\Left\Vert{R}_{k+1}\Right\Vert }^{2}}{{\Left\Vert {R}_{k}\Right\Vert}^{2}}{P}_{k}^{H}\Left({V}^{\ast\,\!}-{V}_{k+1}\Right)\,\,+{Q}_{k}^{H}\Left({W}^{\ast\,\!}-{W}_{k+1}\Right)\Right)
\Right]\Right\}
\label{eq12}
\end{dmath}

\end{document}

-
And Morten has left the TeX world, so there is not many who know much about the code, let alone understand it. There are several open bugs in the current breqn, but we either do not have the time for it or does not quite understand the code. –  daleif Dec 19 '12 at 13:26
ok thanks for your effort but it doesn't useful to me because the alignment doesn't seem to be good as the breaking lines not aligned with there opening bracket more than that using \big or \bigg ,...etc doesn't fit the content size do you have any more idea –  Mohamed Talaat Bakr Dec 19 '12 at 14:34
Sorry that's as much as I can do during working hours. Breqn is perhaps the most complicated code in commonly available latex packages, if it doesn't do what you want, then changing its internals requires a lot of coffee –  David Carlisle Dec 19 '12 at 14:57
@David I'd argue that some of the LaTeX3 code is more elaborate (admittedly I might be biased). I am hoping to rewrite breqn some time in the next couple of months (depending on PhD etc), so hopefully in 12 months I can start answering questions about breqn by telling people to use a hypothetical xbreqn. To do that, I will have to understand the code of breqn, understand the code of nath`, and combine the two. Good times ahead. One reason I think it should be easier for me now than for Michael and Morten is that engines have become faster, so I can afford to drop optimizations. –  Bruno Le Floch Dec 19 '12 at 15:13