4

I was trying to fit this equation manipulation in a page as shown in the MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry} \geometry{a4paper,top=2.5cm,bottom=2.5cm,left=2cm,right=2cm,heightrounded,bindingoffset=5mm}

\usepackage{amsmath,cancel,witharrows}

\begin{document}

\noindent
some words
\begin{DispWithArrows*}
    P(t|t)&=E\left[\tilde{e}_x(t)\tilde{e}_x^T(t)\right]\\
    &=E\left[\big((I-K(t)C)e_x(t)-K(t)v(t)\big)\big((I-K(t)C)e_x(t)-K(t)v(t)\big)^T\right] \Arrow[i]{$e_x$ is uncorrelated with $v(t)$\\collect $E[e_x(t)e_x^T(t)]$ and $E[v(t)v^T(t)]$}\\
    &=\big(I-K(t)C\big)E[e_x(t)e_x^T(t)]\big(I-K(t)C\big)^T+K(t)E[v(t)v^T(t)]K(t)\\
    &=\big(I-K(t)C\big)P(t|t-1)\big(I-K(t)C\big)^T+K(t)R_vK^T(t)\\
    &\begin{split}
        =P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        +K(t)CP(t|t-1)C^TK^T(t)+K(t)R_vK^T(t)
    \end{split}
    \Arrow[i]{in the 4th and 5th terms,\\factor out $K(t)$}\\
    &\begin{split}
        =P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        +K(t)\big(CP(t|t-1)C^T+R_v\big)K^T(t)
    \end{split}    
    \Arrow[i]{\eqref{eqn_kalman_filter_gain}}\\
    &\begin{split}=P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
    +\underbrace{P(t|t-1)C^T\big(CP(t|t-1)C^T+R_v\big)^{-1}}_{=K(t)}\big(CP(t|t-1)C^T+R_v\big)K^T(t)\end{split}\\
    &=P(t|t-1)\cancel{-P(t|t-1)C^T K^T(t)}-K(t)CP(t|t-1)\cancel{+P(t|t-1)C^TK^T(t)}\\
    &=P(t|t-1)-K(t)CP(t|t-1)\Arrow[i]{factor out $P(t|t-1)$}\\
    P(t|t)&=\big(I-K(t)C\big)P(t|t-1)
\end{DispWithArrows*}
some other words

\end{document}

Problem 1: The text displayed with the package witharrows and with the command \Arrow[i]{...} falls off the right edge of the sheet, while the equation doesn't start immediately at left but in the center-left of the sheet, so it doesn't exploit all the available space.

Problem 2: I tried to use the split environment but it doesn't work very well because some equations don't start aligned with the others (I think it's because I don't use it properly) and because its efforts are useless due to problem 1.

If my description is not clear, the output of the MWE will be clearer:

if my description is not clear, the output of the MWE will be clearer

How can I solve these problems?

Thanks in advance!

1
  • 1
    Use the options fleqn and mathindent=0pt, i.e., start the equation with \begin{DispWithArrows*}[fleqn,mathindent=0pt]. This typesets the equation flush left .
    – gernot
    Aug 2 at 17:45
6

Solution to problem 1: Start the equation with \begin{DispWithArrows*}[fleqn,mathindent=0pt]. This typesets the equation flushed left. See the documentation of the package witharrows for an explanation.

Solution to problem 2: Precede the lines of the split environment with &. The first column of a split environment is right-aligned, the second one left aligned. Moreover, add \quad in lines that do not start with a relation symbol.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry} \geometry{a4paper,top=2.5cm,bottom=2.5cm,left=2cm,right=2cm,heightrounded,bindingoffset=5mm}

\usepackage{amsmath,cancel,witharrows}

\begin{document}

