LaTeX forum ⇒ Math & ScienceEquations on two columns Topic is solved

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Equations on two columns  Topic is solved

Postby Cham » Sun Jan 11, 2015 11:00 pm

I frequently have to display two small equations on the same line (especially to save space), and want them to be centered. The left equation should be centered in the middle of the left part of the text, while the right equation should be centered on the right part of the text. Currently, I'm unable to achieve this, using the \align and \array environments.

Here's a MWE to show the ugly distribution of equations on a page, that I want to fix.
  1. \documentclass[12pt,letterpaper]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{amsmath}
  4. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  5.  
  6. \begin{document}
  7. A small line of text :
  8. \begin{align}
  9. x &= y^2 - c + d + z^3,
  10. & z &= x^3 + x - y^2.
  11. \end{align}
  12. Just another small line of text :
  13. \begin{align}
  14. x -5 y + 3 &= y^2,
  15. & y^2 &= -\: x^2 + 4 - y^2 - x.
  16. \end{align}
  17. Yet another line :
  18. \begin{align}
  19. A + B + E &= b,
  20. & B - C &= 0.
  21. \end{align}
  22. The global distribution of these equations is ugly !
  23. \end{document}


Here's a preview of the compiled code, with some approximate reference lines :
2equs.jpg
2equs.jpg (30.33 KiB) Viewed 3292 times


The red line is (approximately) the middle of the text. The blue lines are (approximately) the middle of the left and right parts. I want to "force" the equations to be centered on the blue lines.

Any idea about how to achieve what I'm looking for ?

Tags:

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Wed Jan 14, 2015 4:13 pm

So, no idea/suggestions about my query ?

I feel there's certainly a "simple" solution to this in LaTeX (or maybe not).

User avatar
vermiculus
Posts: 3
Joined: Mon Mar 10, 2014 1:10 am

Postby vermiculus » Wed Jan 14, 2015 5:42 pm

