4

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?

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

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

4

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.

  • 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

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.