给长桌涂上颜色

给长桌涂上颜色

当我为 的标题着色时longtable,颜色会延伸到表格。当我使用普通表格时,不会发生这种情况。我无法弄清楚为什么会发生这种情况,更重要的是,如何解决它。

显示了代码的一个小示例和问题的图片。在此处输入图片描述

\documentclass{report}

\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs} 

\usepackage{longtable}
\usepackage[table]{xcolor}
\usepackage{float}

\definecolor{Gray}{gray}{0.95}

\def\ColorTabelHeader{Gray}

\newcommand*{\BeginHeadcolor}{%
\toprule
\rowcolor{\ColorTabelHeader}
  \noalign{\kern-\belowrulesep \begingroup
  \color{\ColorTabelHeader} \hrule height\belowrulesep
  \endgroup }}
\newcommand*{\EndHeadcolor}{%
  \noalign{\begingroup \color{\ColorTabelHeader}
  \hrule height\aboverulesep \endgroup
  \kern-\aboverulesep } \midrule}

\begin{document}

\begin{table}[H] 
\caption{A simple table example}
  \begin{center}
    \begin{tabular}{cccc}
    \BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ 
    \EndHeadcolor
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
    \bottomrule
    \end{tabular} 
   \label{tab:oversigt_Forsogelementer}
  \end{center}
\end{table}


\begin{center}
\begin{longtable}{cccc}
\caption{A simple longtable example}\\
\BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\EndHeadcolor
\endfirsthead
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\EndHeadcolor
\endhead
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\midrule
\endlastfoot
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
\end{longtable}
\end{center}
\end{document}

答案1

\hrule中的longtable从左边距移到右边距,因为它未设置在对齐内。longtable以块形式处理以支持分页。

包重新定义了\hline,它存在同样的问题,因为它在内部也使用了\hrule。 这里使用的技巧是使所有列都成为一行。 列数可在宏 中找到\LT@cols

\LTBeginHeadColor并可\LTEndHeadColor用于longtable

\makeatletter
\newcommand*{\LTBeginHeadcolor}{%
  \toprule
  \multispan\LT@cols
    \vadjust pre{\kern-\belowrulesep}%
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\belowrulesep\hfill
    \endgroup
  \cr
  \rowcolor{\ColorTabelHeader}%
}
\newcommand*{\LTEndHeadcolor}{%
  \multispan\LT@cols
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\aboverulesep\hfill
    \endgroup
  \cr
  \noalign{%
    \kern-\aboverulesep
  }%
  \midrule
}
\makeatother

完整示例:

\documentclass{report}

\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs} 

\usepackage{longtable}
\usepackage[table]{xcolor}
\usepackage{float}

\definecolor{Gray}{gray}{0.95}

\def\ColorTabelHeader{Gray}

\newcommand*{\BeginHeadcolor}{%
  \toprule
  \rowcolor{\ColorTabelHeader}%
  \noalign{%
    \kern-\belowrulesep
    \begingroup
      \color{\ColorTabelHeader}\hrule height\belowrulesep
    \endgroup
  }%
}
\newcommand*{\EndHeadcolor}{%
  \noalign{%
    \begingroup
      \color{\ColorTabelHeader}%
      \hrule height\aboverulesep
    \endgroup
    \kern-\aboverulesep
  }%
  \midrule
}
\makeatletter
\newcommand*{\LTBeginHeadcolor}{%
  \toprule
  \multispan\LT@cols
    \vadjust pre{\kern-\belowrulesep}%
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\belowrulesep\hfill
    \endgroup
  \cr
  \rowcolor{\ColorTabelHeader}%
}
\newcommand*{\LTEndHeadcolor}{%
  \multispan\LT@cols
    \begingroup
      \color{\ColorTabelHeader}%
      \leaders\hrule height\aboverulesep\hfill
    \endgroup
  \cr
  \noalign{%
    \kern-\aboverulesep
  }%
  \midrule
}
\makeatother

\begin{document}

\begin{table}[H] 
\caption{A simple table example}
  \begin{center}
    \begin{tabular}{cccc}
    \BeginHeadcolor
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\rowcolor{\ColorTabelHeader}
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ 
    \EndHeadcolor
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
    \bottomrule
    \end{tabular} 
   \label{tab:oversigt_Forsogelementer}
  \end{center}
\end{table}


\begin{longtable}{cccc}
  \caption{A simple longtable example}\\
  \LTBeginHeadcolor
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \rowcolor{\ColorTabelHeader}
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \LTEndHeadcolor
\endfirsthead
  \multicolumn{4}{c}%
  {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\[\aboverulesep]
  \LTBeginHeadcolor
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \rowcolor{\ColorTabelHeader}
  \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
  \LTEndHeadcolor
\endhead
  \midrule
  \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
  \midrule
\endlastfoot
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
  1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3  & 4 \\
\end{longtable}
\end{document}

第 1 页第2页

相关内容