0

I'm having a problem with formatting a tcolorbox so that it contains a table and its width fits the width of the table. A MWE of my code is as follows:

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}

\begin{document}
    \begin{tcolorbox}[skin = enhanced jigsaw, tabularx = {p{2cm}p{3cm}}, capture = hbox, breakable = false, title = {Test}, label = {test}, center]
        \textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
        1 & 2\tabularnewline
        3 & 4\tabularnewline
        5 & 6\tabularnewline
    \end{tcolorbox}
\end{document}

LuaLaTeX throws the following errors when trying to compile it:

Argument of \TX@get@body has an extra }. \end{tcolorbox}
Paragraph ended before \TX@get@body was complete. \end{tcolorbox}
Missing } inserted. \end{tcolorbox}

The same code compiles fine without the hbox option but I can't seem to find any information on the incompatibility between it and tabularx. Setting or unsetting explicit table column widths (e.g., with p) has no effect. Please advise.

  • 1
    please provide a small test document that shows the error, not a fragment. Note that tabularx = {cc} looks wrong, there should always be at least one X column otherwise tabularx can do nothing useful. – David Carlisle Nov 15 '18 at 11:20
  • Added a full MWE. – Arets Paeglis Nov 15 '18 at 11:33
1

I'm not sure what the real problem is. Using tabularx without an X column is wrong to begin with.

Perhaps defining a new style modeled on tabularx is what you're looking for:

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}

\makeatletter
\tcbset{
  tabular/.style={
    boxsep=\z@,top=\z@,bottom=\z@,leftupper=\z@,rightupper=\z@,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,
    before upper={\arrayrulecolor{tcbcol@frame}\def\arraystretch{1.1}%
      \tcb@hack@currenvir\tabular{#1}},
    after upper=\endtabular\arrayrulecolor{black}},
}
\makeatother

\begin{document}

\begin{tcolorbox}[
  skin = enhanced jigsaw,
  tabular = {p{2cm}p{3cm}},
  capture = hbox,
  breakable = false,
  title = {Test},
  label = {test},
  center
]
\textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
1 & 2\tabularnewline
3 & 4\tabularnewline
5 & 6\tabularnewline
\end{tcolorbox}

\end{document}

enter image description here

| improve this answer | |
  • You're right. And thanks, I believe this solves my problem. – Arets Paeglis Dec 16 '18 at 16:02
0

What about this:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\usepackage{tabularx, colortbl, booktabs}
\begin{document}

\begin{tcolorbox}[tabularx = {XX}, breakable = false, title = {Test}, label = {test}, center]
    \textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
    1 & 2\tabularnewline
    3 & 4\tabularnewline
    5 & 6\tabularnewline
\end{tcolorbox}

\noindent X\dotfill X\par
\end{document}

enter image description here

Is this what is asked for ?

\documentclass{standalone}
\usepackage{booktabs}
%\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}

\begin{document}
    \begin{tcolorbox}[skin = enhanced jigsaw, breakable = false,
      capture=hbox, title = {Test}, label = {test}, center]
      \begin{tabular}{p{2cm}p{3cm}}
        \textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
        1 & 2\tabularnewline
        3 & 4\tabularnewline
        5 & 6
      \end{tabular}
    \end{tcolorbox}
\end{document}

enter image description here

| improve this answer | |
  • I'm afraid this doesn't solve my problem, since in my actual code the table columns have set widths. – Arets Paeglis Nov 15 '18 at 11:34
  • I don't know what your problem is... – user4686 Nov 15 '18 at 11:35
  • See OP. I can't use the X specifier for my table and that doesn't solve the problem anyway, since it changes column widths instead of the width of the box. – Arets Paeglis Nov 15 '18 at 11:38
  • The edited example is my current fallback solution but it's not optimal since I have to manually fit paddings etc., whereas using the tabularx option "fills" the colorbox with the table and overall seems to provide better integration. – Arets Paeglis Nov 15 '18 at 11:47
  • @AretsPaeglis if you do not use X then use tabular not tabularx, tabularx without an X column can not do anything to achieve the specified width so it will just act like tabular but give underfull box warnings. – David Carlisle Nov 15 '18 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.