# Table caption inside tabular environment

I would like to write a table with latex which looks as similar as possible to this one here but I do not know how to add the caption to the header of the table as seen down below:

I reach as far as this here (see code down below) with only a "normal" standardized caption as we are used to from latex.

\documentclass[oneside,a4paper]{article}

\usepackage{mathpazo}

\usepackage[table,hideerrors]{xcolor}
\usepackage{booktabs,ctable}
\usepackage{caption}

\begin{document}
\begin{center}
\definecolor{blue}{HTML}{E7E8E8}
\rowcolors{1}{blue}{blue}
\begin{tabular}{ l c }
Parameter & Value \\
\specialrule{.1em}{.05em}{.05em}
\textbf{Parameter} & \textbf{Value} \\
\midrule
Number of territories & 22 \\
Age of sexual maturity (years) & 2 \\
& \\
Winter mortality & \\
Mean of the winter strength distribution (days) & 117\\
\specialrule{.1em}{.05em}{.05em}
\end{tabular}
\captionof{table}{Overview of processes, parameters and default values of the marmot model}
\label{tab:variables}
\end{center}


Edit: Modified (near perfect!) table based on the responses to work with multicol package.

\begin{center}
\refstepcounter{table} \label{tab:variables}
\definecolor{blue}{HTML}{E7E8E8}
\rowcolors{1}{blue}{blue}
\begin{tabular}{ L{5.1cm} C{2cm} }
\multicolumn{2}{L{\dimexpr7.1cm+2\tabcolsep\relax}}{%
\cellcolor{black}\color{white}\textbf{\small Table \ref{tab:variables} - Overview of processes, parameters, and default values of parameters of the Rapa Nui model}} \\
Parameter & Value \\
\specialrule{.1em}{.05em}{.05em}
\textbf{Parameter} & \textbf{Value} \\
\specialrule{.1em}{.05em}{.05em}
Number of territories & 22 \\
Age of sexual maturity (years) & 2 \\
& \\
Winter mortality & \\
Mean of the winter strength distribution (days) & 117\\
\specialrule{.1em}{.05em}{.05em}
\end{tabular}
\end{center}


So far, I have only two complains left:

• The caption has too much space above, unlike @Mico's example. I did not figure out how to change that!
• I did not managed to get the above code to function with dcolumn to align numbers suggested by @fldpi

Edit2: my final version (updated Mico's final version of the table)

\begin{center}
\refstepcounter{table} \label{tab:variables}
\definecolor{blue}{HTML}{E7E8E8}
\rowcolors{1}{blue}{blue}
\begin{tabular}{ L{5.1cm} d{3.0} }
\multicolumn{2}{L{\dimexpr7.1cm+2\tabcolsep\relax}}{%
\cellcolor{black}
\bfseries\small%
\textcolor{white}{Table \ref{tab:variables} -- Overview of processes, parameters, and default values of parameters of the marmot model}} \\
Parameter &
\multicolumn{1}{C{2cm}}{Value} \\
\specialrule{.1em}{.05em}{.05em}
Number of territories & 22 \\
Age of sexual maturity (years) & 2 \\[1ex]
Winter mortality & \\
Mean of the winter strength distribution (days) & 117\\
\specialrule{.1em}{.05em}{.05em}
\end{tabular}
\end{center}

• I hope, you are not expecting us to type the whole stuff ;-) And you are missing \usepackage{caption}, most likely – user31729 Dec 3 '14 at 16:27
• A general introduction for how to create LaTeX tables can be found in the LaTeX wiki. Furthermore, on this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". – Sverre Dec 3 '14 at 16:54
• I understand your critical comments. I will do the job, no worries. I tend to open threads like this if I think that the outcome might be of interest for others, too. I was not sure if it is possible to create something like the mentioned table in latex at all, and hoped to get feedback for that. I will keep posting what I got. – Til Hund Dec 3 '14 at 17:39
• I edit the question to be more specific. Check out the updated latex code as well. Thank you! – Til Hund Dec 3 '14 at 19:25
• @TilHund - I've edited the title of your posting a bit more. For LaTeX users, "caption", "table", and "tabular" tended to be highly specific terms; it's important not to confuse people inadvertently, right? – Mico Dec 3 '14 at 19:42

The answer below focuses on getting the caption inside the tabular environment. It doesn't deal with colors or with inverted white on black printing as those would be to be secondary issues.

I suggest you do the following:

• Use a table environment.

• The first two instructions after \begin{table} should be \refstepcounter{table} and \label{tab:variables}.

• Define the widths of the columns explicitly, say, 7.5cm and 1cm. To keep the code readable, I suggest you define new column types called, say, L and C, which each take a parameter (the usable width of the respective column). The array package provides the instruction \newcolumntype for just this purpose.

• Inside the tabular, use the instruction