\noindent
some words
\begin{DispWithArrows*}[fleqn,mathindent=0pt]
    P(t|t)&=E\left[\tilde{e}_x(t)\tilde{e}_x^T(t)\right]\\
    &=E\left[\big((I-K(t)C)e_x(t)-K(t)v(t)\big)\big((I-K(t)C)e_x(t)-K(t)v(t)\big)^T\right] \Arrow[i]{$e_x$ is uncorrelated with $v(t)$\\collect $E[e_x(t)e_x^T(t)]$ and $E[v(t)v^T(t)]$}\\
    &=\big(I-K(t)C\big)E[e_x(t)e_x^T(t)]\big(I-K(t)C\big)^T+K(t)E[v(t)v^T(t)]K(t)\\
    &=\big(I-K(t)C\big)P(t|t-1)\big(I-K(t)C\big)^T+K(t)R_vK^T(t)\\
    &\begin{split}
        &=P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        &\quad+K(t)CP(t|t-1)C^TK^T(t)+K(t)R_vK^T(t)
    \end{split}
    \Arrow[i]{in the 4th and 5th terms,\\factor out $K(t)$}\\
    &\begin{split}
        &=P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        &\quad+K(t)\big(CP(t|t-1)C^T+R_v\big)K^T(t)
    \end{split}    
    \Arrow[i]{\eqref{eqn_kalman_filter_gain}}\\
    &\begin{split}
      &=P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
      &\quad+\underbrace{P(t|t-1)C^T\big(CP(t|t-1)C^T+R_v\big)^{-1}}_{=K(t)}\big(CP(t|t-1)C^T+R_v\big)K^T(t)\end{split}\\
    &=P(t|t-1)\cancel{-P(t|t-1)C^T K^T(t)}-K(t)CP(t|t-1)\cancel{+P(t|t-1)C^TK^T(t)}\\
    &=P(t|t-1)-K(t)CP(t|t-1)\Arrow[i]{factor out $P(t|t-1)$}\\
    P(t|t)&=\big(I-K(t)C\big)P(t|t-1)
\end{DispWithArrows*}
some other words

\end{document}
2
  • Very neat solution, thank you!
    – Stefano
    Aug 4 at 8:18
  • ahahah amazing bio XD
    – Stefano
    Aug 4 at 9:59
4

Ok, here is my analysis and suggestion for solution.

First I introduced some abbreviations via the \def macro to better see your structure. Already with adaptions described below it reads:

% --- formulas ----------------------------------------------------
\def\ZA{E\left[\tilde{e}_x(t)\tilde{e}_x^T(t)\right]}
\def\ZB{E\left[\big((I-K(t)C)e_x(t)-K(t)v(t)\big)\big((I-K(t)C)e_x(t)-K(t)v(t)\big)^T\right]}
\def\ZCa{\big(I-K(t)C\big)E[e_x(t)e_x^T(t)]\big(I-K(t)C\big)^T+K(t)E[v(t)v^T(t)]K(t)}
\def\ZCb{\big(I-K(t)C\big)P(t|t-1)\big(I-K(t)C\big)^T+K(t)R_vK^T(t)}
\def\ZD{P(t|t-1)\cancel{-P(t|t-1)C^T K^T(t)}-K(t)CP(t|t-1)\cancel{+P(t|t-1)C^TK^T(t)}}
\def\AB{P(t|t-1)-K(t)CP(t|t-1)\Arrow[i]{factor out $P(t|t-1)$}}
\def\AA{P(t|t)&=\big(I-K(t)C\big)P(t|t-1)}
% --- split groups -------------------------------------------------------
\def\SG{P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        +K(t)CP(t|t-1)C^TK^T(t)+K(t)R_vK^T(t)}
\def\SH{P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        +K(t)\big(CP(t|t-1)C^T+R_v\big)K^T(t)}
\def\SK{P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
    +\underbrace{P(t|t-1)C^T\big(CP(t|t-1)C^T+R_v\big)^{-1}}_{=K(t)}\times \\
    \times\big(CP(t|t-1)C^T+R_v\big)K^T(t)}
% --- arrow texts --------------------------------------------------------
\def\RA{$e_x$ is uncorrelated with\\ $v(t)$ collect\\ $E[e_x(t)e_x^T(t)]$ and\\ $E[v(t)v^T(t)]$}
\def\RB{in the 4th and 5th terms,\\factor out $K(t)$}
\def\RC{\eqref{eqn_kalman_filter_gain}}
% ------------------------------------

There is no particular naming. ZA..AB just are your formulas, SG, SH and SK are inside your split-environemnts, RA..RC are the arrow texts; all of them free from &, besides the \underbrace. With this your set of equations and hence your structure reads:

\begin{DispWithArrows*}[format = ll]
    P(t|t)  &=\ZA\\
            &=\ZB \Arrow[i]{\RA}\\% \RA is too long
            &=\ZCa\\
            &=\ZCb\\
            &\begin{split}% line #5, continuing with '+'
                  =\SG
            \end{split}
            \Arrow[i]{\RB}\\
            &\begin{split}
                  =\SH
             \end{split}    
            \Arrow[i]{\RC}\\% unreferenced eqn, yields ??
            &\begin{split}
                =\SK
            \end{split}\\
            &=\ZD\\
            &=\AB\\
    \AA
\end{DispWithArrows*}

A solution to the first arrow text can be obtained by just entering some '\' linebreaks manually, see \def\RA.

For problem 2 I suggest to split the line with \times as indicated in \def\SK, so that the remaining centered part is less of a distraction, hopefully.

