![如何在 longtable 中的標題周圍放置一個填充顏色的框架?](https://rvso.com/image/390901/%E5%A6%82%E4%BD%95%E5%9C%A8%20longtable%20%E4%B8%AD%E7%9A%84%E6%A8%99%E9%A1%8C%E5%91%A8%E5%9C%8D%E6%94%BE%E7%BD%AE%E4%B8%80%E5%80%8B%E5%A1%AB%E5%85%85%E9%A1%8F%E8%89%B2%E7%9A%84%E6%A1%86%E6%9E%B6%EF%BC%9F.png)
我想tcolorbox
在 longtable 的標題周圍放置一個填充顏色的框架(例如帶有包),我將其放入\endlastfoot
.我用這個問題的答案:盒裝標題和圖形,帶有彩色標題對於我的其他數字/表格,但無法使其在 longtable 環境中工作。我已經使用了這個問題的答案給長桌上色嘗試為多行標題著色,但無法使其看起來漂亮並帶有邊框。
\documentclass[a4paper, 12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english,ngerman]{babel}
\usepackage[margin=2cm]{geometry}
\setlength{\parindent}{5mm}
%Tabels:
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{caption}
%Adjusted Command to color the caption from https://tex.stackexchange.com/questions/345589/color-a-longtable/345602#345602
\definecolor{lightGray}{gray}{0.95}
\def\ColorTabelHeader{lightGray}
\newcommand*{\BeginHeadcolor}{%
\arrayrulecolor{gray}\toprule[0.7pt]
\rowcolor{\ColorTabelHeader}%
\noalign{%
\kern-\belowrulesep
\begingroup
\color{\ColorTabelHeader}\hrule height\belowrulesep
\endgroup
}%
}
\newcommand*{\EndHeadcolor}{%
\noalign{%
\begingroup
\color{\ColorTabelHeader}%
\hrule height\aboverulesep
\endgroup
\kern-\aboverulesep
}%
\toprule[0.7pt] \arrayrulecolor{black}
}
\makeatletter
\newcommand*{\LTBeginHeadcolor}{%
\arrayrulecolor{gray}\toprule[0.7pt]
\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
}%
\toprule[0.7pt] \arrayrulecolor{black}
}
\makeatother
% Settings for Longtable
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\setlength{\LTcapwidth}{\columnwidth}
% To make it load faster
\setcounter{LTchunksize}{50}
\begin{document}
\renewcommand\arraystretch{0.8}
\begin{longtable}{c|c >{\columncolor[gray]{.9}}c c >{\columncolor[gray]{.9}}c c}
\LTBeginHeadcolor
\caption[]{\textbf{Caption:}
some information describing the content}\\
\LTEndHeadcolor \endfoot
\LTBeginHeadcolor
\caption[]{\textbf{Caption:}
this is a very long caption with some additional information regarding the parameters.\label{tab:Sample}}\\*
\rowcolor{\ColorTabelHeader}
\multicolumn{6}{c}{\parbox{\LTcapwidth}{{\footnotesize This Information is so long, that I had to put it in an extra paragraph with a smaller font}}}\\
\LTEndHeadcolor
\endlastfoot
\toprule
\multirow{2}[2]{*}[2mm]{\textbf{Parameter}} & & \textbf{BLH} & \textbf{0h} & \textbf{4h} & \textbf{8h} \\
& & Median (SD) & Median (SD) & Median (SD) & Median (SD) \\
\midrule \endfirsthead
\toprule
\multirow{2}[2]{*}[2mm]{\textbf{Parameter}} & & \textbf{BLH} & \textbf{0h} & \textbf{4h} & \textbf{8h} \\
& & Median (SD) & Median (SD) & Median (SD) & Median (SD) \\
\midrule \endhead
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \\[0,3cm]
\bottomrule
\end{longtable}%%
\end{document}
答案1
感謝 Axel,我找到了解決問題的可能方法。由於我對 Latex 比較陌生,並且不太習慣宏,請告訴我我犯的任何錯誤,或者只是編輯我的答案:
彩色填充框架 LTcaptionbox 的程式碼:
\usepackage[many]{tcolorbox}
\makeatletter
\newtcolorbox{LTcaptionbox}[7][]{enhanced, colframe=gray, colback=gray!30, arc=0pt, outer arc=0pt, boxrule=0.7pt, before={}, #1}
\DeclareCaptionOption{stuffforbox}{\def\caption@stuffforbox{#1}}
\DeclareCaptionBox{myLTbox}{%
\begin{LTcaptionbox}[code={\pgfkeysalsofrom{\caption@stuffforbox}}]
#2%
\end{LTcaptionbox}%
}
\makeatother
\captionsetup[longtable]{stuffforbox={}, box={myLTbox}, slc=off}
和我編輯的表格(我添加了“tabu”包來填充整個頁面並將“\bottomrule”和LTCaptionbox調整為相同的寬度)
\documentclass[a4paper, 12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english,ngerman]{babel}
\usepackage[margin=2cm]{geometry}
\setlength{\parindent}{5mm}
%Tabels:
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{caption}
\usepackage{tabu}
\usepackage[many]{tcolorbox}
\makeatletter
\newtcolorbox{LTcaptionbox}[7][]{enhanced, colframe=gray, colback=gray!30, arc=0pt, outer arc=0pt, boxrule=0.7pt, before={}, #1}
\DeclareCaptionOption{stuffforbox}{\def\caption@stuffforbox{#1}}
\DeclareCaptionBox{myLTbox}{%
\begin{LTcaptionbox}[code={\pgfkeysalsofrom{\caption@stuffforbox}}]
#2%
\end{LTcaptionbox}%
}
\makeatother
\captionsetup[longtable]{stuffforbox={}, box={myLTbox}, slc=off}
% Settings for Longtable
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\setlength{\LTcapwidth}{\glueexpr(\textwidth-10mm-1,4pt)\relax} % minus Distance text- to outer edge of the bounding-box on both sides
% To make it load faster
\setcounter{LTchunksize}{50}
\begin{document}
\begin{longtabu}{X|X X X X X}
\caption[]{\textbf{Caption:} some information describing the content}\endfoot
\caption[CaptionLOT]{\textbf{Caption:} this is a very long caption with some additional information regarding the parameters.\label{tab:Sample}\\*
{\footnotesize This Information is so long, that I had to put it in an extra paragraph with a smaller font}}
\endlastfoot
\toprule
\multirow{2}[2]{*}[2mm]{\textbf{Parameter}} & & \textbf{BLH} & \textbf{0h} & \textbf{4h} & \textbf{8h} \\
& & Median (SD) & Median (SD) & Median (SD) & Median (SD) \\
\midrule \endfirsthead
\toprule
\multirow{2}[2]{*}[2mm]{\textbf{Parameter}} & & \textbf{BLH} & \textbf{0h} & \textbf{4h} & \textbf{8h} \\
& & Median (SD) & Median (SD) & Median (SD) & Median (SD) \\
\midrule \endhead
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
Parameter & Group1 & 1 & 2 & 3 & 4 \\*
{[unit]} & Group2 & 1 & 2 & 3 & 4 \vspace{0,3cm}\\
\bottomrule \addlinespace[1pt]
\end{longtabu}%%
\end{document}
PS:我無法將「\columncolor」與「Longtabu」的 X 列一起使用。有誰知道這個問題的可能解決方案?
如果你想保留「Longtable」環境,你可以使用:
\newlength\q
\setlength\q{\dimexpr 0.166\textwidth -2\tabcolsep\relax}
和
\begin{longtable}{p{\q}|p{\q} >{\columncolor[gray]{.95}\centering\arraybackslash}p{\q} >{\centering\arraybackslash}p{\q} >{\columncolor[gray]{.95}\centering\arraybackslash}p{\q} >{\centering\arraybackslash}p{\q}}