我在 tcolorbox 中使用数组,但数组的列之间出现了不必要的间隙。如何让此数组完美地适合父 tcolorbox 并消除列之间的间隙?
\documentclass[a4paper]{book}
\usepackage[margin=2cm]{geometry}
\usepackage{avant}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{array}
\usepackage{colortbl}
\usepackage{lipsum}
\definecolor{darkgrey}{HTML}{E2E1E6}
%problembox environment
\newenvironment{problembox}
{%
\renewcommand{\arraystretch}{1.5}
\begin{tcolorbox}[
colframe=white,
colback=darkgrey,
boxrule=0pt,
fontupper=\sffamily,
arc=0pt,
top=0pt,
bottom=0pt,
left=0pt,
right=0pt,
boxsep=0pt,
]
\small
\setlength{\tabcolsep}{0pt}% Remove column spacing
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}@{}}
\rowcolor{blue!10}
\textbf{Problem Name} & \textbf{Source} & \textbf{Difficulty} \\
}{%
\end{tabular*}
\end{tcolorbox}
}
\begin{document}
\chapter{Algorithms}
\section{Introduction}
\lipsum[1]
\begin{problembox}
P1 & S1 & Easy \\
P2 & S2 & Difficult \\
P3 & S3 & Normal \\
P4 & S4 & Normal \\
P5 & S5 & Hard \\
\end{problembox}
\lipsum[2]
\end{document}
答案1
如果您确实想将表格放在 环境中tcolorbox
,您可以尝试。该包提供了类似于经典的nicematrix
环境,但使用 PGF/Tikz 为行着色(该环境应在此处与键 一起使用)。{NiceTabular*}
{tabular}
{NiceTabular*}
colortbl-like
\documentclass[a4paper]{book}
\usepackage[margin=2cm]{geometry}
\usepackage{avant}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{array}
% \usepackage{colortbl} % not loaded
\usepackage{lipsum}
\usepackage{nicematrix}
\definecolor{darkgrey}{HTML}{E2E1E6}
%problembox environment
\newenvironment{problembox}
{%
\renewcommand{\arraystretch}{1.5}
\begin{tcolorbox}[
colframe=white,
colback=darkgrey,
boxrule=0pt,
fontupper=\sffamily,
arc=0pt,
top=0pt,
bottom=0pt,
left=0pt,
right=0pt,
boxsep=0pt,
]
\small
\setlength{\tabcolsep}{0pt}% Remove column spacing
\begin{NiceTabular*}{\textwidth}{@{\extracolsep{\fill}}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}@{}}[colortbl-like]
\rowcolor{blue!10}
\textbf{Problem Name} & \textbf{Source} & \textbf{Difficulty} \\
}{%
\end{NiceTabular*}
\end{tcolorbox}
}
\begin{document}
\chapter{Algorithms}
\section{Introduction}
\lipsum[1]
\begin{problembox}
P1 & S1 & Easy \\
P2 & S2 & Difficult \\
P3 & S3 & Normal \\
P4 & S4 & Normal \\
P5 & S5 & Hard \\
\end{problembox}
\lipsum[2]
\end{document}
答案2
只是为了好玩,一个带有tcbraster
(或tcbitemize
)的解决方案。
\documentclass[a4paper]{book}
\usepackage[margin=2cm]{geometry}
\usepackage{avant}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{array}
\usepackage{colortbl}
\usepackage{lipsum}
\definecolor{darkgrey}{HTML}{E2E1E6}
%problembox environment
\newenvironment{problembox}
{%
\renewcommand{\arraystretch}{1.5}
\begin{tcbitemize}[
colframe=white,
colback=darkgrey,
fontupper=\small\sffamily,
sharp corners,
raster columns=3,
raster column skip=0pt,
raster row skip=0pt,
raster equal height=rows,
raster row 1/.style={colback=black!30},
halign=center,
size=small,
boxrule=0pt,
]}
{ \end{tcbitemize}
}
\begin{document}
\chapter{Algorithms}
\section{Introduction}
\lipsum[1]
\begin{problembox}
\tcbitem Problem Name
\tcbitem Source
\tcbitem Difficulty
\tcbitem P1
\tcbitem S1
\tcbitem Easy
\tcbitem P2
\tcbitem S3
\tcbitem Difficult
\tcbitem P3
\tcbitem S3
\tcbitem Normal
\end{problembox}
\lipsum[2]
\end{document}
答案3
(在意识到我最初的答案关注的是与 OP 关心的不同类型的“差距”之后,我重写了这个答案。)
我如何使这个数组完美地适合父 tcolorbox 并消除列之间的间隙?
请注意
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.33\textwidth}@{}}
三列的总宽度为0.99\textwidth
为不是 1.00\textwidth
。相反,两个列间间隙的宽度为0.005\textwidth
,而不是0pt
。要解决此问题,只需将上面显示的代码片段替换为
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
>{\centering\arraybackslash}p{\dimexpr0.333\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.333\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.334\textwidth}@{}}
\documentclass[a4paper]{book}
\usepackage[margin=2cm]{geometry}
\usepackage{avant}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{array}
\usepackage{colortbl}
\usepackage{lipsum}
\definecolor{darkgrey}{HTML}{E2E1E6}
%problembox environment
\newenvironment{problembox}
{%
\renewcommand{\arraystretch}{1.5}
\begin{tcolorbox}[
colframe=white,
colback=darkgrey,
boxrule=0pt,
fontupper=\sffamily,
arc=0pt,
top=0pt,
bottom=0pt,
left=0pt,
right=0pt,
boxsep=0pt,
]
\small
\setlength{\tabcolsep}{0pt}% no inter-column whitespace padding
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
>{\centering\arraybackslash}p{\dimexpr0.333\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.333\textwidth}
>{\centering\arraybackslash}p{\dimexpr0.334\textwidth} @{}}
\rowcolor{blue!10}
\textbf{Problem Name} & \textbf{Source} & \textbf{Difficulty} \\
}{%
\end{tabular*}
\end{tcolorbox}
}
\begin{document}
\chapter{Algorithms}
\section{Introduction}
\lipsum[1]
\begin{problembox}
P1 & S1 & Easy \\
P2 & S2 & Difficult \\
P3 & S3 & Normal \\
P4 & S4 & Normal \\
P5 & S5 & Hard \\
\end{problembox}
\lipsum[2]
\end{document}