2

If I want to write two functions f and g vertically in the center of the page I will write like this:

\begin{align*}
\f(x)=\left\{\begin{array}{lll}
                1 & \textnormal{if} & x\in A \\
                2 & \textnormal{} & \textnormal{otherwise}
            \end{array}\right.
\end{align*}

\begin{align*}
\g(x)=\left\{\begin{array}{lll}
                1 & \textnormal{if} & x\in B \\
                2 & \textnormal{} & \textnormal{otherwise}
            \end{array}\right.
\end{align*}

How I write this two functions horizontally, one in the left end another one in the right end ?

4
  • @ Count Zero: How u do this?
    – user31526
    Jun 9, 2012 at 15:44
  • You could put them in a minipage. Jun 9, 2012 at 15:47
  • Oh, sorry, should've posted that... :) If you have inline code, use backticks: `. Alternatively, backticks can be applied to selections with Ctrl+K. If you have full code blocks, either indent every line with 4 spaces or select the block and use the button with the braces above the edit the edit window.
    – Count Zero
    Jun 9, 2012 at 15:47
  • Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it).
    – Joseph Wright
    Jun 9, 2012 at 16:06

3 Answers 3

5

As egreg pointed out, since you are not have any alignment points for the align environment, you could simply use gather* (or equation* since you are converting to a one line equation):

enter image description here

Notes:

  • You should also have a look at the cases (and dcases if using mathtools package).
  • There should never be two subsequent display math equations without some text in between, and you should not leave a blank line (a commented line is fine) between display math environments. Otherwise there is too much vertical space.
  • You can use \text{} as well to get to upright roman text in math mode (with amsmath).

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

% Not sure what these are defined as so adjust as desired
\newcommand*{\f}{F}%
\newcommand*{\g}{G}%

\begin{document}
\begin{gather*}
\f(x)=\left\{\begin{array}{lll}
                1 & \textnormal{if} & x\in A \\
                2 & \textnormal{} & \textnormal{otherwise}
            \end{array}\right.
\quad
\g(x)=\left\{\begin{array}{lll}
                1 & \textnormal{if} & x\in B \\
                2 & \textnormal{} & \textnormal{otherwise}
            \end{array}\right.
\end{gather*}
\end{document}

If you have multiple lines of equations and do require horizontal alignment points, then this can also be done with the align environment.

Notes:

  • The align environment provides multiple rl pairs of equations with spaces between them.
  • The alignat environment provides multiple rl pairs without spaces so spaces would need to be inserted if so desired.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

% Not sure what these are defined as so adjust as desired
\newcommand*{\f}{F}%
\newcommand*{\g}{G}%

\begin{document}
\begin{align*}
\f(x)=\left\{\begin{array}{lll}
                1 & \textnormal{if} & x\in A \\
                2 & \textnormal{} & \textnormal{otherwise}
            \end{array}\right. &&
\g(x)=\left\{\begin{array}{lll}
                1 & \textnormal{if} & x\in B \\
                2 & \textnormal{} & \textnormal{otherwise}
            \end{array}\right.
\end{align*}
\end{document}
2
  • What does align* do that can't be done by gather* (changing && into a spacing command)?
    – egreg
    Jun 9, 2012 at 17:23
  • And equation* is best, obviously. :)
    – egreg
    Jun 9, 2012 at 17:44
3

Since the others have been faster and posted the solutions with align and alignat, I will post the one with minipages (as suggested by Peter Grill). :)

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}

%with minipages

\begin{minipage}{.45\linewidth}
\[
f(x) = \begin{cases}
                1 & \textnormal{if $x\in A$} \\
                2 & \textnormal{otherwise}
            \end{cases}
            \]
\end{minipage}
\begin{minipage}{.45\linewidth}
\[
g(x) =  \begin{cases}
                1 & \text{if $x\in B$} \\
                2 & \textnormal{otherwise}
            \end{cases}
            \]
\end{minipage}
\end{document}

I slightly modified your code (it does not compile - see \f and \g...) and also used cases instead of array.

2

If "horizontally" in this case means side by side, you can use the alignat* environment from »amsmath« (here loaded by »mathtools« from the »mh« bundle).

\documentclass[11pt]{article}
\usepackage{mathtools}   % loads »amsmath«

\begin{document}
  \begin{alignat*}{2}
    f(x)&=\left\{
    \begin{array}{lll}
      1 & \textnormal{if} & x\in A \\
      2 & \textnormal{} & \textnormal{otherwise}
    \end{array}
    \right.
    &\qquad
    g(x)&=\left\{
    \begin{array}{lll}
      1 & \textnormal{if} & x\in B \\
      2 & \textnormal{} & \textnormal{otherwise}
    \end{array}
    \right.
  \end{alignat*}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .