xcolor の rowcolor が複数行の括弧をブロックする

xcolor の rowcolor が複数行の括弧をブロックする

行ごとに異なる色が交互に表示される表を作成しようとしていますが、ドキュメントが次のようにコンパイルされていることに気付きました。

色の付いた行は「cases」ステートメントの括弧を「カバー」しますが、色の付いていない行はカバーしません。

行列やその他の大きな括弧で同様の問題がありましたが、積分や合計などの他の括弧では問題ありませんでした。解決策を調べました。ここそしてここですが、どちらも理解できていないし、それが私の問題にどのように関連しているのかもよくわかりません。

私の質問は、これが行を色付けする方法に問題があるかどうか、そしてそうするためのより一般的な方法があるかどうかです。

私のマークアップはここにあります:

\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}

答え1

ここに画像の説明を入力してください

それは\left\{\begin{array}{rl} ... \end{array}\right.おそらくあなたを助けるでしょう:

\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}

答え2

mathtoolsの代わりにロードしamsmath(mathtools は amsmath の拡張です)、たとえば `dcases* 環境を使用します。

\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} 

ここに画像の説明を入力してください

答え3

{NiceTabular}の環境は、nicematrixと同様のツールを提供しますcolortblが、描画には PGF/Tikz を使用します。

その環境を使用すると、必要なものを直接入手できます (ただし、nicematrixPGF/Tikz ノードを使用するため、複数のコンパイルが必要になります)。

\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}

上記コードの出力

関連情報