# Tabularx inside a tcolorbox: problem with column size and alignment

Consider the following code, derived from this answer and which produces the output shown at the bottom of this post.

\documentclass[a4paper, 10pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[listings,skins,theorems,breakable,most]{tcolorbox}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{multicol}

\NewDocumentCommand{\tcbtab}{O{}m+m}{%
\begin{tcolorbox}[%
enhanced,
arc = 0pt,
outer arc = 0pt,
titlerule = 0pt,
boxsep = 0pt,
left = 0pt,
right = 0pt,
top=0pt,
colframe = black,
box align=center,
halign=center,
valign=center,
attach boxed title to top={xshift=0mm,yshift=0mm},
boxed title style={
enhanced,
colback = black,
arc=0pt,
outer arc=0pt,
},
listing only,
title = {\centering\makebox[\linewidth][c]{#2}},#1]
#3%
\end{tcolorbox}%
}

\begin{document}
\lipsum[1]
\tcbtab{Title}{
\begin{tabularx}{\linewidth}{|p{0.5\linewidth}|p{0.5\linewidth}|}
First column & Second column
\end{tabularx}
}
\lipsum[1]
\begin{multicols}{2}
\lipsum[1]
\tcbtab{Title}{
\begin{tabularx}{\linewidth}{|p{0.5\linewidth}|p{0.5\linewidth}|}
First column & Second column
\end{tabularx}
}
\lipsum[1-2]
\end{multicols}
\end{document}


Now that I have my tcolorboxes aligned and centered, I would like to put tables in them. However, the current approach does not give the expected result, the columns being too wide.

I would like to be able to specificy a tabularx where, I could easily specify p{0.2\something}*5 or something like this if I want 5 multiline columns, or p{0.5\something}*2, if I want 2 multiline columns, or p{0.2\something}|p{0.2\something}|c if I want 2 multiline columns and 1 centered column that fills all the remaining space.

The syntax could be a little different, but I would like to be able to easily design boxed tables without spending hours on aligning each column by hand for it to look good. And the result should be compatible with \multicolumn and \multirow. How to achieve this? (And also how to remove the vertical space before and after the array inside the tcolorbox?)

• Hm, why do you use tabularx but are not applying the X column type? Please note that tcolorbox also has a tabularx mode – user31729 Oct 3 '17 at 14:52

Use the tabularx mode of tcolorbox (and remove the boxed title there).

\documentclass[a4paper, 10pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage[skins,theorems,breakable,most]{tcolorbox}

\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage{multicol}

\NewDocumentCommand{\tcbtab}{O{}m+m}{%
\begin{tcolorbox}[%
enhanced,
sharp corners,
outer arc = 0pt,
titlerule = 0pt,
boxsep=0pt,
left=0pt,
right=0pt,
top=0pt,
colframe = black,
box align=center,
halign=center,
valign=center,
attach boxed title to top={xshift=0mm,yshift=0mm},
boxed title style={
enhanced,
colback = black,
arc=0pt,
outer arc=0pt,
},
listing only,
title = {\centering\makebox[\linewidth][c]{#2}},#1]
#3%
\end{tcolorbox}%
}

\NewDocumentCommand{\tcbtabagain}{O{}m+m}{%
\begin{tcolorbox}[%
enhanced,
sharp corners,
right=0pt,
left=0pt,
titlerule = 0pt,
colframe = black,
box align=center,
halign=center,
valign=center,
%    attach boxed title to top={xshift=0mm,yshift=0mm},
%    boxed title style={
%      enhanced,
%      colback = black,
%      arc=0pt,
%      outer arc=0pt,
%    },
listing only,
title = {\centering{#2}},
tabularx={|*{2}{X}|},
#1]
#3%
\end{tcolorbox}%
}

\begin{document}
\lipsum[1]
\tcbtab[tabularx={|X|X|}]{Title}{%
First column & Second column
}
\lipsum[1]
\begin{multicols}{2}
\lipsum[1]
\tcbtabagain{Title}{%
First column & Second column
}
\lipsum[1-2]
\end{multicols}
\end{document}


• If I compile your code as is, I obtain this: imgur.com/vVovIfK (last version of texlive on linux). How to make the first table work? – Vincent Oct 3 '17 at 15:15
• @Vincent: Sorry, I had posted a different version than on my disk. Try again with corrected code, please – user31729 Oct 3 '17 at 15:20
• In the first approach, why there is an empty line? Is there any way to remove it? – Vincent Oct 3 '17 at 15:37
• @Vincent: As I wrote in the answer: Remove the attached title, it is not necessary in my point of view and causes the additional space – user31729 Oct 3 '17 at 15:56
• See question here: tex.stackexchange.com/questions/394401/… . Is there really no way to combine attach title and tabularx? (if that's the case, I guess it's a package bug). Then how to put the title at the bottom? – Vincent Oct 3 '17 at 16:00