Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have this equation. It is quite wide, and I've had to split things over two lines. However, the part to the left of the = is quite long, and it's causing me some issues.

I've posted a MWE here. This produces the following that I could just about live with (it is 7.6 pt too wide apparently):

This is OK

The problem is that when I have the same equation in my full document, it adds extra space after the = and is now definitely not OK:

This is not OK

I presume this is a consequence of the longer equation number. Can anyone suggest a way to get my equation fitted into the margins? I have thought of breaking the line after the = sign, but I'd like the = sign on the lower line to line up and that's the one that is spilling off to the right. I have also thought of breaking before the =, and aligning the = signs at the left hand end, which would be the best solution, but I honestly can't work out how to do it. I suspect I need some combination of gather/split or something, but it's doing my head in!

\documentclass[12pt, a4paper, oneside, fleqn]{report}

% Page geometry etc
%----------------------------
\usepackage{setspace}                % allow different line spacing
\renewcommand{\topfraction}{0.85}
\renewcommand{\textfraction}{0.1}
\usepackage[top=2.5cm, left=3.5cm, bottom=2.5cm, right=2.5cm, includehead]{geometry}
\geometry{headheight=28pt, headsep=18pt}

% Maths stuff
%------------
\usepackage{amsmath}

\begin{document}

\onehalfspacing
\mathindent=\parindent

The equation below is too wide for the page margins. The question is how best to format it so that it looks sensible but doesn't spill off the right hand margin?
\begin{alignat}{2}
{\textstyle \frac{1}{2}} \textrm{Cov}(\Delta_{i,p1}-\Delta_{i,p2},\Delta_{j,p1}-\Delta_{j,p2})
& = && {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p1})
+ {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p2})  - \notag \\
&   && {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})
- {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p1}) \\
& = && \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2}
- \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{alignat}


\end{document}

UPDATE: I've just had a go with gather, but now my equation numbers are way off to the right, even in my test document:

\begin{gather}
{\textstyle \frac{1}{2}} \textrm{Cov}(\Delta_{i,p1}-\Delta_{i,p2},\Delta_{j,p1}-\Delta_{j,p2}) \notag \\
\begin{alignat}{2}
& = {} && {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p1})
+ {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p2})  - \notag \\
&   && {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})
- {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p1}) \\
& = {} && \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2}
- \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{alignat}
\end{gather}

gather

share|improve this question
    
You can use \tfrac{}{} instead of {\textstyle \frac{}{} }. –  Torbjørn T. May 23 at 9:42
    
@TorbjørnT. thanks, that's a useful aside :) –  FionaSmith May 23 at 9:48
    
Please don't recommend \textrm{Cov}, that is wrong on so many levels. Define \Cov via \DeclareMathOperator\Cov{Cov} and use \Cov –  daleif May 23 at 9:52
    
@daleif that's a bit harsh ;-) you can only do what you know how to do! But, yes, that is a great suggestion - I'd already done that for \DeclareMathOperator\Tr{Tr} but this text is older. –  FionaSmith May 23 at 10:01
1  
@FionaSmith, the problem is two fold: (1) many people do what you do and ignores it, thus we educate you. (2) Other people may come by this question at some point and trust what ever on this site, thus we need to make sure readers know what to do –  daleif May 23 at 10:54

6 Answers 6

up vote 3 down vote accepted

The answer by @JF Meier can be edited easily to correct the alignment of the '=' signs by using

\begin{align}
{\textstyle \frac{1}{2}} \textrm{Cov}(\Delta_{i,p1}-\Delta_{i,p2},&\Delta_{j,p1}-\Delta_{j,p2}) \notag \\
 = & {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p1})
+ {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p2})  - \notag \\
&    {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})
- {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p1}) \\
= & \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2}
- \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{align}
share|improve this answer
    
This gives wrong spacing around the equals sign. (Which can be corrected by ={} &.) –  Torbjørn T. May 23 at 10:02
    
Thanks, this gives the best-looking output in my opinion –  FionaSmith May 23 at 10:21
    
@TorbjørnT. thanks yes I'd already made that change myself! –  FionaSmith May 23 at 10:27

I'd go with align, the easiest way is masking the width of the common left hand side, rather than fixing an arbitrary alignment point.

\documentclass[12pt, a4paper, oneside, fleqn]{report}

% Page geometry etc
%----------------------------
\usepackage[top=2.5cm, left=3.5cm, bottom=2.5cm, right=2.5cm, includehead]{geometry}
\geometry{headheight=28pt, headsep=18pt}

% Maths stuff
%------------
\usepackage{mathtools}

\DeclareMathOperator{\Cov}{Cov}

\setlength{\mathindent}{\parindent}

\begin{document}
The equation below is too wide for the page margins. The question is how best 
to format it so that it looks sensible but doesn't spill off the right hand margin?
\begin{align}
{\textstyle \frac{1}{2}} \textrm{Cov}(\Delta_{i,p1}-\Delta_{i,p2},&\Delta_{j,p1}-\Delta_{j,p2}) \notag \\
 = & {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p1})
+ {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p2})  - \notag \\
&    {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})
- {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p1}) \\
= & \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2}
- \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{align}
Above is Chaplin's code, below it's mine.
\begin{align}
\mathmakebox[3\mathindent][l]{
  \tfrac{1}{2} \Cov(\Delta_{i,p1}-\Delta_{i,p2},\Delta_{j,p1}-\Delta_{j,p2})
}\notag \\
 ={}& \tfrac{1}{2}\Cov(\Delta_{i,p1},\Delta_{j,p1}) +
      \tfrac{1}{2}\Cov(\Delta_{i,p2},\Delta_{j,p2}) - {} \notag \\
    & \tfrac{1}{2}\Cov(\Delta_{i,p1},\Delta_{j,p2}) -
      \tfrac{1}{2}\Cov(\Delta_{i,p2},\Delta_{j,p1}) \\[\jot]
 ={}& \Cov(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2} -
      \Cov(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{align}


\end{document}

Note the vertical spacing between the rows containing the numbers, that helps in distinguishing the two block.

You can also easily change the apparent width of the left hand side, here set to three times the math indent. If you don't want to load mathtools just for that, the same appearance is obtained by

\hspace{3\mathindent}
\lefteqn{
  \hspace{-3\mathindent}
  \tfrac{1}{2} \Cov(\Delta_{i,p1}-\Delta_{i,p2},\Delta_{j,p1}-\Delta_{j,p2})
}\notag \\

enter image description here

share|improve this answer
    
Thank you for a very detailed answer as usual! I had got rid of the \mathindent setting as I didn't need it now my eqn is nice and short, so in fact your code looked similarly spaced to @Chaplins when I pasted it in my doc. Like @dalief's solution it is more flexible than Chaplin's but I already accepted that one. In particular, I really like the extra vertical space, by the way. –  FionaSmith May 23 at 10:56
    
Just one thing though: could you explain what \makemathbox is doing that is different from \MoveEqLeft? They seem to have a similar outcome? I don't quite understand the latter as the doc says it "moves the first line left" but the first line prints the same in all these solutions. –  FionaSmith May 23 at 10:59
    
@FionaSmith I'm not really into all of mathtools' tricks. ;-) But \MoveEqLeft only allows using em units. –  egreg May 23 at 11:02
    
Right, that would explain why when I tried to substitute in 3\mathindent to see if it ended up looking the same as yours, it didn't work! –  FionaSmith May 23 at 11:03

I'm with JF Meier on this, but here is how I'd to it. Note the minus belong at the start of the line in displayed math, not the end.

