23

I have this text in LaTeX:

$$
sgn(x)=
\begin{cases}
   -1 & \mbox{za podmínky } x>0\\
    0 & \mbox{za podmínky } x=0\\
    1 & \mbox{za podmínky } x>0
\end{cases}
$$

sgn

But I would like to move the "0" and "1" more to the right. I need to move these numbers just below "1", not to have it below "-".

3
  • 1
    You could add \phantom{-} in front of the 0 and 1
    – dustin
    Jul 19, 2013 at 23:04
  • 3
    Not related to your question, but you should define a new command using \DeclareMathOperator{\sgn}{sgn} at the preamble such that you can use \sgn(x) instead of sgn(x).
    – Francis
    Jul 19, 2013 at 23:46
  • 2
    Also see Why is \[\] preferable to $$?
    – Werner
    Jul 20, 2013 at 1:02

7 Answers 7

22

Simplest is probably to use \phantom{-}1

25

Referencing my chat message from 2013-04-05, here is the definition of the cases environment that accepts one optional argument, the column specification of the left column. Thus writing \begin{cases}[r] the left column is set right-aligned.

This way you actually could even use siunitx’ table typsetting with the S column.

Code

\documentclass{article}
\usepackage{amsmath,siunitx}
\DeclareMathOperator{\sgn}{sgn}
\makeatletter
\renewenvironment{cases}[1][l]{\matrix@check\cases\env@cases{#1}}{\endarray\right.}
\def\env@cases#1{%
  \let\@ifnextchar\new@ifnextchar
  \left\lbrace\def\arraystretch{1.2}%
  \array{@{}#1@{\quad}l@{}}}
\makeatother
\begin{document}
\[
\sgn(x)=
\begin{cases}[c]
   -1 & \text{za podmínky } x>0\\
    0 & \text{za podmínky } x=0\\
    1 & \text{za podmínky } x>0
\end{cases}
 \]

 \[
 \sgn(x)=
 \begin{cases}[r]
    -1 & \text{za podmínky } x>0\\
     0 & \text{za podmínky } x=0\\
     1 & \text{za podmínky } x>0
 \end{cases}
 \]

 \[
 \sgn(x)=
 \begin{cases}[{S[table-format=-1.0]}]
    -1 & \text{za podmínky } x>0\\
     0 & \text{za podmínky } x=0\\
     1 & \text{za podmínky } x>0
 \end{cases}
 \]
\end{document}

Output

enter image description here

3
  • 4
    This definition of cases is absolutely great (I am just tired of doing phantoms ;)). Please consider adding/sending your improvement to the amsmath package. Mar 15, 2016 at 13:20
  • I get Package amsmath Error: Old form `\matrix' should be \begin{matrix}.
    – Nermin
    Aug 11, 2022 at 8:44
  • @Nermin I don't using TeXLive 2022 and amsmath 2022/04/08 v2.17n AMS math features. Aug 11, 2022 at 11:12
9

The cases environment of the amsmath package uses array with the column specification @{}l@{\quad}l@{}. Thus \multicolumn will work, e.g.:

\multicolumn{1}{@{}r@{\quad}}{...}

The alignment can also be influenced by \hfil and \hfill, because array or tabular are using \hfil to align and justify their column types l, c, and r.

\hfill 0 % puts 0 to the right

The following example shows the different methods:

\documentclass{article}

\usepackage{amsmath}

\begin{document}
\[
  \begin{cases}
    \mbox{left/default}   &       \mbox{left/default} \\
    \multicolumn{1}{@{}l@{\quad}}{\mbox{left}} &
    \multicolumn{1}{@{}l@{}}     {\mbox{left}} \\
    %
    \hfil  \mbox{center} & \hfil  \mbox{center} \\
    \multicolumn{1}{@{}c@{\quad}}{\mbox{center}} &
    \multicolumn{1}{@{}c@{}}     {\mbox{center}} \\
    %
    \hfill \mbox{right}  & \hfill \mbox{right} \\
    \multicolumn{1}{@{}r@{\quad}}{\mbox{right}} &
    \multicolumn{1}{@{}r@{}}     {\mbox{right}} \\
    %
    \multicolumn{1}{@{}p{10em}@{\quad}}{Column type p \dotfill\newline with two lines \hrulefill} &
    \multicolumn{1}{@{}p{10em}@{}}     {Column type p \dotfill\newline with two lines \hrulefill} \\
  \end{cases}
\]
\end{document}

Result

Applied to the case of the OP:

\documentclass{article}

\usepackage{amsmath}

\DeclareMathOperator{\sgn}{sgn}

\begin{document}
\[
  \sgn(x)=
  \begin{cases}
         -1 & \mbox{za podmínky } x>0\\
   \hfill 0 & \mbox{za podmínky } x=0\\
   \hfill 1 & \mbox{za podmínky } x>0
  \end{cases}
\]
\end{document}

Result

3
  • I've taken the liberty of editing your text to make its English a bit more idiomatic. I trust you're OK with that.
    – Mico
    Sep 14, 2013 at 17:15
  • @Mico: Thanks, of course I am OK with the corrections. I have also updated the image. Sep 14, 2013 at 17:26
  • Brilliant, just use \hfill for baby cases! Thanks.
    – kcrisman
    Apr 7, 2019 at 2:48
4

Here is an alternative solution, which uses array for better alignment control:

\begin{align*}
    \sgn(x)=\left\{
    \begin{array}{lll}
    -1& \mbox{za podmínky} & x>0\\
    0 & \mbox{za podmínky} & x=0\\
    1 & \mbox{za podmínky} & x>0
    \end{array}\right.
\end{align*}

\begin{align*}
    \sgn(x)=\left\{
    \begin{array}{ccc}
    -1& \mbox{za podmínky} & x>0\\
    0 & \mbox{za podmínky} & x=0\\
    1 & \mbox{za podmínky} & x>0
    \end{array}\right.
\end{align*}

\begin{align*}
    \sgn(x)=\left\{
    \begin{array}{rrr}
    -1& \mbox{za podmínky} & x>0\\
    0 & \mbox{za podmínky} & x=0\\
    1 & \mbox{za podmínky} & x>0
    \end{array}\right.
\end{align*}

Result:
enter image description here

1
  • 1
    @daleif: This old post got upvoted recently, which reminds me: why not make a centered dcases into mathtools? Something like \newcases{cdcases}{\quad}{% $\m@th\hfil\displaystyle{##}$\hfil}{$\m@th\displaystyle{##}$\hfil}{\lbrace}{.} That said, I think the package document should mention \newcases command.
    – Francis
    Nov 5, 2013 at 13:21
3

For a more global take on this, you can adjust the alignment of cases to be right-left rather than the default (left-left). Here's how:

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}% http://ctan.org/pkg/inputenc
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\makeatletter
\def\env@cases{%
  \let\@ifnextchar\new@ifnextchar
  \left\lbrace
  \def\arraystretch{1.2}%
  \array{@{}r@{\quad}l@{}}% Changed from \array{@{}l@{\quad}l@{}}
}
\makeatother
\DeclareMathOperator{\Sign}{sgn}

\begin{document}

\[
  \Sign(x) = \begin{cases}
   -1 & \text{za podmínky } x > 0 \\
    0 & \text{za podmínky } x = 0 \\
    1 & \text{za podmínky } x > 0
  \end{cases}
\]

\end{document}

You'll note that the alignment comes from the array column specification @{}l@{\quad}l@{}, which can be adjusted to suit your needs.

2

Just another simple approach.

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{amsmath}
\DeclareMathOperator{\sgn}{sgn}
\begin{document}
\abovedisplayskip=0pt\relax
\[
    \sgn(x)=
    \left\{
    \begin{aligned}
     -1 & \text{ za podm\'inky } x>0\\
      0 & \text{ za podm\'inky } x=0\\
      1 & \text{ za podm\'inky } x<0
    \end{aligned}
    \right.
\]
\end{document}

enter image description here

1
  • 1
    There is a typo, < should be changed to >! Sep 14, 2013 at 13:03
-1

Before considering the positions of $0$ and $-1$, one should use \cases typographically right:

\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}
\begin{document}
$$
sgn(x)=
\begin{cases}
   -1 & \mbox{za podmínky } x>0\\
    0 & \mbox{za podmínky } x=0\\
    1 & \mbox{za podmínky } x>0
\end{cases}
$$
$$
\mathrm{sgn}(x)=
\begin{cases}
   -1\text{,} & \text{za podmínky  $x>0$;}\\
    0\text{,} & \text{za podmínky  $x=0$;}\\
    1\text{,} & \text{za podmínky $ x>0$.}
\end{cases}
$$

\end{document}
1

You must log in to answer this question.

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