Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

So I've created a latex command using newcommand. This is its definition

\newcommand{\twopartdef}[3]
{
\left\{
    \begin{array}{ll}
        #1 & \mbox{if } #2 \\
        #3 & \mbox{otherwise}
    \end{array}
\right
}

But when I try to use the command with this example

f$(x)$ = \twopartdef{$\lbrace \alpha \rbrace}{$x >$ 10}{\lbrace \beta \rbrace}

Latex tells me that it added the symbol $ because it was unmatched..

What am doing wrong? What corrections should I make?

share|improve this question

migrated from stackoverflow.com Jul 17 '12 at 17:18

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 4 down vote accepted

I assume you are writing this entire array construction in a display math environment (since it's at least two lines in height. As such, you don't need any math-switching delimiters inside your usage:

enter image description here

\documentclass{article}
\newcommand{\twopartdef}[3]{%
  \left\{
    \begin{array}{ll}
      #1 & \mbox{if } #2 \\
      #3 & \mbox{otherwise}
    \end{array}
  \right.
}
\begin{document}
\[
  f(x) = \twopartdef{\{ \alpha \}}{x > 10}{\{ \beta \}}
\]
\end{document}
​

You have a \arraycolsep gap between the extensible left brace and the start of your "two part definition". If you want to remove that, use the array column specification {@{}ll}. Also, I've used \right. (not just \right) to end off the extensible brace pair.

amsmath provides a cases environment that does something similar. Here's your definition using that:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\twopartdef}[3]{%
  \begin{cases}
    #1 & \text{if #2} \\
    #3 & \text{otherwise}
  \end{cases}
}
\begin{document}
\[
  f(x) = \twopartdef{\{ \alpha \}}{$x > 10$}{\{ \beta \}}
\]
\end{document}

For completeness, the entire condition is set in \text, which requires you to wrap x>10 in math mode again.

share|improve this answer
    
Thanks for your explanation! Also, I was aware of the second solution you suggested, just wanted to understand why it wasn't working. –  Joseph Elcid Jul 17 '12 at 16:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.