(Edit: I misread. The following will keep *alignment* from line to line, but it will not be centered. You may be able to combine \intertext with some other equation environment, but I haven't the time to experiment.)

Use \intertext from the amsmath package:

  1. \documentclass[12pt,letterpaper]{book}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{amsmath}
  4. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  5.  
  6. \begin{document}
  7. \begin{align}
  8. \intertext{A small line of text:}
  9. x &= y^2 - c + d + z^3, & z &= x^3 + x - y^2.
  10. \intertext{Just another small line of text:}
  11. x -5 y + 3 &= y^2, & y^2 &= -\: x^2 + 4 - y^2 - x.
  12. \intertext{Yet another line:}
  13. A + B + E &= b, & B - C &= 0.
  14. \end{align}
  15. The global distribution of these equations is ugly!
  16. \end{document}
  17.  
Last edited by vermiculus on Wed Jan 14, 2015 5:56 pm, edited 1 time in total.

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Wed Jan 14, 2015 5:55 pm

Hmm, this isn't a solution. The distribution of the equations is still horrible (all the = are aligned).

The equations need to be centered on the blue lines defined above.

And the \intertex command isn't an option here, since there are too much equations scattered in the document (the MWE is just an example). And some blocks of text are actually full paragraphs, without equations !

User avatar
Johannes_B
Site Moderator
Posts: 3803
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Wed Jan 14, 2015 6:54 pm

I asked in the TEX.SX chat and egreg provided a solution:
  1. \documentclass{article}
  2. \usepackage{amsmath,environ}
  3.  
  4. \makeatletter
  5. \NewEnviron{dblequation}
  6. {\expandafter\dbl@equation\BODY\@nil}
  7. \def\dbl@equation#1&#2\@nil{%
  8. \begin{equation}
  9. \makebox[\dimexpr\displaywidth-6em]{%
  10. \makebox[.5\displaywidth]{$\displaystyle#1$}%
  11. \makebox[.5\displaywidth]{$\displaystyle#2$}%
  12. }
  13. \end{equation}%
  14. }
  15.  
  16. \begin{document}
  17.  
  18. \noindent\smash{\vrule depth 8cm width 1pt}\hfill
  19. \smash{\vrule depth 8cm width 1pt}\hfill
  20. \smash{\vrule depth 8cm width 1pt}\hfill
  21. \smash{\vrule depth 8cm width 1pt}\hfill
  22. \smash{\vrule depth 8cm width 1pt}\par
  23.  
  24. A small line of text:
  25. \begin{dblequation}
  26. x = y^2 - c + d + z^3, & z = x^3 + x - y^2.
  27. \end{dblequation}
  28. Just another small line of text:
  29. \begin{dblequation}
  30. x -5 y + 3 = y^2, & y^2 = -x^2 + 4 - y^2 - x.
  31. \end{dblequation}
  32. Yet another line:
  33. \begin{dblequation}
  34. A + B + E = b, & B - C = 0.
  35. \end{dblequation}
  36. The global distribution of these equations is ugly!
  37. \end{document}



Please see the further discussion.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Wed Jan 14, 2015 8:18 pm

Hmm, it's working.

Before I use it definitely in my huge document, is there any other (simpler or better) solution, anyone ?

User avatar
Johannes_B
Site Moderator
Posts: 3803
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Wed Jan 14, 2015 8:35 pm

Well, egreg poposed a different solution.

  1. \documentclass{article}
  2. \usepackage{amsmath}
  3.  
  4. \makeatletter
  5. \newenvironment{dblequation}
  6. {\setbox\z@=\vbox\bgroup
  7. \global\let\dbleq@label\@empty
  8. \def\label##1{\gdef\dbleq@label{##1}}%
  9. \halign\bgroup&$\displaystyle##$\cr}
  10. {\crcr\egroup\setbox0=\lastbox
  11. \dimen@=\displaywidth
  12. \advance\dimen@-\wd\z@
  13. \ifdim\dimen@<3em
  14. \@tempswatrue
  15. \else
  16. \@tempswafalse
  17. \fi
  18. \setbox\z@=\hbox{%
  19. \unhbox\z@
  20. \unskip\global\setbox\thr@@=\lastbox
  21. \unskip\global\setbox\@ne=\lastbox
  22. }%
  23. \egroup
  24. \begin{equation}
  25. \ifx\dbleq@label\@empty\else\label{\dbleq@label}\fi
  26. \kern-1.5em
  27. \hbox to\dimexpr.5\displaywidth\if@tempswa-1.5em\fi{\hss\unhbox\@ne\hss}
  28. \hbox to\dimexpr.5\displaywidth\if@tempswa-1.5em\fi{\hss\unhbox\thr@@\hss}
  29. \kern-1.5em
  30. \end{equation}
  31. }
  32. \makeatother
  33.  
  34. \begin{document}
  35.  
  36. \noindent\smash{\vrule depth 8cm width 1pt}\hfill
  37. \smash{\vrule depth 8cm width 1pt}\hfill
  38. \smash{\vrule depth 8cm width 1pt}\hfill
  39. \smash{\vrule depth 8cm width 1pt}\hfill
  40. \smash{\vrule depth 8cm width 1pt}\par
  41.  
  42. A small line of text and \eqref{X}:
  43. \begin{dblequation}\label{X}
  44. x = y^2 - c + d + z^3, & z = x^3 + x - y^2.
  45. \end{dblequation}
  46. Just another small line of text:
  47. \begin{dblequation}
  48. x -5 y + 3 = y^2, & y^2 = -x^2 + 4 - y^2 - x.
  49. \end{dblequation}
  50. Yet another line:
  51. \begin{dblequation}
  52. A + B + E = b, &
  53. f(x)=\begin{cases} 1 & \text{if $x=0$} \\ 0 & \text{if $x\ne0$}\end{cases}
  54. \end{dblequation}
  55. The global distribution of these equations is ugly!
  56. \end{document}



egreg is a real TeX and LaTeX wizard. I guess you have to live with it.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Wed Jan 14, 2015 9:21 pm

The second solution looks much more complicated than the first one (while it's working, I don't understand a single line in it !).

What are its advantages, relative to the first solution ?

User avatar
Johannes_B
Site Moderator
Posts: 3803
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Thu Jan 15, 2015 11:57 am

As egreg already mentioned, you can use labels.
I haven't looked closely at the solutions, though i am going to because there is a lot to learn about the internals. To be quite honest, the solution isn't complicated at all. The user interface is clean as a whistle, you are using ampersands to delimit the column. Just like you are used to. The definition of the whole thing is basically, what every package defines.

But, one question i want you to ask yourself is: Why the hell am i the first one needing this?
I think there is a much simpler thing going on, i.e. this is an xy-Problem. Wanting x but describing y.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

User avatar
Cham
Posts: 894
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Thu Jan 15, 2015 4:35 pm

Labels appear to be working with the first solution too.

But I didn't tested both solutions in depths yet.


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 3 guests