T-F-S / tcolorbox

A LaTeX package to create highly customizable colored boxes.
http://www.ctan.org/pkg/tcolorbox
LaTeX Project Public License v1.3c
213 stars 15 forks source link

\NewTColorBox does not work with tabularx? #274

Closed Heziode closed 3 months ago

Heziode commented 3 months ago

I am trying to create a custom environment using tabularx.

Here is my code:

\documentclass{article}
\usepackage{array,tabularx,colortbl}
\usepackage[table]{xcolor}
\usepackage{tcolorbox}
\usepackage{caption}

%%% Color definition
\definecolor{gray-200}{HTML}{e5e7eb}
\definecolor{gray-500}{HTML}{6b7280}
\definecolor{gray-600}{HTML}{4b5563}
\definecolor{gray-800}{HTML}{1f2937}
\definecolor{slate-50}{HTML}{f8fafc}

%%% TColorBox definition

\tcbuselibrary{%
  documentation,theorems,breakable,skins,xparse%
}

\tcbset{callout/.style={%
    breakable,
    fonttitle=\sffamily\bfseries\fontsize{10.5pt}{15pt},
    enlarge top by=1mm,
    separator sign dash,
    label separator=-,
    colback=white,
    boxrule=0.25mm,
    titlerule=0mm,
    arc=6pt,
    top=2mm,
    toptitle=2mm,
    bottomtitle=2mm,
    bottom=2mm,
    left=6mm,
    right=2mm,
    before skip=10pt,
    after skip=10pt,
    enhanced,
    clip upper,
    before title={%
      \hypersetup{hidelinks}%
      \tcbset{%
        color definition=white,
        color hyperlink=white,
      }%
    }%
  }%
}%

\tcbset{
  tablebox/.style={
    callout,
    enhanced,
    breakable,
    colframe=gray-200,
    arc=6pt,
    colbacktitle=slate-50,
    coltitle=gray-500,
    toptitle=2mm,
    bottomtitle=-2mm,
    boxrule=0.25mm,
    colupper=gray-800
  }
}

\NewTColorBox{mytablebox}{%
  callout,
  enhanced,
  breakable,
  colframe=gray-200,
  arc=6pt,
  colbacktitle=slate-50,
  coltitle=gray-500,
  toptitle=2mm,
  bottomtitle=-2mm,
  boxrule=0.25mm,
  colupper=gray-800
}

\begin{document}

\begin{table}
\centering
\rowcolors{1}{white}{slate-50}
\captionsetup[table]{font={bf,sf,color=gray-600}}
\setlength\extrarowheight{.5em}
\begin{tcolorbox}[tablebox,title={\captionof{table}{Table Title}\label{tab:title}},tabularx={>{\centering\arraybackslash}X|>{\centering\arraybackslash}X}]
  GK & Paul Robinson \\[.5em]
  LB & Lucus Radebe \\[.5em]
  DC & Michael Duberry \\[.5em]
  DC & Dominic Matteo \\[.5em]
  RB & Didier Domi \\[.5em]
  MC & David Batty \\[.5em]
  MC & Eirik Bakke \\[.5em]
  MC & Jody Morris \\[.5em]
  FW & Jamie McMaster \\[.5em]
  ST & Alan Smith \\[.5em]
  ST & Mark Viduka \\[.5em]
  \hline
  ST & Alan Smith \\[.5em]
  ST & Mark Viduka \\[.5em]
\end{tcolorbox}
\end{table}

Table~\ref{tab:title} works.

\begin{table}
\centering
\rowcolors{1}{white}{slate-50}
\captionsetup[table]{font={bf,sf,color=gray-600}}
\setlength\extrarowheight{.5em}
\begin{mytablebox}[title={\captionof{table}{Table Title2}\label{tab:title2}},tabularx={>{\centering\arraybackslash}X|>{\centering\arraybackslash}X}]
  GK & Paul Robinson \\[.5em]
  LB & Lucus Radebe \\[.5em]
  DC & Michael Duberry \\[.5em]
  DC & Dominic Matteo \\[.5em]
  RB & Didier Domi \\[.5em]
  MC & David Batty \\[.5em]
  MC & Eirik Bakke \\[.5em]
  MC & Jody Morris \\[.5em]
  FW & Jamie McMaster \\[.5em]
  ST & Alan Smith \\[.5em]
  ST & Mark Viduka \\[.5em]
  \hline
  ST & Alan Smith \\[.5em]
  ST & Mark Viduka \\[.5em]
\end{mytablebox}
\end{table}

Table~\ref{tab:title2} does not works.
\end{document}

Using a tcbset to define a style works. However, when I attempt to create a tcolorbox environment using the same styles, I got a compilation error:

LaTeX cmd: Invalid argument type 'c' in environment 'mytablebox'.
Environment mytablebox undefined.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced alignment tab character &.
Misplaced \noalign.
\hline ->\noalign
Misplaced alignment tab character &.
Misplaced alignment tab character &.
\begin{table} on input line 107 ended by \end{mytablebox}.
\begin{table} on input line 107 ended by \end{mytablebox}.

So the question is: does \NewTColorBox should works with tabularx?

muzimuzhi commented 3 months ago

You use \NewTColorBox with wrong syntax.

This works.

% O{} means one optional argument with empty default value,
% see "texdoc usrguide" for more info
\NewTColorBox{mytablebox}{ O{} }{
  callout,
  enhanced,
  breakable,
  colframe=gray-200,
  arc=6pt,
  colbacktitle=slate-50,
  coltitle=gray-500,
  toptitle=2mm,
  bottomtitle=-2mm,
  boxrule=0.25mm,
  colupper=gray-800,%
  #1% the two percent characters make sure "#1" is NOT surrounded by spaces
}
Heziode commented 3 months ago

Thank you,

It works. I only have a strange white line (or cut?) of the box when it goes to the next page:

CleanShot 2024-03-13 at 13 23 03

And the table is not fully displayed The corresponding PDF: nicetab.pdf

muzimuzhi commented 3 months ago

You have that "strange white line" with tcolorbox env too, and it's caused by \captionof. Setting \captionsetup[table]{font={bf,sf,color=gray-600}, skip=0pt} solves the problem.

Update: Oh I misunderstood your problem.

This is a bug tracker so for further usage questions Q&A forums like tex.stackexchange.com is preferable.

Heziode commented 3 months ago

Ok thank you

muzimuzhi commented 3 months ago

I only have a strange white line (or cut?) of the box when it goes to the next page:

CleanShot 2024-03-13 at 13 23 03

Not only the strange white space, but also not all tabular rows are typeset, with a warning

Package tcolorbox Warning: The upper box part has become overfull on input line
 131.

Setting \begin{mytablebox}[breakable=false, ...] works. Since content of a float (here table environment) cannot contain page breaks, setting breakable=true for tcolorboxes in floats will never work and seems to even cause problems.