我正在尝试将此环境的第三行的背景涂成tabu
红色:
\documentclass{beamer}
\usepackage{tabu}
\usepackage{colortbl}
\begin{document}
\begin{frame}
\begin{tabu}{XX}
\everyrow{\hline} \hline
Row 1 & Row 1 \\
Row 2 & Row 2 \\
\rowcolor{red}
Row 3 & Row 3 \\
Row 4 & Row 4 \\
\end{tabu}
\end{frame}
\end{document}
该rowcolor
命令无效,输出中没有一行具有彩色背景。我很确定这在某个时候(可能是很久以前)曾经起作用,但我知道可能colortbl
(现在)与不兼容tabu
。我也尝试过使用该\taburowcolors
命令,但没有成功。
tabu
在文档内部的环境中为单行着色的推荐方法是什么beamer
?
笔记:我正在使用 XeLaTeX 进行编译,以防出现差异。
答案1
在评论中,您指出您正在使用 tabu 在每一行后插入 \hline。这也可以这样做(如果需要 \ 的可选参数,则必须添加一些调整):
\documentclass{beamer}
\usepackage{colortbl}
\newcommand\tabnewlinewithhline{\tabularnewline\hline}
\newcommand\tabaddhline{\let\\\tabnewlinewithhline}
\newcolumntype\ccwithhline{c>{\tabaddhline}c}
\begin{document}
\begin{frame}
\begin{tabular}{\ccwithhline}
\hline
Row 1 & Row 1 \\
Row 2 & Row 2 \\
\rowcolor{red}
Row 3 & Row 3 \\
Row 4 & Row 4 \\
\end{tabular}
\end{frame}
\end{document}
答案2
下面定义了两个环境,一个mytabular
(类似于tabular
)和一个mytabularx
(类似于tabularx
)环境,它们\hline
在每行之间放置一个,并\hline
在环境的顶部和底部放置两个。它通过本地修补array
包的一些内部来实现这一点。任何嵌套的tabular
或array
环境都不应包含这些\hline
,这对于创建一些特殊的对齐是可取的(例如使用\makecell
需要这个)。在这两个环境中,最后一行不需要以\\
(负责大部分所需代码)结尾。
该机制应该可以工作,但如果有人发现错误,请报告。此外,如果有人认为该机制太笨拙,请随时发表评论并建议更好的方法(我知道,\everycr
但出于我不知道的原因,我没有使用它,所以这可能是修补的可行替代方案array
)\\
。
\documentclass[]{article}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{tabularx}
\usepackage{etoolbox}
\usepackage{environ}
\makeatletter
\newcommand\mypatchcmd[3]%>>>
{%
\patchcmd#1{#2}{#3}{}{\GenericError{}{Patching \string#1\space failed}{}{}}%
}%<<<
\newcommand\mypretocmd[2]%>>>
{%
\pretocmd#1{#2}{}{\GenericError{}{Patching \string#1\space failed}{}{}}%
}%<<<
\newcommand\myapptocmd[2]%>>>
{%
\apptocmd#1{#2}{}{\GenericError{}{Patching \string#1\space failed}{}{}}%
}%<<<
\newcommand*\unpatch@array%>>>
{%
\let \@@array \@@array@original
\let \@arraycr \@arraycr@original
\let \@xarraycr \@xarraycr@original
\let \@xargarraycr \@xargarraycr@original
\let \@yargarraycr \@yargarraycr@original
\let \tabular \tabular@original
\let \endtabular \endtabular@original
\let \array \array@original
\let \endarray \endarray@original
}%<<<
\newcommand*\patch@array%>>>
{%
\mypretocmd\tabular{\unpatch@array}%
\mypretocmd\array{\unpatch@array}%
\myapptocmd\@yargarraycr{\my@hline}%
\mypatchcmd\@xarraycr{\cr}{\cr\my@hline}%
\mypatchcmd\@xargarraycr{\cr}{\cr\my@hline}%
\myapptocmd\@@array{\hline\hline}%
}%<<<
\newcommand*\backup@array%>>>
{%
\let \@@array@original \@@array
\let \@arraycr@original \@arraycr
\let \@xarraycr@original \@xarraycr
\let \@xargarraycr@original \@xargarraycr
\let \@yargarraycr@original \@yargarraycr
\let \tabular@original \tabular
\let \endtabular@original \endtabular
\let \array@original \array
\let \endarray@original \endarray
}%<<<
\newenvironment{mytabular}[2][c]%>>>
{%>>>
\backup@array
\patch@array
\begin{tabular@original}[#1]{#2}%
}%<<<
{%>>>
\crcr
\hline
\hline
\end{tabular@original}%
}%<<<
%<<<
\newcommand\my@hline%>>>
{%
\noalign{\ifnum0=`}\fi
\futurelet\reserved@a\my@xhline
}%<<<
\newcommand\my@xhline%>>>
{%
\ifx\reserved@a\end
\expandafter\my@yhline
\else
\hrule\@height\arrayrulewidth
\ifx\reserved@a\hline
\vskip\doublerulesep
\fi
\expandafter\my@xhline@fin
\fi
}%<<<
\newcommand*\my@xhline@fin%>>>
{%
\ifnum0=`{\fi}%
}%<<<
\newcommand\my@yhline[2]%>>>
{%
\begingroup
\def\tmpa{#2}%
\def\tmpb{mytabular}%
\expandafter
\endgroup
\ifx\tmpa\tmpb
\else
\hrule\@height\arrayrulewidth
\fi
\ifnum0=`{\fi}%
\end{#2}%
}%<<<
\newcommand\mytabularx@expandhelper[2]%>>>
{%
#2#1%
}%<<<
\NewEnviron{mytabularx}%>>>
{%
\backup@array
\patch@array
\edef\BODY{\expandafter\mytabularx@striplast\expandafter{\BODY}}%
\expandafter\mytabularx@expandhelper\expandafter{\BODY}{\begin{tabularx}}%
\crcr
\hline
\hline
\end{tabularx}%
}%<<<
\newcommand\myifempty[1]%>>>
{%
\if\relax\detokenize{#1}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}%<<<
\newcommand\myifnobbackslash[1]%>>>
{%
\myifbbackslash@#1\\\endmyifbbackslash@
}%<<<
\def\myifbbackslash@#1\\#2\endmyifbbackslash@%>>>
{%
\myifempty{#2}%
}%<<<
\newcommand\mytabularx@striplast[1]%>>>
{%
\myifnobbackslash{#1}
{%
\unexpanded{#1}%
}
{%
\mytabularx@striplast@#1\endmytabularx@striplast
}%
}%<<<
\def\mytabularx@striplast@#1\\#2\endmytabularx@striplast%>>>
{%
\myifempty{#2}
{%
\unexpanded{#1}%
}%
{%
\unexpanded{#1\\}%
\mytabularx@striplast{#2}%
}%
}%<<<
\makeatother
\begin{document}
\noindent
\begin{mytabularx}{\linewidth}{XX}
Row 1 & Row 1 \\
Row 2 & Row 2 \\
\rowcolor{red}
Row 3 & Row 3 \\
Row 4 & Row 4 \\
\end{mytabularx}
\bigskip
\begin{mytabular}{ll}
a & b\\
c & d\\
\multicolumn{2}{c}{ab}\\
e & f\\
\begin{tabular}{@{}l@{}}a\\b\end{tabular} & cd \\
\end{mytabular}
\end{document}