Math & ScienceEquations on two columns

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

Equations on two columns

Post by Cham »

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.

Code: Select all

\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
2equs.jpg (30.33 KiB) Viewed 16011 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 ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Equations on two columns

Post by Cham »

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

Equations on two columns

Post by vermiculus »

(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:

Code: Select all

\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.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Equations on two columns

Post by Cham »

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: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Equations on two columns

Post by Johannes_B »

I asked in the TEX.SX chat and egreg provided a solution:

Code: Select all

\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}

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

Re: Equations on two columns

Post by Cham »

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: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Equations on two columns

Post by Johannes_B »

Well, egreg poposed a different solution.

Code: Select all

\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.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Equations on two columns

Post by Cham »

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: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Equations on two columns

Post by Johannes_B »

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

Re: Equations on two columns

Post by Cham »

Labels appear to be working with the first solution too.

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