问题:

问题:

我正在尝试创建一个具有交替行颜色的表格圆角。

到目前为止,我结合了两个答案来实现这一目标:

这种方法总体上效果很好,但问题是交替行颜色方法填充的是正方形,而不是圆角,而且行颜色“泄漏”到了表格边框之外。请参见下图的左上角和左下角。

有没有简单的方法可以解决这个问题?也许可以使用命名tikz节点来剪切其外部的所有内容?

问题:

问题

梅威瑟:

\documentclass[border=10pt,10pt,table]{standalone}

\usepackage{tikz}
\usepackage{xcolor}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}
  \begin{tikzpicture}
  \node (thetable) [inner sep=0pt] {
    \begin{tabularx}{\textwidth}{>{\raggedright\arraybackslash\hsize=1.4\hsize}X>{\raggedright\arraybackslash\hsize=0.6\hsize}X}
      \arrayrulecolor{white}
      \textbf{Column 1} & \textbf{Column 2} \\ \hline
      Some text. & Some text. \\ \hline
      Some text. & Some text.
    \end{tabularx}
  };
  \draw [rounded corners=.5em] (thetable.north west) rectangle (thetable.south east);
  \end{tikzpicture}
\end{document}

答案1

设置rounded corners=.5em为整体的选项tikzpicture并用作clip节点选项。如果要更改,line width还必须将新值设置为选项tikzpicture

请注意,line width如果表格(包括边框)适合,也会影响表格宽度\textwidth

\documentclass[border=10pt,tikz,table]{standalone}
%\documentclass{article}
%\usepackage[table]{xcolor}
%\usepackage{tikz}
\usepackage{booktabs}
\usepackage{tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}
  \noindent\begin{tikzpicture}[line width=.1mm,rounded corners=.5em]
  \node(thetable) [clip,inner sep=.5\pgflinewidth] {
    \begin{tabularx}{\dimexpr\textwidth-2\pgflinewidth\relax}{>{\raggedright\arraybackslash\hsize=1.4\hsize}X>{\raggedright\arraybackslash\hsize=0.6\hsize}X}
      \arrayrulecolor{white}
      \textbf{Column 1} & \textbf{Column 2} \\ \hline
      Some text. & Some text. \\ \hline
      Some text. & Some text.
    \end{tabularx}
  };
  \draw ([xshift=.5*\pgflinewidth,yshift=-.5*\pgflinewidth]thetable.north west) 
    rectangle ([xshift=-.5*\pgflinewidth,yshift=.5*\pgflinewidth]thetable.south east);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

或者您可以使用来even odd rule填充表格周围的区域:

\documentclass[border=10pt,tikz,table]{standalone}
%\documentclass{article}
%\usepackage[table]{xcolor}
%\usepackage{tikz}
\usepackage{booktabs}
\usepackage{tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}
  \noindent\begin{tikzpicture}[line width=.1mm]
  \node (thetable) [inner sep=.5\pgflinewidth] {
    \begin{tabularx}{\dimexpr\textwidth-2\pgflinewidth\relax}{>{\raggedright\arraybackslash\hsize=1.4\hsize}X>{\raggedright\arraybackslash\hsize=0.6\hsize}X}
      \arrayrulecolor{white}
      \textbf{Column 1} & \textbf{Column 2} \\ \hline
      Some text. & Some text. \\ \hline
      Some text. & Some text.
    \end{tabularx}
  };
  \fill[white,even odd rule]
    (current bounding box.north west)rectangle(current bounding box.south east)
    {[rounded corners=.5em]([xshift=.5*\pgflinewidth,yshift=-.5*\pgflinewidth]thetable.north west) 
      rectangle ([xshift=-.5*\pgflinewidth,yshift=.5*\pgflinewidth]thetable.south east)};
  \draw[rounded corners=.5em] ([xshift=.5*\pgflinewidth,yshift=-.5*\pgflinewidth]thetable.north west) 
    rectangle ([xshift=-.5*\pgflinewidth,yshift=.5*\pgflinewidth]thetable.south east);
  \end{tikzpicture}
\end{document}

答案2

如果您只需要一个框架表格,tcolorbox可以帮助您(正如 cmhughes 所建议的那样)。

它有一个tabularx选项,可以接受 tabularx 标题定义,也可以接受colortblxcolor命令来为单元格、行或列着色。

array除了 之外,还必须加载tabularxxcolor(或) 。colortbltcolorbox

如果使用rowcolors或,则在定义中包含选项rowcolor会很方便(见下图)。clip uppertcolorbox

\documentclass[tikz,border=10pt,10pt,table]{standalone}
\usepackage{array,tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\usepackage[most]{tcolorbox}

\begin{document}
  \rowcolors{2}{tablerow1}{tablerow2}

  \begin{tcolorbox}[enhanced, notitle, clip upper, 
         tabularx={>{\raggedright\arraybackslash\hsize=1.4\hsize}X%
                   >{\raggedright\arraybackslash\hsize=0.6\hsize}X}]
     \arrayrulecolor{white}
     \textbf{Column 1} & \textbf{Column 2} \\ \hline
     Some text. & Some text. \\ \hline
     Some text. & Some text.
  \end{tcolorbox}    
\end{document}

在此处输入图片描述

如果没有clip upper选择,结果是:

在此处输入图片描述

更新

如果code必须在表格启动之前应用某些操作,请使用选项 tabularx*={<code>}{<preamble>}而不是tabularx

\documentclass[tikz,border=10pt,10pt,table]{standalone}
\usepackage{array,tabularx}
\definecolor{tablerow1}{RGB}{225,217,205}
\definecolor{tablerow2}{RGB}{236,229,221}

\usepackage[most]{tcolorbox}

\begin{document}
\rowcolors{2}{tablerow1}{tablerow2}
\begin{tcolorbox}[enhanced, notitle, clip upper,
       tabularx*={\renewcommand*{\arraystretch}{1.4}}%
                 {>{\raggedright\arraybackslash\hsize=1.4\hsize}X%
              >{\raggedright\arraybackslash\hsize=0.6\hsize}X}]
    \arrayrulecolor{white}
    \textbf{Column 1} & \textbf{Column 2} \\ \hline
      Some text. & Some text. \\ \hline
      Some text. & Some text.
\end{tcolorbox}
\end{document}

在此处输入图片描述

答案3

具有(≥ 6.18)。您需要多次编译(因为{NiceTabular}在后台使用 PGF/Tikz 节点)。nicematrixnicematrix

\documentclass[border=10pt,10pt,table]{standalone}

\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{X[14,l]X[6,l]}[rounded-corners]
\CodeBefore
  \rowcolors[RGB]{1}{225,217,205}{236,229,221}
\Body
  \Block[rounded-corners,draw,transparent]{*-*}{}
  \RowStyle{\bfseries}
  Column 1 & Column 2 \\ \Hline[color=white]
  Some text. & Some text. \\ \Hline[color=white]
  Some text. & Some text.
\end{NiceTabular}

\end{document}

上述代码的输出

相关内容