\documentclass{report}
\usepackage{amsmath,mathtools}
\DeclareMathOperator\Cov{Cov}
\begin{document}
\begin{align}
  \MoveEqLeft[3] 
  \tfrac{1}{2}
  \Cov(\Delta_{i,p1}-\Delta_{i,p2},\Delta_{j,p1}-\Delta_{j,p2}) 
  \notag \\
  = {} &
  \tfrac{1}{2}\Cov(\Delta_{i,p1},\Delta_{j,p1}) +
  \tfrac{1}{2}\Cov(\Delta_{i,p2},\Delta_{j,p2}) \notag
  \\
  & - \tfrac{1}{2}\Cov(\Delta_{i,p1},\Delta_{j,p2}) -
  \tfrac{1}{2}\Cov(\Delta_{i,p2},\Delta_{j,p1})
  \\
  = {} & \Cov(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2} -
  \Cov(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{align}
\end{document}

enter image description here


Edit, as to the pull back, I think the image below explains it. In the top image the vertical line illustrate where the alignment is, notice how the two subsequent lines are indented compared to where the alignment is.

What \MoveEqLeft does is the same as in the upper image, but instead of indenting line 2 and 3, we do a negative indent (we pull it back) on the first line, giving the impression that line 2 and 3 are indented. (mathematicians are lazy ;-)

enter image description here

share|improve this answer
    
Thanks, it's a good solution, even if I don't quite get what the \MoveEqLeft does - I'll look at documentation. I prefer the look of the other solution so accepted that, but this one works perfectly too. Thanks also for the suggestion to move the minus sign, I had wondered as it looked a bit weird stuck on the end. –  FionaSmith May 23 at 10:26
    
OK, I see what I didn't see before, that the [3] controls the indent, so I now have your solution looking more like the other one. So in some ways a better solution as more flexible. I already accepted that one though and it seems mean to unaccept. –  FionaSmith May 23 at 10:52
    
By the way, I am sure you are correct that it should be [3] and not [8] or something, but I think it looks better within the whole document to have these equations aligned a bit like the previous equation –  FionaSmith May 23 at 10:53
    
the [3] is the number of em's to pull back. One of the style books I follow recommend aligning to the out most left, and indenting subsequent lines by 2em (2em from the left to the left of the =). \MoveEqLeft does the equivalent, align to the left and pull back the first line. Here I use [3] beause the alignment point is at the right of the =. Also I like this better as it does not waste so much space below the first line as your original. –  daleif May 23 at 10:58
1  
The easiest way to understand \MoveEqLeft (defined in mathtools) is that it replaces & in an align environment, so that the beginning of the first line should be aligned with the = sign, except the alignment point is shifted 3 em to the left. –  Bernard May 23 at 11:08

Just to add my own gather solution in case it helps anyone in the future - which works as long as I didn't need the equation numbers on the aligned section

\begin{gather}
\tfrac{1}{2} \Cov(\Delta_{i,p1}-\Delta_{i,p2},\Delta_{j,p1}-\Delta_{j,p2}) \\
\begin{alignedat}{2}
& = {} && \tfrac{1}{2}\Cov(\Delta_{i,p1},\Delta_{j,p1})
+ \tfrac{1}{2}\Cov(\Delta_{i,p2},\Delta_{j,p2})  - {} \\
&   && \tfrac{1}{2}\Cov(\Delta_{i,p1},\Delta_{j,p2})
- \tfrac{1}{2}\Cov(\Delta_{i,p2},\Delta_{j,p1}) \notag \\
& = {} && \Cov(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2}
- \Cov(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2} 
\end{alignedat}
\end{gather}

As it happens I'd made a note to remove the extra equation number anyway, but I'd prefer it on the bottom line than the top

share|improve this answer

I would suggest something like this:

\begin{align}
{\textstyle \frac{1}{2}} \textrm{Cov}(\Delta_{i,p1}-\Delta_{i,p2},&\Delta_{j,p1}-\Delta_{j,p2}) \notag \\
& =  {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p1})
+ {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p2})  - \notag \\
&    {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})
- {\textstyle \frac{1}{2}}\textrm{Cov}(\Delta_{i,p2},\Delta_{j,p1}) \\
& =  \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1=p2}
- \textrm{Cov}(\Delta_{i,p1},\Delta_{j,p2})|_{p1 \ne p2}
\end{align}
share|improve this answer
    
Thanks, call me picky, but I don't like the third line starting where the = sign is - I'd like it aligned with the stuff after the = sign in the row above. Hence alignat which seem to be making everything complicated. Maybe I'm wrong to want that mathematically speaking. I'm sure @egreg will be along soon to tell me off ;-) –  FionaSmith May 23 at 9:43
    
You can add some space by \quad or \hspace after the & –  JF Meier May 23 at 11:02

Another suggestion: put the first left-hand side on a single line of its own, and starting slightly left aligned to the first = sign. See the question Multiline equation with LHS alone on first line? for how to achieve this.

share|improve this answer
    
Thanks! I can't quite work out what the code you suggest is doing, but from your comments to another answer on that question it seems that the \MoveEqLeft suggested by @dalief is similar and I prefer the look of one of the other solutions posted. –  FionaSmith May 23 at 10:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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