[Tex/LaTex] How to put the label in a table’s column

labelstables

How to put the label in a table's column, just like this picture.

When I put \caption{aaa} in a column, the document cannot be compiled.

enter image description here

Best Answer

The following setup provides a way to insert a \tabularcaption{<cols>}{<colspec>}{<caption>}. It uses \multicolumn{<cols>}{<colspec>}{<stuff>} to insert the appropriate content, formatted the way you specify it using the caption package:

enter image description here

\documentclass{article}

\usepackage{caption}

\makeatletter
\newcommand{\tabularcaption}[3]{%
  \multicolumn{#1}{#2}{%
    \refstepcounter{table}% Step the table counter
    \captionfont% Set the caption font
    {\captionlabelfont\tablename\ \thetable}% Set the caption label
    \caption@lsep% Set the caption separation
    {\captiontextfont #3}% Set the caption text
    \addcontentsline{lot}{table}{\protect\numberline{\thetable}#3}% Add caption to LoT
  }%
}
\makeatother

% Whatever formatting you need for the captions
\captionsetup{%
  font = sl,
  labelfont = bf,
  textfont = it
}

\begin{document}

\listoftables

\begin{table}
  \centering
  \begin{tabular}{ | *{7}{l|} }
    \hline
    \tabularcaption{7}{|l|}{A table} \\
    \hline
    A   & B   & C     & D    & E    & F   & G     \\
    \hline
    1   & 2   & 3     & 4    & 5    & 6   & 7     \\
    One & Two & Three & Four & Five & Six & Seven \\
    \hline
  \end{tabular}
\end{table}

\end{document}

Ideally one would like to just use something like \tabularcaption{<caption>}, but you'd need the flexibility to adjust the caption layout based on the content of the tabular. As such, the first two arguments are mandatory, requiring you to specify both the number of columns the caption should span, as well as the column specification/layout.

Related Question