答案1
以下设置提供了一种插入 的方法\tabularcaption{<cols>}{<colspec>}{<caption>}
。它使用\multicolumn{<cols>}{<colspec>}{<stuff>}
插入适当的内容,并按照您使用 指定的方式格式化caption
包裹:
\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}
理想情况下,人们会希望只使用类似 的东西\tabularcaption{<caption>}
,但您需要根据 的内容灵活地调整标题布局tabular
。因此,前两个参数是必需的,要求您指定标题应跨越的列数以及列规范/布局。
答案2
一个解决方案可能是,但里面的标题是\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}