![在独立表格顶部添加标题](https://linux22.com/image/346235/%E5%9C%A8%E7%8B%AC%E7%AB%8B%E8%A1%A8%E6%A0%BC%E9%A1%B6%E9%83%A8%E6%B7%BB%E5%8A%A0%E6%A0%87%E9%A2%98.png)
如何在独立文档中的两个表格顶部添加标题?例如
\documentclass[border=1pt,convert={outfile=\jobname.jpg}]{standalone}
\usepackage{color,colortbl}
\usepackage[table]{xcolor}
\usepackage{fontspec}
\setmainfont{Kaiti TC}
\usepackage{ifthen}
%
\usepackage{caption}
\def\tand{&}
\definecolor{Gray}{gray}{0.9}
%
\begin{document}
\newcounter{zh}
\newcounter{us}
\newcolumntype{g}{>{\columncolor{Gray}}c}
%
\begin{tabular}{|c|g|}
\hline
\setcounter{zh}{0}%
\setcounter{us}{10}%
China \tand US \\ \hline%
\whiledo{\thezh<12}{%
\thezh \tand \theus \\ \hline%
\ifnum\value{zh}=11\hline\end{tabular}\fi
\stepcounter{zh}%
\stepcounter{us}%
\ifnum\value{us}>23 \setcounter{us}{0}\fi
}%
\begin{tabular}{|c|g|}
\hline
\setcounter{zh}{12}%
\setcounter{us}{22}%
China \tand US \\ \hline%
\whiledo{\thezh<24}{%
\thezh \tand \theus \\ \hline%
\ifnum\value{zh}=23\hline\end{tabular}\fi
\stepcounter{zh}%
\stepcounter{us}%
\ifnum\value{us}>23 \setcounter{us}{0}\fi
}%
\end{document}
答案1
最简单的方法是将你的表格放在一个独特的表格中,后者放在 中threeparttable
,这有利于测量表格宽度。然后使用正常的caption
。我借此机会指出,你不必加载,color
因为你加载了xcolor
,也不必colortbl
加载它(xcolor)选项[table]
:
\documentclass[border=1pt,convert={outfile=\jobname.jpg}]{standalone}%
\usepackage{color,colortbl}
\usepackage[table]{xcolor}
\usepackage{fontspec}
\setmainfont{Kaiti TC}
\usepackage{ifthen}
%
\usepackage{caption, float, threeparttable}
\def\tand{&}
\definecolor{Gray}{gray}{0.9}
%
\begin{document}
\newcounter{zh}
\newcounter{us}
\newcolumntype{g}{>{\columncolor{Gray}}c}
%b
\begin{threeparttable}%
\captionsetup{skip=4pt}%
\caption{Some caption}%
\begin{tabular}{@{}c@{}}
\begin{tabular}{|c|g|}
\hline
\setcounter{zh}{0}%
\setcounter{us}{10}%
China \tand US \\ \hline%
\whiledo{\thezh<12}{%
\thezh \tand \theus \\ \hline%
\ifnum\value{zh}=11\hline%
\end{tabular}\fi
\stepcounter{zh}%
\stepcounter{us}%
\ifnum\value{us}>23 \setcounter{us}{0}\fi%
}%
\begin{tabular}{|c|g|}
\hline
\setcounter{zh}{12}%
\setcounter{us}{22}%
China \tand US \\ \hline%
\whiledo{\thezh<24}{%
\thezh \tand \theus \\ \hline%
\ifnum\value{zh}=23\hline%
\end{tabular}\fi
\stepcounter{zh}%
\stepcounter{us}%
\ifnum\value{us}>23 \setcounter{us}{0}\fi
}%
\end{tabular}
\end{threeparttable}
\end{document}
答案2
由于您正在使用该caption
包,因此您可以\captionof{table}{<text>}
在不使用浮动的情况下使用它。要将其放在两个表的顶部,您可以使用minipage
,即:
\begin{minipage}{5cm}
\captionof{table}{The caption}
\centering
\begin{tabular}{...}... \end{tabular}% \hfill or \quad here?
\begin{tabular}{...}... \end{tabular}
\end{minipage}
但是作为一个standalone
类,为什么你需要caption
包和\captionof
命令呢?
\begin{minipage}{5cm}
\centering
Table 1. The caption
\begin{tabular}{...}... \end{tabular}%
\begin{tabular}{...}... \end{tabular}
\end{minipage}