我在格式化一个跨多页的超表格时遇到了困难,该表格的宽度与页面宽度相同,单元格内包含图像和文本。我的第一个问题是表格似乎比我试图设置的 \textwidth 更宽。其次,插入图像时,其余文本会被向下移动。第三,当使用图像并且有足够的行来跨越多页时,第一页会变成空白,第二页包含表格的第一个单元格,第三页包含表格的第三页单元格。这是我的代码,后面是一些图像来说明我的意思。
\documentclass{book}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage[margin=1.0in]{geometry}
\usepackage{booktabs,supertabular}
\usepackage[table]{xcolor}
\usepackage{refcount}
\usepackage{makecell}
\pagestyle{plain}
\newcommand{\TableHeaderContent}[3]{
\ifnum\value{#1}=\value{#2}
#3
\else
#3 (Sheet~\the\numexpr\value{page}+1-\the\numexpr\value{#1}~of~\the\numexpr\value{#2}+1-\the\numexpr\value{#1})
\fi
}
\newcommand{\TableCell}[1]{
\multicolumn{1}{p{0.25\linewidth}}{#1}
}
\begin{document}
\newcounter{StartTableCounter}
\newcounter{EndTableCounter}
\setcounter{StartTableCounter}{\getpagerefnumber{StartTable}}
\setcounter{EndTableCounter}{\getpagerefnumber{EndTable}}
\tablehead{
\multicolumn{4}{l}{\textbf{\TableHeaderContent{StartTableCounter}{EndTableCounter}{Table 1.1}}}\\\hline
\multicolumn{1}{p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 1}} &
\multicolumn{1}{|p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 2}} &
\multicolumn{1}{|p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 3}} &
\multicolumn{1}{|p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 4}}\\\hline
}
\tablefirsthead{
\multicolumn{4}{l}{\textbf{\TableHeaderContent{StartTableCounter}{EndTableCounter}{Table 1.1}}}\\\hline
\multicolumn{1}{p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 1}} &
\multicolumn{1}{|p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 2}} &
\multicolumn{1}{|p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 3}} &
\multicolumn{1}{|p{0.25\linewidth}}{\cellcolor{gray!25}\textbf{Header 4}}\\\hline
}
\tabletail{\hline}
\tablelasttail{\hline}
\label{StartTable}
\begin{supertabular*}{\linewidth}{p{0.25\linewidth}|p{0.25\linewidth}|p{0.25\linewidth}|p{0.25\linewidth}}
\includegraphics[width=\linewidth]{blackbox.jpeg} &
Content 2 &
Content 3 &
Long text with lots of words thats more than one line but not one really big word which apparently can cause some problems.\\\hline
\end{supertabular*}
\label{EndTable}
\end{document}
如您所见,第一页是空的,第二页是错误的。总结一下,为什么表格会超出 \textwidth?为什么图片会将其余内容压低?为什么当我将图片引入其中时,页面格式会变得很奇怪?
答案1
除了列溢出问题,我还找到了其他一些问题的解决方案。只需将 TableCell 命令更改为如下所示:
\newcommand{\TableCell}[1]{
\makecell*[{{p{\linewidth}}}]{#1}
}
以下是完整的新代码。其中包括一些小改动,例如将表头居中以及将工作表计数器放在表的第一个和最后一个元素中,以提供更准确的工作表计数。
\documentclass{book}
\usepackage{graphicx}
\usepackage{placeins}
\usepackage{anyfontsize}
\usepackage{fontspec}
\usepackage[margin=1.0in]{geometry}
\usepackage{tocloft}
\usepackage{scrextend}
\usepackage{caption}
\usepackage{booktabs,supertabular}
\usepackage[table]{xcolor}
\usepackage{refcount}
\usepackage{makecell}
\usepackage{tcolorbox}
\usepackage{pifont}
\usepackage{hyperref}
\pagestyle{plain}
\definecolor{noteOutline}{rgb}{0.122, 0.435, 0.698}
\definecolor{noteBackground}{RGB}{161, 192, 255}
\setlength\extrarowheight{2pt}
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}}
\newcommand{\MyChapter}[2]{
\phantomsection
\addcontentsline{toc}{chapter}{#2}
\begin{center}
{\textbf{\Huge{#1}}}\\[15pt]
{\textbf{\huge{#2}}}
\end{center}
}
\newcommand{\TableHeaderContent}[3]{
\ifnum\value{#1}=\value{#2}
#3
\else
#3 (Sheet~\the\numexpr\value{page}+1-\the\numexpr\value{#1}~of~\the\numexpr\value{#2}+1-\the\numexpr\value{#1})
\fi
}
\newcommand{\TableCell}[1]{
\makecell*[{{p{\linewidth}}}]{#1}
}
\newcommand{\thickline}{
\Xhline{4\arrayrulewidth}
}
\begin{document}
\MyChapter{Section 1}{Title}
\begin{center}
\addcontentsline{lot}{table}{Table 1.1 }
\newcounter{Table10StartTableCounter}
\newcounter{Table10EndTableCounter}
\setcounter{Table10StartTableCounter}{\getpagerefnumber{Table10StartTable}}
\setcounter{Table10EndTableCounter}{\getpagerefnumber{Table10EndTable}}
\tablehead{
\multicolumn{3}{l}{\textbf{\TableHeaderContent{Table10StartTableCounter}{Table10EndTableCounter}{Table 1.1 }}}\\\thickline
\multicolumn{1}{>{\centering}p{0.333262\linewidth}|}{\cellcolor{gray!25}{Cell 1}} &
\multicolumn{1}{|>{\centering}p{0.333369\linewidth}}{\cellcolor{gray!25}{Cell 2}} &
\multicolumn{1}{|>{\centering}p{0.333369\linewidth}}{\cellcolor{gray!25}{Cell 4}}
\\\hline
}
\tablefirsthead{
\multicolumn{3}{l}{\textbf{\TableHeaderContent{Table10StartTableCounter}{Table10EndTableCounter}{Table 1.1 }}}\\\thickline
\multicolumn{1}{>{\centering}p{0.333262\linewidth}|}{\cellcolor{gray!25}{Cell 1}} &
\multicolumn{1}{|>{\centering}p{0.333369\linewidth}}{\cellcolor{gray!25}{Cell 2}} &
\multicolumn{1}{|>{\centering}p{0.333369\linewidth}}{\cellcolor{gray!25}{Cell 4}}
\\\hline
}
\tabletail{\thickline}
\tablelasttail{\thickline}
\begin{supertabular*}{\linewidth}{p{0.333262\linewidth}|p{0.333369\linewidth}|p{0.333369\linewidth}}
\TableCell{Cell 4}\label{Table10StartTable} &
\TableCell{Cell 5} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 7} &
\TableCell{Cell 8} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 1} &
\TableCell{Cell 2} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 4} &
\TableCell{Cell 5} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 7} &
\TableCell{Cell 8} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 1} &
\TableCell{Cell 2} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 4} &
\TableCell{Cell 5} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 7} &
\TableCell{Cell 8} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 1} &
\TableCell{Cell 2} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 4} &
\TableCell{Cell 5} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\\\hline
\TableCell{Cell 7} &
\TableCell{Cell 8} &
\TableCell{\includegraphics[width=0.333369\linewidth]{image1.jpg}}\label{Table10EndTable}\\
\end{supertabular*}
\end{center}
\end{document}