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

載入( mathtoolsmathtoolsamsmath是 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 進行繪圖。

使用該環境,您可以直接獲得所需的內容(但由於使用 PGF/Tikz 節點,因此需要多次編譯nicematrix)。

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

上述程式碼的輸出

相關內容