因此我使用 TikZ 在我的单元格中创建了渐变,这很好。
但我仍然有几个问题:
文本本身的颜色发生了变化,而我只想更改背景颜色。文本下方的水平线也面临同样的问题(它是
\hline
,因为我在表格内)。如果我只是在多列函数中添加
|c|
首字母,垂直线就不会与表格的其余部分对齐。c
相关问题链接:表格中某个单元格的渐变颜色。
答案1
- 使用下面的代码(取自问题中链接的答案)我似乎无法重现提到的第一个问题,因此需要一个最小的工作示例。
下面的代码还展示了如何使用
\vrule
内部!{...}
添加垂直规则。\documentclass{article} \usepackage[table]{xcolor} \usepackage{array} \usepackage{tikz} \usetikzlibrary{calc} % Andrew Stacey's code from % http://tex.stackexchange.com/a/50054/3954 \makeatletter \tikzset{% remember picture with id/.style={% remember picture, overlay, save picture id=#1, }, save picture id/.code={% \edef\pgf@temp{#1}% \immediate\write\pgfutil@auxout{% \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}% }, if picture id/.code args={#1#2#3}{% \@ifundefined{save@pt@#1}{% \pgfkeysalso{#3}% }{ \pgfkeysalso{#2}% } } } \def\savepointas#1#2{% \expandafter\gdef\csname save@pt@#1\endcsname{#2}% } \def\tmk@labeldef#1,#2\@nil{% \def\tmk@label{#1}% \def\tmk@def{#2}% } \tikzdeclarecoordinatesystem{pic}{% \pgfutil@in@,{#1}% \ifpgfutil@in@% \tmk@labeldef#1\@nil \else \tmk@labeldef#1,(0pt,0pt)\@nil \fi \@ifundefined{save@pt@\tmk@label}{% \tikz@scan@one@point\pgfutil@firstofone\tmk@def }{% \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic% \pgfsys@getposition{\pgfpictureid}\save@this@pic% \pgf@process{\pgfpointorigin\save@this@pic}% \pgf@xa=\pgf@x \pgf@ya=\pgf@y \pgf@process{\pgfpointorigin\save@orig@pic}% \advance\pgf@x by -\pgf@xa \advance\pgf@y by -\pgf@ya }% } \newcommand\tikzmark[2][]{% \tikz[remember picture with id=#2] #1;} \makeatother % end of Andrew's code \newcommand\ShadeCell[3]{% \begin{tikzpicture}[overlay,remember picture]% \shade[#3] ( $ (pic cs:#1) + (0pt,8.5pt) $ ) rectangle ( $ (pic cs:#2) + (-0.2pt,-3.5pt) $ ); \end{tikzpicture}% }% \begin{document} \ShadeCell{start1}{end1}{% top color=blue,bottom color=magenta!70,opacity=0.5} \ShadeCell{start2}{end2}{% left color=red,right color=green,opacity=0.5} \begin{tabular}{| l | c |} \hline Uncolored cell & \multicolumn{1}{!{\tikzmark{start1}} c !{\vrule\tikzmark{end1}}}{Colored cell} \\ \hline \multicolumn{1}{!{\vrule\tikzmark{start2}} l !{\vrule\tikzmark{end2}}}{Another colored cell} & Another uncolored cell \\ \hline \end{tabular} \end{document}
答案2
使用{NiceTabular}
,nicematrix
可以很容易地在表格的单元格中添加颜色渐变。
\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\begin{document}
\begin{NiceTabular}{lc}[hvlines]
Uncolored cell
& \Block[tikz={top color=blue,bottom color=magenta!70,opacity=0.5}]{}{} Colored cell \\
\Block[tikz={left color=red,right color=green,opacity=0.5}]{}{} Another colored cell
& Another uncolored cell
\end{NiceTabular}
\end{document}
您需要多次编译。