为表格行着色:是什么原因导致颜色溢出表格边距?如何修复?

为表格行着色:是什么原因导致颜色溢出表格边距?如何修复?

以下是有问题的表格:

桌子图片

正如我们所见,颜色溢出了表格边缘。

  1. 是什么原因造成的?
  2. 如何修复?最好采用微创手术。

我目前还没有进一步降低 MWE 以增加该解决方案对当前情况有效的可能性(lipsum部分已被删除)。

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf, singlelinecheck=false]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)
\usepackage{threeparttable}

\makeatletter\usepackage{microtype}\g@addto@macro\@verbatim{\microtypesetup{activate=false}}\makeatother%

\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{O}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
} %to make entire row boldface/color with one command instead of one at a time

\begin{document}

\begin{table}[!htbp]
\setlength{\extrarowheight}{1.5ex}
\centering
\begin{threeparttable}
    \caption{Title for your table goes here \vspace{-0.7em}}
    \begin{tabular}{@{} Nc|Oc|Oc|Oc|Oc @{}}
        \Xhline{0.1em}
        \rowstyle{\bfseries\cellcolor{cyan!70}} %here is where the colouring takes place
        {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\\[1ex]
        \Xhline{0.07em}
        $x^2 = 4py$ & \makecell{up if $p > 0$ \\ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\\
        \Xcline{1-5}{0.03em}%horisontal line
        $y^2 = 4px$ & \makecell{right if $p > 0$\\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\\
        \Xhline{0.08em}
        \end{tabular}
    \label{table: label for table}
\end{threeparttable}
\end{table}
        
\end{document}

答案1

我建议使用,\columncolor 因为您对单元格两侧的颜色悬垂有可选参数,并结合中和\rowcolors{2}{white}{white}列标题后的颜色:

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabu} %for \cellcolor to work inside \rowstyle
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)

\makeatletter\usepackage{microtype}\g@addto@macro\@verbatim{\microtypesetup{activate=false}}\makeatother%
\usepackage{floatrow, caption}
\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{O}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
} %to make entire row boldface/color with one command instead of one at a time
\usepackage{lipsum}

\begin{document}

\lipsum[11]

\begin{table}[!htbp]
\captionsetup{singlelinecheck=false}\setlength{\extrarowheight}{2ex}
    \centering
    \ttabbox{\rowcolors{2}{white}{white}\begin{tabular}{@{}>{\columncolor{cyan!70}[0pt][\tabcolsep]}Nc|*{3}{>{\columncolor{cyan!70}}Oc|}>{\columncolor{cyan!70}[\tabcolsep][0pt]}Oc @{}}
        \Xhline{0.08em}
        %\rowstyle{\bfseries\cellcolor{cyan!70}}
        {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\\[1.5ex]
        \Xhline{0.05em}
       $x^2 = 4py$ & \makecell{up if $p > 0$ \\ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\\[1.5ex]
        \Xhline{0.03em}%horizontal line
        $y^2 = 4px$ & \makecell{right if $p > 0$\\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\\[1.5ex]
        \Xhline{0.08em}
        \end{tabular}}{%
    \caption{A very very long title for your table goes here and nowhere else. }
    \label{table: label for table}}
\end{table}

\end{document} 

在此处输入图片描述

答案2

利用{NiceTabular}及其nicematrix内置工具来为行、单元格和列着色,您可以直接获得预期的结果。

但是,为了能够{NiceTabular}在 内使用{threeparttable},必须首先使用 的专用钩子threeparttable

\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother

在 2020/10/01 之前的 LaTeX 版本中,钩子系统不可用,因此您必须改用以下方法完成该工作etoolbox

\usepackage{etoolbox}
\makeatletter
\AtBeginEnvironment{threeparttable}{\TPT@hookin{NiceTabular}}
\makeatother

对于完整的 MWE:

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf, singlelinecheck=false]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)
\usepackage{threeparttable}
\usepackage{nicematrix}

\makeatletter\usepackage{microtype}\g@addto@macro\@verbatim{\microtypesetup{activate=false}}\makeatother%

\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{O}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
} %to make entire row boldface/color with one command instead of one at a time


\makeatletter
\AddToHook{env/threeparttable/begin}
{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}}
\makeatother

\begin{document}

\begin{table}[!htbp]
\setlength{\extrarowheight}{1.5ex}
\centering
\begin{threeparttable}
    \caption{Title for your table goes here \vspace{-0.7em}}
    \begin{NiceTabular}{@{} Nc|Oc|Oc|Oc|Oc @{}}
    \CodeBefore
      \rowcolor{cyan!70}{1}
    \Body
        \Xhline{0.1em}
        \rowstyle{\bfseries} %here is where the colouring takes place
        {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\\[1ex]
        \Xhline{0.07em}
        $x^2 = 4py$ & \makecell{up if $p > 0$ \\ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\\
        \Xcline{1-5}{0.03em}%horisontal line
        $y^2 = 4px$ & \makecell{right if $p > 0$\\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\\
        \Xhline{0.08em}
    \end{NiceTabular}
    \label{table: label for table}
\end{threeparttable}
\end{table}
        
\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容