6

I would like to have an environment that puts two equations side by side. The following works but Is there a way to make this a dedicated newenvironment?

  \documentclass[10pt]{article}
    \begin{document}
    \begin{minipage}{0.5\linewidth}
      \begin{equation}
        y_1(x)=x^2
      \end{equation}
    \end{minipage}
    \hspace{0.5cm}
    \begin{minipage}{0.5\linewidth}
      \begin{equation}
        y_2(x)=2x+1
      \end{equation}
    \end{minipage}
  \end{document}

My attempt is the folowing:

\newenvironment{sidebyside}[1]
{%
  \begin{minipage}{#1\linewidth}
  \csname align*\endcsname
}{%
  \csname endalign*\endcsname
  \end{minipage}
}

Which gives the error:

LaTeX Error: \begin{minipage} on input line ... ended by \end{sidebyside}.
4
  • Use \minipage and \endminipage. – egreg Jan 18 '15 at 0:47
  • How would this work with the argument #1? – arynhard Jan 18 '15 at 0:51
  • Just use it normally e.g. \minipage{#1\linewidth}, I think. – cfr Jan 18 '15 at 2:34
  • not the question, but in the first example, 0.5\linewidth + 0.5\linewidth + 0.5cm is going to give you an overfull box .5cm too wide. plus the paragraph indent, unless latex breaks it to two lines (which it may since you don't end the first minipage with a %; i didn't try). – barbara beeton Jan 18 '15 at 14:41
3

Maybe a simpler syntax will help you:

\documentclass{article}
\usepackage{amsmath}
\usepackage{lipsum} % just for the example

\newlength{\sbswidth}
\newenvironment{sidebysideeq}[1][0.5]
 {\[% start equation
  \setlength{\sbswidth}{#1\linewidth}%
  \minipage{\sbswidth}
  \centering
  $\!\aligned
 }
 {\endaligned$\endminipage\]}
\newcommand{\breaksideeq}{%
  \endaligned$\endminipage
  \setlength{\sbswidth}{\dimexpr\linewidth-\sbswidth}%
  \minipage{\sbswidth}
  \centering
  $\!\aligned
}

\begin{document}

\lipsum*[2]
\begin{sidebysideeq}
  y_1(x)=x^2
\breaksideeq
  y_2(x)=2x+1
\end{sidebysideeq}
\lipsum*[3]
\begin{sidebysideeq}[0.3]
  y_1(x)&=x^2\\
  y_2(x)&=x^3-x
\breaksideeq
  y_3(x)=2x+1+a+b+c+d+e+f+g
\end{sidebysideeq}
\lipsum[2]
\end{document}

The optional argument to sidebysideeq tells the fraction of \linewidth reserved for the left part, default is 0.5.

enter image description here

One can easily add a key-value interface for setting more aspects of the environment: here I use a key for the left fraction and for the reciprocal vertical alignment. The default values are equivalent to left=0.5 and align=c. The possible values for align are c, t or b (but the last one is probably not really useful).

\documentclass{article}
\usepackage{amsmath,keyval}
\usepackage{lipsum} % just for the example

\makeatletter
\define@key{SBS}{left}{\def\SBS@left{#1}}
\define@key{SBS}{align}{\def\SBS@align{#1}}
\def\SBS@left{0.5}
\def\SBS@align{c}

\newlength{\sbswidth}
\newenvironment{sidebysideeq}[1][]
 {\[% start equation
  \setkeys{SBS}{#1}%
  \setlength{\sbswidth}{\SBS@left\linewidth}%
  \minipage[\SBS@align]{\sbswidth}
  \centering
  $\!\aligned[\SBS@align]
 }
 {\endaligned$\endminipage\]}
\newcommand{\breaksideeq}{%
  \endaligned$\endminipage
  \setlength{\sbswidth}{\dimexpr\linewidth-\sbswidth}%
  \minipage[\SBS@align]{\sbswidth}
  \centering
  $\!\aligned[\SBS@align]
}
\makeatother

\begin{document}
\lipsum*[2]
\begin{sidebysideeq}
  y_1(x)=x^2
\breaksideeq
  y_2(x)=2x+1
\end{sidebysideeq}
\lipsum*[3]
\begin{sidebysideeq}[left=0.3,align=t]
  y_1(x)&=x^2\\
  y_2(x)&=x^3-x
\breaksideeq
  y_3(x)=2x+1+a+b+c+d+e+f+g
\end{sidebysideeq}
\lipsum[2]
\end{document}

enter image description here

If you want to support \tag, then a more complex approach should be taken.

\documentclass{article}
\usepackage{amsmath,keyval,environ}
\usepackage{lipsum} % just for the example

\makeatletter
\define@key{SBS}{left}{\def\SBS@left{#1}}
\define@key{SBS}{align}{\def\SBS@align{#1}}
\def\SBS@left{0.5}
\def\SBS@align{c}

\newlength{\sbswidth}
\NewEnviron{sidebysideeq}[1][]{%
  \par\centering
  \setkeys{SBS}{#1}%
  \penalty\predisplaypenalty
  \if\SBS@align t\vskip-\baselineskip\vskip\prevdepth\fi
  \setlength{\sbswidth}{\SBS@left\linewidth}%
  \expandafter\make@sidebysideeq\BODY\@nil
}

\def\make@sidebysideeq#1\breaksideeq#2\@nil{%
  \begin{minipage}[\SBS@align]{\sbswidth}
  \vskip-\baselineskip\vskip\abovedisplayskip
  \begin{align*}#1\end{align*}
  \vskip\belowdisplayskip
  \end{minipage}%
  \setlength{\sbswidth}{\dimexpr\linewidth-\sbswidth}%
  \begin{minipage}[\SBS@align]{\sbswidth}
  \vskip-\baselineskip\vskip\abovedisplayskip
  \begin{align*}#2\end{align*}
  \vskip\belowdisplayskip
  \end{minipage}
}
\makeatother


\begin{document}
\lipsum*[2]
\begin{sidebysideeq}[align=t]
  y_1(x)=x^2
\breaksideeq
  y_2(x)=2x+1
\end{sidebysideeq}
\lipsum*[3]
\begin{sidebysideeq}[left=0.3,align=t]
  y_1(x)&=x^2 \tag{*}\\
  y_2(x)&=x^3-x
\breaksideeq
  y_3(x)=2x+1+a+b+c+d+e+f+g
\end{sidebysideeq}
\lipsum[2]
\end{document}

enter image description here

7
  • Would you mind explaining how this differs from Gonzalo Medina's solution? It looks to have more lines of code that achieve the same thing. I am not an expert at latex, thus I am here, but it looks like there is some automation going on in there. Could you elaborate for learning purposes? – arynhard Jan 18 '15 at 16:08
  • @arynhard Not very different; but with this you use just one environment and specify the length only once. Besides, I use aligned, which is less expensive than align*. – egreg Jan 18 '15 at 16:10
  • I see it now, this is a more elegant solution. In fact I have been trying to figure out how I could achieve this. Having one environment is a lot cleaner. Thank you for the solution. – arynhard Jan 18 '15 at 16:13
  • Do you know how I might align each equation to the top of the environment. In some cases one side of sidebysideeq will have a lot more lines of math, and the other side gets vertically centered to it. Can I make this optional? – arynhard Jan 18 '15 at 16:19
  • One comment, this breaks of I use tag in the environment. Which is important for me. – arynhard Jan 18 '15 at 16:25
5

\begin{minipage}, \end{minipage} won't work in the definition of the environment and will trigger an error

\begin{minipage} on input line 16 ended by \end{sidebyside}.

due to the way align* is processed (it reads the content more than once for calculating the alignments); when this internal processing finishes, the \begin{minipage} will incorrectly be matched to the \end{sidebyside} and the error occurs. If you use equation instead of align, then you can use \begin{minipage}, \endminipage, for example.

Here's one possibility, using \minipage, \end{minipage} instead:

\documentclass[10pt]{article}
\usepackage{amsmath}

\newenvironment{sidebyside}[1]
{%
  \minipage{#1\linewidth}
  \csname align*\endcsname
}{%
  \csname endalign*\endcsname
  \endminipage
}

\begin{document}

\noindent
\begin{sidebyside}{0.5}
  y_1(x)=x^2
\end{sidebyside}%
\begin{sidebyside}{0.5}
  y_2(x)=2x+1
\end{sidebyside}

\end{document}

enter image description here

7
  • 1
    Perfect, thank you. Would you mind explaining why \minipage works but \begin{minipage} does not? Also, I am not too clear why the csname is necessary for align. – arynhard Jan 18 '15 at 1:10
  • @Mico that's because I made some redundancy in the code. I'll fix it in some minutes. – Gonzalo Medina Jan 18 '15 at 1:17
  • @arynhard answer fixed! – Gonzalo Medina Jan 18 '15 at 1:21
  • @Mico answer fixed! – Gonzalo Medina Jan 18 '15 at 1:21
  • @arynhard \csname \endcsname is required since the name has the star *. It wouldn't be necessary for align, but it is required for align*. – Gonzalo Medina Jan 18 '15 at 1:26
4

You can use environ package and lead a simple life ;)

\documentclass[10pt]{article}
\usepackage{environ}
\NewEnviron{sidebyside}[1]
{%
  \begin{minipage}{#1\linewidth}
  \begin{equation}
  \BODY
  \end{equation}
  \end{minipage}%
}
\begin{document}

\noindent
\begin{sidebyside}{0.5}
  y_1(x)=x^2
\end{sidebyside}%
\begin{sidebyside}{0.5}
  y_2(x)=2x+1
\end{sidebyside}

\end{document}

enter image description here

One can make the width option optional too.

\documentclass[10pt]{article}
\usepackage{environ}
\NewEnviron{sidebyside}[1][0.5]
{%
  \begin{minipage}{#1\linewidth}
  \begin{equation}
  \BODY
  \end{equation}
  \end{minipage}%
}
\begin{document}

\noindent
\begin{sidebyside}
  y_1(x)=x^2
\end{sidebyside}%
\begin{sidebyside}
  y_2(x)=2x+1
\end{sidebyside}

\end{document}

so that \begin{sidebyside} takes 0.5\linewidth unless you specify it like \begin{sidebyside}[0.4]

1
  • 1
    You can replace equation environment with align* too here. – user11232 Jan 18 '15 at 1:23

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.