\multicolumn{2}{L{\dimexpr 8.5cm+2\tabcolsep\relax}}{%
Table \ref{tab:variables} -- Overview of processes, parameters, and
default values of parameters of the marmot model}


What's going on here? The material is being typeset across the two basic columns of the tabular environment. The combined with is 7.5cm + 1cm + 2\tabcolsep. The admittedly somewhat inelegant-looking expression \dimexpr 8.5cm+2\tabcolsep\relax calculates this width "on the fly".

• The text of the caption starts with Table \ref{tab:variables} --: the argument of \ref should be the text string you used in the earlier \label instruction.

\documentclass[a4paper]{article}

\usepackage{mathpazo}
\usepackage{booktabs,caption,array,ragged2e}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\begin{document}

\begin{table}
\refstepcounter{table} \label{tab:variables}
\centering
\begin{tabular}{ L{7.5cm} C{1cm} }
\specialrule{.1em}{.05em}{.05em}
\multicolumn{2}{L{\dimexpr8.5cm+2\tabcolsep\relax}}{%
Table \ref{tab:variables} -- Overview of processes, parameters, and default values of parameters of the marmot model} \\
\specialrule{.1em}{.05em}{.05em}
\textbf{Parameter} & \textbf{Value} \\
\midrule
Number of territories & 22 \\
Age of sexual maturity (years) & 2 \\
Winter mortality & \\
Mean of the winter strength distribution (days) & 117\\
\specialrule{.1em}{.05em}{.05em}
\end{tabular}
\end{table}
\end{document}


• Use of d column type for second column; predetermined width of second column is maintained by using \multicolumn{1}{C{1.3cm}}{\textbf{Value}} instruction for header of second column

• Use of inverted colors in header

\documentclass[a4paper]{article}
\usepackage[table]{xcolor}
\definecolor{blue}{HTML}{E7E8E8}
\rowcolors{1}{blue}{blue}

\usepackage{booktabs,array,ragged2e,dcolumn}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\begin{center}
\refstepcounter{table} \label{tab:variables}
\begin{tabular}{ L{5.8cm} d{3.0} }
\multicolumn{2}{L{\dimexpr7.1cm+2\tabcolsep\relax}}{%
\cellcolor{black}\color{white}\bfseries\small%
Table \ref{tab:variables} -- Overview of processes, parameters, and default values of parameters of the Rapa Nui model} \\
\textbf{Parameter} &
\multicolumn{1}{C{1.3cm}}{\textbf{Value}} \\
\specialrule{.1em}{.05em}{.05em}
Number of territories & 22 \\
Age of sexual maturity (years) & 2 \\[1ex]
Winter mortality & \\
Mean of the winter strength distribution (days) & 117\\
\specialrule{.1em}{.05em}{.05em}
\end{tabular}
\end{center}
\end{document}

• I should have mentioned that I am using the multicol package which does not(?) allow the table environment... – Til Hund Dec 4 '14 at 11:30
• Awesome edit! You fast responses are very much appreciated. Thank you very much! :) – Til Hund Dec 4 '14 at 16:43

The simplest way to get the caption at the top would be to make it part of the table. You can use \colorbox to set the basic background colour and \cellcolor for the background of the caption and you can use the dcolumn package to align the decimal point of the values:

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{dcolumn}
\definecolor{lightgray}{gray}{0.9}
\newcolumntype{d}[1]{D{.}{.}{#1} }

\begin{document}
\begingroup\setlength{\fboxsep}{0pt}
\colorbox{lightgray}{%
\begin{tabular}{p{.2cm}ld{1}}
\multicolumn{3}{p{\textwidth}}{\cellcolor{black}\color{white}\textbf{Table 1 - Overview of processes, parameters and default values of parameters of the marmot model}}\\
\noalign{\smallskip}
\multicolumn{2}{l}{\textbf{Parameter}} & \textbf{Value} \\
\midrule
& Number of territories & 22 \\
& Age of sexual maturity (years) & 2 \\\\
\multicolumn{2}{l}{Winter mortality} & \\
& Mean of the winter strength distribution (days) & 117 \\
& Standard deviation of the winter strength distribution (days) & 10.2 \\
\bottomrule
\end{tabular}%
}\endgroup
\end{document}


• How would you cross-reference such a table, using either the basic \ref method or something more complicated, like \autoref or \cref? – Mico Dec 3 '14 at 20:44
• This solution functions with the multicol package but has issue with referencing as state by @Mico. – Til Hund Dec 4 '14 at 11:31
• Note the new edit in the original post above. – Til Hund Dec 4 '14 at 14:30
• Love the easier approach but, unfortunately, it does not function with multicol and does not(?) allow to reference on table. Thank you anyway for your reply. Much appreciated! – Til Hund Dec 4 '14 at 16:45