As you can see I also entered the [format = ll] statement at the beginning of the DispWithArrows* environment, to make sure alignement as intended. So %\noindent becomes obsolete ... (done)

Some new problems remain, like the touching arrows #2 and #3.

Result after 2 compile runs, at least:

result

Complete code, for your reference:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry} \geometry{a4paper,top=2.5cm,bottom=2.5cm,left=2cm,right=2cm,heightrounded,bindingoffset=5mm}

\usepackage{amsmath,cancel,witharrows}

% --- formulas ----------------------------------------------------
\def\ZA{E\left[\tilde{e}_x(t)\tilde{e}_x^T(t)\right]}
\def\ZB{E\left[\big((I-K(t)C)e_x(t)-K(t)v(t)\big)\big((I-K(t)C)e_x(t)-K(t)v(t)\big)^T\right]}
\def\ZCa{\big(I-K(t)C\big)E[e_x(t)e_x^T(t)]\big(I-K(t)C\big)^T+K(t)E[v(t)v^T(t)]K(t)}
\def\ZCb{\big(I-K(t)C\big)P(t|t-1)\big(I-K(t)C\big)^T+K(t)R_vK^T(t)}
\def\ZD{P(t|t-1)\cancel{-P(t|t-1)C^T K^T(t)}-K(t)CP(t|t-1)\cancel{+P(t|t-1)C^TK^T(t)}}
\def\AB{P(t|t-1)-K(t)CP(t|t-1)\Arrow[i]{factor out $P(t|t-1)$}}
\def\AA{P(t|t)&=\big(I-K(t)C\big)P(t|t-1)}
% --- split groups -------------------------------------------------------
\def\SG{P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        +K(t)CP(t|t-1)C^TK^T(t)+K(t)R_vK^T(t)}
\def\SH{P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
        +K(t)\big(CP(t|t-1)C^T+R_v\big)K^T(t)}
\def\SK{P(t|t-1)-P(t|t-1)C^TK^T(t)-K(t)CP(t|t-1)+\\
    +\underbrace{P(t|t-1)C^T\big(CP(t|t-1)C^T+R_v\big)^{-1}}_{=K(t)}\times \\
    \times\big(CP(t|t-1)C^T+R_v\big)K^T(t)}
% --- arrow texts --------------------------------------------------------
\def\RA{$e_x$ is uncorrelated with\\ $v(t)$ collect\\ $E[e_x(t)e_x^T(t)]$ and\\ $E[v(t)v^T(t)]$}
\def\RB{in the 4th and 5th terms,\\factor out $K(t)$}
\def\RC{\eqref{eqn_kalman_filter_gain}}
% ------------------------------------

\begin{document}
%\noindent
some words
\begin{DispWithArrows*}[format = ll]
    P(t|t)  &=\ZA\\
            &=\ZB \Arrow[i]{\RA}\\% \RA is too long
            &=\ZCa\\
            &=\ZCb\\
            &\begin{split}% line #5, continuing with '+'
                  =\SG
            \end{split}
            \Arrow[i]{\RB}\\
            &\begin{split}
                  =\SH
             \end{split}    
            \Arrow[i]{\RC}\\% unreferenced eqn, yields ??
            &\begin{split}
                =\SK
            \end{split}\\
            &=\ZD\\
            &=\AB\\
    \AA
\end{DispWithArrows*}
some other words

\end{document}
4
  • 1
    Thank you! Your modular way to write the equation is pretty cool, but it's not so feasible in my case. I don't understand the meaning of [format = ll] and "to make sure alignment as intended".
    – Stefano
    Aug 4 at 8:29
  • @Stefano, Fine :) // I refer to page 6 in mirror.marwan.ma/ctan/macros/generic/witharrows/witharrows.pdf , where using this format is explained. It's intended to work as in tables, where you could align individual columns left (l), center (c) or right (r). Sometimes this is a source of error, so I tried to exert some control ;-) // Turned out that the line I split via \times was causing the "wrong alignment".
    – MS-SPO
    Aug 4 at 8:54
  • 1
    @Stefano, about the \def parts, the purpose was more for myself to better see the structure of your table-like part. It might have been that some & got misplaced hidden inside your formulas ... which wasn't :) // If it's useful in a regular document may depend. However, I myself strive for a clear coding here, because it's so easy to do sth wrong AND being unable to find it. // Perhaps it's a good idea to define such substitutes for smaller entities, which occure often, like K(t)CP(t|t-1). Then you'd use the \newcommand-approach, not TeX as I did ;-)
    – MS-SPO
    Aug 4 at 8:58
  • 1
    Ok, thanks! I was misled by the double l :) As you said, the \def parts are very useful in this case but since I'm working on a document of 100+ pages with a lot of equations, it would take too long to create pieces of the equation and organize them in another .tex file. Moreover this a document containing the notes of an exam I have already given :))) There would be no need to change it, but I like to "tinker" with LaTeX. In the future, maybe for a project report or my master thesis, I will definitely use this kind of approach. Thank you again!
    – Stefano
    Aug 4 at 9:40
