# Caption using Tables

I am having a problem using captions with Tables. My caption seems to have a weird border so it creates a new line using only one or two words when trying to caption a table.

Here is the code I am using and a picture of the problem. Any help is much appreciated on how to make my borders for the caption run the full length of the table.

\begin{table}[H]
\parbox{.2\linewidth}{
\centering
\label{dt1}
\begin{tabular}[b]{ccc}
&Upward Balancing  &Downward Balancing\\
\hline
Surplus imbalance &Elspot price & (BM-)  \\
Deficit imbalance &(BM+)         & Elspot price \\

\hline
\end{tabular}
\caption{Imbalance Prices for Producers}
}
\end{table}


• Welcome to TeX.SE. Why are you encasing all material inside the table environment in a \parbox{.2\linewidth}{...} "wrapper"? The result of such a setup is that the tabular material will use the minimally necessary space -- which is evidently a lot more than 0.2\linewidth! -- while the argument of \caption will be forced into a very narrow box. – Mico Sep 18 '17 at 6:44
• A separate comment: \label must come after, not before, \caption if you intend to be able to cross-reference the table elsewhere in the document. – Mico Sep 18 '17 at 6:46

Some suggestions:

• Don't encase the contents of the table environment in a \parbox "wrapper", especially not one that's manifestly too narrow to contain the tabular material. All you will achieve is to constrain the width of the caption material.

• If you wish to be able to cross-reference the table in question, place the \label statement after, not before, the \caption statement. (If you have no plans to create cross-references using LaTeX's machinery, you may as well omit the \label statement.)

• Consider loading the booktabs package and replacing the two generic-LaTeX \hline directives with \midrule and \bottomrule instructions. That'll lead to much-improved spacing above and below the rules. Additionally, you may want to provide (a lot) more structure to the header material, to make it easier for your readers to "grasp" what the tabular material is all about.

The following screenshot shows both your original table (with the "cramped" caption) as well as one that implements the preceding suggestions.

There's also an attempt to cross-reference both table environments; observe that the first cross-reference fails because the \label was placed before \caption.

\documentclass{report} % choose a suitable document class
\usepackage{float,booktabs,xcolor}
\begin{document}
\noindent \emph{Line to illustrate width of textblock}:
\hrule

\bigskip\noindent
\textcolor{red}{First, the original version:}
\begin{table}[H]
\parbox{.2\linewidth}{%
\centering
\label{dt1}
\begin{tabular}[b]{ccc}
&Upward Balancing  &Downward Balancing\\
\hline
Surplus imbalance &Elspot price & (BM-)  \\
Deficit imbalance &(BM+)         & Elspot price \\

\hline
\end{tabular}
\caption{Original version}
}
\end{table}

\bigskip\noindent
\textcolor{red}{Now the modified version:}
\begin{table}[H]
\centering
\begin{tabular}{@{}lcc@{}}
\toprule
Type of imbalance & \multicolumn{2}{c@{}}{Direction of balancing}\\
\cmidrule(l){2-3}
& Upward & Downward \\
\midrule
Surplus & Elspot price       & (BM$-$)  \\
Deficit & (BM$+$)            & Elspot price \\
\bottomrule
\end{tabular}
\caption{Modified version} \label{dt2}
\end{table}

\noindent
Cross-references to Tables \ref{dt1} and \ref{dt2}---note the unfortunate gap'' where the first cross-reference should appear.
\end{document}