## LaTeX forum ⇒ Math & Science ⇒ Equations on two columns Topic is solved

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

### Equations on two columns  Topic is solved

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.
\documentclass[12pt,letterpaper]{book}\usepackage[T1]{fontenc}\usepackage{amsmath}\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry} \begin{document}A small line of text :	\begin{align}		x &= y^2 - c + d + z^3,		& z &= x^3 + x - y^2.	\end{align}Just another small line of text :	\begin{align}		x -5 y + 3 &= y^2,		& y^2 &= -\: x^2 + 4 - y^2 - x.	\end{align}Yet another line :	\begin{align}		A + B + E &= b,		& B - C &= 0.	\end{align}The global distribution of these equations is ugly !\end{document}

Here's a preview of the compiled code, with some approximate reference lines : 2equs.jpg (30.33 KiB) Viewed 6040 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:

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm
So, no idea/suggestions about my query ?

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

vermiculus
Posts: 3
Joined: Mon Mar 10, 2014 1:10 am
(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:

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

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 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 !

Johannes_B
Site Moderator
Posts: 4186
Joined: Thu Nov 01, 2012 4:08 pm
I asked in the TEX.SX chat and egreg provided a solution:
\documentclass{article}\usepackage{amsmath,environ} \makeatletter\NewEnviron{dblequation}  {\expandafter\dbl@equation\BODY\@nil}\def\dbl@equation#1&#2\@nil{%   \begin{equation}   \makebox[\dimexpr\displaywidth-6em]{%     \makebox[.5\displaywidth]{$\displaystyle#1$}%     \makebox[.5\displaywidth]{$\displaystyle#2$}%   }   \end{equation}%} \begin{document} \noindent\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\par A small line of text:\begin{dblequation}x = y^2 - c + d + z^3, & z = x^3 + x - y^2.\end{dblequation}Just another small line of text:\begin{dblequation}x -5 y + 3 = y^2, & y^2 = -x^2 + 4 - y^2 - x.\end{dblequation}Yet another line:\begin{dblequation}A + B + E = b, & B - C = 0.\end{dblequation}The global distribution of these equations is ugly!\end{document}

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

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm
Hmm, it's working.

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

Johannes_B
Site Moderator
Posts: 4186
Joined: Thu Nov 01, 2012 4:08 pm
Well, egreg poposed a different solution.

\documentclass{article}\usepackage{amsmath} \makeatletter\newenvironment{dblequation}  {\setbox\z@=\vbox\bgroup     \global\let\dbleq@label\@empty     \def\label##1{\gdef\dbleq@label{##1}}%     \halign\bgroup&$\displaystyle##$\cr}  {\crcr\egroup\setbox0=\lastbox     \dimen@=\displaywidth     \advance\dimen@-\wd\z@     \ifdim\dimen@<3em       \@tempswatrue     \else       \@tempswafalse     \fi     \setbox\z@=\hbox{%       \unhbox\z@       \unskip\global\setbox\thr@@=\lastbox       \unskip\global\setbox\@ne=\lastbox     }%   \egroup   \begin{equation}   \ifx\dbleq@label\@empty\else\label{\dbleq@label}\fi   \kern-1.5em   \hbox to\dimexpr.5\displaywidth\if@tempswa-1.5em\fi{\hss\unhbox\@ne\hss}   \hbox to\dimexpr.5\displaywidth\if@tempswa-1.5em\fi{\hss\unhbox\thr@@\hss}   \kern-1.5em   \end{equation}}\makeatother \begin{document} \noindent\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\hfill\smash{\vrule depth 8cm width 1pt}\par A small line of text and \eqref{X}:\begin{dblequation}\label{X}x = y^2 - c + d + z^3, & z = x^3 + x - y^2.\end{dblequation}Just another small line of text:\begin{dblequation}x -5 y + 3 = y^2, & y^2 = -x^2 + 4 - y^2 - x.\end{dblequation}Yet another line:\begin{dblequation}A + B + E = b, &    f(x)=\begin{cases} 1 & \text{if $x=0$} \\ 0 & \text{if $x\ne0$}\end{cases}\end{dblequation}The global distribution of these equations is ugly!\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.

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 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 ?

Johannes_B
Site Moderator
Posts: 4186
Joined: Thu Nov 01, 2012 4:08 pm
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.

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm
Labels appear to be working with the first solution too.

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