在独立表格顶部添加标题

在独立表格顶部添加标题

如何在独立文档中的两个表格顶部添加标题?例如

\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}

相关内容