我有一张表格 (longtable),其中有许多列,背景颜色交替变化。有时,我希望有一行横跨所有列。但是,如果我使用 \multicolumn,该行上的所有背景颜色都会丢失。
对于那些行,我希望有一种方法可以让最左边列的文本溢出到其他列。
这是一个简单的例子:
\documentclass[10pt,a4paper]{extarticle}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,textcomp}
\usepackage{longtable}
\usepackage{array}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage[a4paper,top=3cm,bottom=1cm,left=20mm,right=10mm,headheight=2cm]{geometry}
\renewcommand{\familydefault}{\sfdefault}
\colorlet{fondcol}{blue!4!white}
\newcolumntype{F}[2]{>{\columncolor{#1}\scriptsize\raggedright\arraybackslash}p{#2}}
\newcolumntype{L}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\raggedright\arraybackslash}p{#2}}
\newcolumntype{R}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\raggedleft\arraybackslash}p{#2}}
\newcolumntype{C}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\centering\arraybackslash}p{#2}}
\newcolumntype{M}[2]{>{\columncolor{#1}\tiny\raggedright\arraybackslash}p{#2}}
\begin{document}
{
\setlength{\tabcolsep}{2pt}
\setlength{\arrayrulewidth}{1pt}
\begin{longtable}{|
F{white}{55mm}
L{fondcol}{9.5mm}
L{white}{11mm}
R{fondcol}{6mm}
R{white}{5mm}
R{fondcol}{10mm}
C{white}{2mm}
R{fondcol}{8mm}
R{white}{10mm}
R{fondcol}{8mm}
R{white}{10mm}
R{fondcol}{10mm}
R{white}{12mm}
|}
\hline
\centering col1 &
\centering col2 &
\centering col3 &
\centering col4 &
\centering\scriptsize col5 &
\centering\scriptsize col6 &
\centering\tiny col 7 &
\centering\scriptsize col 8 &
\centering\scriptsize col 9 &
\centering\scriptsize col 10 &
\centering\scriptsize col 11 &
\centering\scriptsize col 12 &
\centering\scriptsize col 13
\tabularnewline
\hline
\endhead
\textbf{ I would like this text to span all columns without changing the background colors}
& & & & & & & & & & & &
\tabularnewline
\multicolumn{1}{|w{l}{55mm}}{This text may overflow but is overwritten by the other columns' color)}
& & & & & & & & & & & &
\tabularnewline
\multicolumn{13}{|p{150mm}|}{This text does span all the columns but I lose the colors}
\tabularnewline
foo bar &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00
\tabularnewline
\hline
\end{longtable}
}
\end{document}
使用@Bernard 的答案修改示例;字体发生了变化,我还没有设法恢复相同的字体。在 \textit 之前尝试了 \sffamily ;我得到了正确的字体,但没有斜体。
\documentclass[10pt,a4paper]{extarticle}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,textcomp}
\usepackage{longtable}
\usepackage{array}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage[a4paper,top=3cm,bottom=1cm,left=20mm,right=10mm,headheight=2cm]{geometry}
\renewcommand{\familydefault}{\sfdefault}
\colorlet{fondcol}{blue!4!white}
\newcolumntype{F}[2]{>{\columncolor{#1}\scriptsize\raggedright\arraybackslash}p{#2}}
\newcolumntype{L}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\raggedright\arraybackslash}p{#2}}
\newcolumntype{R}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\raggedleft\arraybackslash}p{#2}}
\newcolumntype{C}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\centering\arraybackslash}p{#2}}
\newcolumntype{M}[2]{>{\columncolor{#1}\tiny\raggedright\arraybackslash}p{#2}}
\begin{document}
{
\setlength{\tabcolsep}{2pt}
\setlength{\arrayrulewidth}{1pt}
\begin{longtable}{|
F{white}{55mm}
L{fondcol}{9.5mm}
L{white}{11mm}
R{fondcol}{6mm}
R{white}{5mm}
R{fondcol}{10mm}
C{white}{2mm}
R{fondcol}{8mm}
R{white}{10mm}
R{fondcol}{8mm}
R{white}{10mm}
R{fondcol}{10mm}
R{white}{12mm}
|}
\hline
\centering col1 &
\centering col2 &
\centering col3 &
\centering col4 &
\centering\scriptsize col5 &
\centering\scriptsize col6 &
\centering\tiny col 7 &
\centering\scriptsize col 8 &
\centering\scriptsize col 9 &
\centering\scriptsize col 10 &
\centering\scriptsize col 11 &
\centering\scriptsize col 12 &
\centering\scriptsize col 13
\tabularnewline
\hline
\endhead
\textbf{I would like this text to span all columns without changing the background colors}
& & & & & & & & & & & &
\tabularnewline
\textit{I would like this text to span all columns without changing the background colors}
& & & & & & & & & & & &
% \llap{\parbox{\dimexpr156.5mm + 24\tabcolsep}{\itshape\sffamily\scriptsize \var{_('gravure')} (\var{_(lig.gtype)})~: \var{lig.grav|esc|grav}}}
\tabularnewline
% \textit{\var{_('gravure')} (\var{_(lig.gtype)})~: \var{lig.grav|esc|grav}}
& & & & & & & & & & & &
\llap{\parbox{\dimexpr156.5mm + 24\tabcolsep}{\textit{I would like this text to span all columns without changing the background colors}}}
\tabularnewline
foo bar &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00
\tabularnewline
\hline
\end{longtable}
}
\end{document}
答案1
这是一个技巧:将您的文本插入到最后的列,作为 \parbox
相关宽度,结合\llap
命令:
\documentclass[10pt,a4paper]{extarticle}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,textcomp}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage[a4paper,top=3cm,bottom=1cm,left=20mm,right=10mm,headheight=2cm]{geometry}
\renewcommand{\familydefault}{\sfdefault}
\colorlet{fondcol}{blue!4!white}
\newcolumntype{F}[2]{>{\columncolor{#1}\scriptsize\raggedright\arraybackslash}p{#2}}
\newcolumntype{L}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\raggedright\arraybackslash}p{#2}}
\newcolumntype{R}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\raggedleft\arraybackslash}p{#2}}
\newcolumntype{C}[2]{>{\columncolor{#1}\ttfamily\fontseries{lc}\selectfont\footnotesize\centering\arraybackslash}p{#2}}
\newcolumntype{M}[2]{>{\columncolor{#1}\tiny\raggedright\arraybackslash}p{#2}}
\begin{document}
{
\setlength{\tabcolsep}{2pt}
\setlength{\arrayrulewidth}{1pt}
\begin{longtable}{|
F{white}{55mm}
L{fondcol}{9.5mm}
L{white}{11mm}
R{fondcol}{6mm}
R{white}{5mm}
R{fondcol}{10mm}
C{white}{2mm}
R{fondcol}{8mm}
R{white}{10mm}
R{fondcol}{8mm}
R{white}{10mm}
R{fondcol}{10mm}
R{white}{12mm}
|}
\hline
\centering col1 &
\centering col2 &
\centering col3 &
\centering col4 &
\centering\scriptsize col5 &
\centering\scriptsize col6 &
\centering\tiny col 7 &
\centering\scriptsize col 8 &
\centering\scriptsize col 9 &
\centering\scriptsize col 10 &
\centering\scriptsize col 11 &
\centering\scriptsize col 12 &
\centering\scriptsize col 13
\tabularnewline
\hline
\endhead
& & & & & & & & & & & & \llap{\parbox{\dimexpr156.5mm + 24\tabcolsep}{I would like this text to span all columns without changing the background colors}}
\tabularnewline
foo bar &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00 &
0,00
\tabularnewline
\hline
\end{longtable}
}
\end{document}