colortbl 和 makecell 的問題

colortbl 和 makecell 的問題

這是一個簡單的例子。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{colortbl}
\usepackage{multirow, makecell}

\begin{document}
\begin{tabular}{c}
  longer text\\
  \cellcolor{teal}\\
  \cellcolor{teal}\multirowcell{-2}[0ex][r]{short\\short}
%   \cellcolor{teal}\multirow{-2}*{short} % is ok but can't break line or set alignment
\end{tabular}
\end{document}

在此輸入影像描述

當多行單元格中的文字短於上面單元格的文字時,單元格的著色效果不佳。multirow可以,但我想在單元格內斷線並設定對齊方式。

由於程式碼將由 python 生成,且列的寬度無法在程式中訪問,也無法設定為適當的值,因此不考慮 、pinwarrayinX等選項。tabularx

答案1

使用時,w{c}{...}列類型很簡單:

\documentclass{article}
\usepackage[table]{xcolor}  % it load colortbl
\usepackage{makecell}

\begin{document}
\begin{tabular}{w{c}{5em}}
  longer text       \\
\rowcolor{teal}
\makecell{short\\short}
\end{tabular}
\end{document}

在此輸入影像描述

附錄: 使用新tabularray包也很簡單:

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}  % with tabularray the colortbl is not needed

\begin{document}
\begin{tblr}{c}
  longer text       \\
  \SetRow{cyan!30}
{short\\short}
\end{tblr}
\end{document}

或者

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}  

\begin{document}
\begin{tblr}{row{2}={cyan!30,c}, c}
  longer text       \\
{short\\short}
\end{tblr}
\end{document}

在這兩種情況下,結果是相同的:

在此輸入影像描述

答案2

此軟體包(≥ 4.0) 提供了以與和nicematrix相容的方式為單元格、行和列著色的工具:multirowmakecell

\documentclass{article}
\usepackage{xcolor}
\usepackage{multirow, makecell}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{c}[code-before = \rowcolor{teal!75}{2,3}]
 longer text\\
 \\
 \multirowcell{-2}[0ex][r]{shorter \\ short}
\end{NiceTabular}
\end{document} 

在某些縮放等級下,您不會在某些 PDF 檢視器中看到細白線(例如請參閱 Bernard 的答案)。

上面程式碼的結果

答案3

Makecell 可能會遇到表格中彩色儲存格的問題。作為解決方法,我建議\Centerstackstackengine普通的\multirow.無關:xcolor使用 option加載[table],您不必加載,colortbl因為前者會為您加載。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multirow, makecell}
\usepackage[usestackEOL]{stackengine}

\begin{document}

\begin{tabular}{c}
  longer text\\
  \cellcolor{teal!75}\\
  \cellcolor{teal!75}\multirowcell{-2}[0ex][r]{shorter \\ short}
% \cellcolor{teal}\multirow{-2}*{short} % is ok but can't break line or set alignment
\end{tabular}
\qquad
\begin{tabular}{c}
  longer text\\
  \cellcolor{teal!75}\\
  \cellcolor{teal!75}\multirow{-2}{*}{\Centerstack[r]{shorter \\ short}}
\end{tabular}

\end{document} 

在此輸入影像描述

答案4

大衛的解決方案

使用嵌套表格

\newcommand{\minitab}[2][l]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
\cellcolor{teal}\multirow{-2}*{\minitab[r]{short\\short}

相關內容