3

I am trying to make a table with different colors alternating per row, and I found that my document was compiling like this:

Rows with colors 'cover' the bracket of a 'cases' statement, but non-colored rows do not.

I've had similar issues with matrices and other large braces, but not others like integrals and sums. I've looked at the solutions on here and here, but I'm not sure that I understand either one, or how it's relevant to my problem.

My questions is whether or not this is an issue with the way I am coloring coloring the rows, and if there was a more conventional way to do so.

My markup is here:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\begin{document}

\begin{table}[h]

\renewcommand{\arraystretch}{1.5}%
\rowcolors{2}{}{gray!10} 
\begin{tabular}{p{3cm}|p{5cm}}

    \textbf{Problem} & \\
    With Color & $n! = 
    \begin{cases} 
        1 & \text{if } n = 0 \\
        (n-1)! &  \text{otherwise}
    \end{cases}$ \\

    Without Color & $n! = 
    \begin{cases} 
        1 & \text{if } n = 0 \\
        (n-1)! &  \text{otherwise}
    \end{cases}$ \\

\end{tabular}
\end{table}
\end{document}
Alex
  • 31
  • 1

3 Answers3

1

enter image description here

it works with \left\{\begin{array}{rl} ... \end{array}\right. maybe it will help you:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\usepackage{array,cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}

\begin{document}
    \begin{table}[h]
\renewcommand{\arraystretch}{1.5}%
\rowcolors{2}{}{gray!10}
\begin{tabular}{p{3cm}|>{$}S{p{5cm}}<{$}}
\textbf{Problem}    & \\
        With Color  & n! = \left\{\begin{array}{rl}
                            1       & \text{if } n = 0 \\[-1ex]
                            (n-1)!  &  \text{otherwise}
                            \end{array}\right. \\
    Without Color   & n! = \begin{cases}
                            1       & \text{if } n = 0 \\
                            (n-1)!  &  \text{otherwise}
                            \end{cases} 
\end{tabular}
    \end{table}
\end{document}
Zarko
  • 296,517
0

Load mathtools in the place of amsmath (mathtools is an extension of amsmath) and use, say, the `dcases* environment:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{mathtools}
\begin{document}

\begin{table}[h]
\renewcommand{\arraystretch}{1.5}%
\rowcolors{2}{}{gray!10}
\begin{tabular}{p{3cm}|p{5cm}}
    \textbf{Problem} & \\
    With Color & $n! =
    \begin{dcases*}
        1 & if $ n = 0 $ \\
        (n-1)! & otherwise
    \end{dcases*}$ \\
\end{tabular}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350
0

The environment {NiceTabular} of nicematrix provides tools similar to those of colortbl but using PGF/Tikz for the drawing.

Using that environment, you have directly what you wish (but you need several compilations since nicematrix uses PGF/Tikz nodes).

\documentclass{article}
\usepackage{amsmath}
\usepackage{nicematrix}
\begin{document}

\begin{table}[h] \renewcommand{\arraystretch}{1.5}% \begin{NiceTabular}{p{3cm}|>{\arraybackslash}p{5cm}}% [code-before = \rowcolors{2}{}{gray!10}] \textbf{Problem} & \ With Color & $n! = \begin{cases} 1 & \text{if } n = 0 \ (n-1)! & \text{otherwise} \end{cases}$ \ Without Color & $n! = \begin{cases} 1 & \text{if } n = 0 \ (n-1)! & \text{otherwise} \end{cases}$ \ \end{NiceTabular} \end{table} \end{document}

Output of the above code

F. Pantigny
  • 40,250