3

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

1

2 Answers 2

4

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.

4

One solution could be but the caption inside a \parbox:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}
\begin{tabularx}{.4\linewidth}{|XXXX|}
\hline
\multicolumn{4}{|c|}{
\parbox{.35\linewidth}{\caption{The caption is not 
outside the tabularx environment.}}}\\[2.3em]
\hline
    11 & 12 & 13 & 14\\
    21 & 22 & 23 & 24\\
    31 & 32 & 33 & 34\\
    41 & 42 & 43 & 44\\\hline
\end{tabularx}
\end{table}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .