# How would I make a table with boxed caption?

How would I make a table with boxed caption like this in which the width of the box adjusted to the size of the table and the height to the title.

• Welcome to TeX SX! Could you post what you've tried? – Bernard Mar 22 at 23:47

## 1 Answer

With the help of caption, floatrow and tabularx, I can propose this:

\documentclass[a4paper]{article}
\usepackage{caption, booktabs, tabularx, cellspace, floatrow}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{3pt}
\addparagraphcolumntypes{X}
\usepackage[table, svgnames]{xcolor}
\DeclareCaptionFormat{colour}{\begin{tabularx}{\linewidth}{Sl >{\columncolor{CornflowerBlue}[1em][1.9em]}S{X}}
\cellcolor{NavyBlue} #1 &#3%
\end{tabularx}}
\DeclareCaptionFont{white}{\color{white}}

\begin{document}
\setcounter{section}{1}
\counterwithin{table}{section}

\begin{table}
\sffamily\centering
\renewcommand{\tabularxcolumn}[1]{>{\arraybackslash}m{#1}}
\captionsetup{font={white, bf, sf}, format =colour}
\floatsetup{captionskip=6pt}
\setlength{\tabcolsep}{1em}
\ttabbox{\caption{Title of my table. Blah blah blah blah blah blah}}%
{\begin{tabular}{*{6}{c}}
\textbf{group} & \textbf{one} & \textbf{two} & \textbf{three} & \textbf{four} & \textbf{sum}\\
\midrule
red & 1 & 2 & 3 & 4 & 10 \\
green & 1 & 2 & 3 & 4 & 10 \\
blue & 1 & 2 & 3 & 4 & 10 \\
\bottomrule
\end{tabular}}
\end{table}

\end{document}


• My always many compliments :-). – Sebastiano Mar 23 at 22:59
• Thank you for your kind appreciation, @Sebastiano! – Bernard Mar 23 at 23:17