4

I propose this code, not perfect, but using the fleqn option and setting the value of mathindent.

I added some simplification of the code, and replaced the | for conditional probabilities with \mid which has a better spacing. Also, the pairs \big( … \big) with \bigl(… \bigr) for the same reason. Last, I replaced the split environment, which produced warnings messages, with aligned.

\documentclass[fleqn]{article}
\usepackage[a4paper, vmargin=2.5cm, hmargin=2cm, heightrounded, bindingoffset=5mm, showframe]{geometry}%
\usepackage{amsmath, cancel, witharrows}

\begin{document}

\noindent
some words
\begin{DispWithArrows*}[fleqn, mathindent=1em, wrap-lines, show-nodes]
    P(t\mid t)&=E\left[\tilde{e}_x(t)\tilde{e}_x^T(t)\right]\\
    &=E\left[\bigl((I-K(t)C)e_x(t)-K(t)v(t)\bigr)\bigl((I-K(t)C)e_x(t)-K(t)v(t)\bigr)^T\right] \Arrow[i]{{$e_x$ is uncorrelated with $v(t)$\\ collect $E[e_x(t)e_x^T(t)]$ and $E[v(t)v^T(t)]$}}\\
    &=\bigl(I-K(t)C\bigr)E[e_x(t)e_x^T(t)]\bigl(I-K(t)C\bigr)^T+K(t)E[v(t)v^T(t)]K(t)\\
    &=\bigl(I-K(t)C\bigr)P(t\mid t-1)\bigl(I-K(t)C\bigr)^T+K(t)R_vK^T(t)\\
    &=\begin{aligned}[t]P(t\mid t-1) & -P(t\mid t-1)C^TK^T(t)-K(t)CP(t\mid t-1)+{\,}\\
        & +K(t)CP(t\mid t-1)C^TK^T(t)+K(t)R_vK^T(t)
    \end{aligned}
    \Arrow[i]{in the 4th and 5th terms,\\factor out $K(t)$}\\
    &=\begin{aligned}[t]P(t\mid t-1) & -P(t\mid t-1)C^TK^T(t)-K(t)CP(t\mid t-1)+{}\\
         & +K(t)\big(CP(t|t-1)C^T+R_v\big)K^T(t)
    \end{aligned}
    \Arrow[i]{\eqref{eqn_kalman_filter_gain}}\\
    &=\begin{aligned}[t]P(t &\mid t-1) -P(t\mid t-1)C^TK^T(t)-K(t)CP(t\mid t-1)+{}\\
     & +\underbrace{P(t\mid t-1)C^T\bigl(CP(t\mid t-1)C^T+R_v\bigr)^{-1}}_{=K(t)}\bigl(CP(t\mid t-1)C^T+R_v\bigr)K^T(t)
    \end{aligned}\\
    &=P(t\mid t-1)\cancel{-P(t\mid t-1)C^T K^T(t)}-K(t)CP(t\mid t-1)\cancel{+P(t\mid t-1)C^TK^T(t)}\\
    &=P(t\mid t-1)-K(t)CP(t\mid t-1)\Arrow[i]{factor out $P(t\mid t-1)$}\\
    P(t\mid t)&=\bigl(I-K(t)C\bigr)P(t\mid t-1)
\end{DispWithArrows*}
some other words

\end{document} 

enter image description here

5
  • Thank you Bernard! I will immediately change the | character with \mid, it looks much better. Up to now I wrote all my conditional probabilities like that (^_^;). Same stuff for the parentheses. The aligned environment is neat, but is it possible to align the 2nd row to the first?
    – Stefano
    Aug 4 at 8:34
  • @Stefano: What do you means exactly? All rows are aligned on the = sign. Could you explain more explicitly?
    – Bernard
    Aug 4 at 9:01
  • Yes, for example, look at the 6th line (right below the 5th equal sign). Is there a way to shift the line so that the plus sign is slightly to the right with respect to the equal sign?
    – Stefano
    Aug 4 at 9:49
  • 1
    I see. I had thought it looked fine to align the + sign with the – above (via the aligned environment. But instead, you may put the & in the line above just before the P at the very beginning of the line above.
    – Bernard
    Aug 4 at 9:56
  • Oops, right, I didn't notice the other & sign
    – Stefano
    Aug 4 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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