代码
考虑下面的代码。
\documentclass[
12pt,
landscape
]{article}
\usepackage{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[
a3paper,
margin = 0.77cm
]{geometry}
\usepackage{fancyhdr}
\usepackage{diagbox}
\usepackage{ragged2e,array}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{etoolbox}
\def\mlr{2.5}
\newcommand*\mc[2]{\multicolumn{3}{c|}{\raisebox{\fpeval{#1*\mlr}ex}{\textbf{#2}}}}
\newcommand*\saenk[1]{\raisebox{\fpeval{-5/9*\mlr}ex}{#1}}
\newcommand*\tid[2]{\saenk{#1\,--\,#2}}
\definecolor{dansk}{rgb}{0.0, 0.0, 1.0}
\definecolor{matematik}{rgb}{0.89, 0.0, 0.13}
\definecolor{historie}{rgb}{1.0, 0.55, 0.0}
\definecolor{engelsk}{rgb}{0.6, 0.4, 0.8}
\definecolor{tysk}{rgb}{0.0, 0.5, 0.0}
\definecolor{biologi}{rgb}{0.55, 0.71, 0.0}
\definecolor{geografi}{rgb}{0.68, 0.85, 0.9}
\definecolor{fysik}{rgb}{0.99, 0.99, 0.59}
\definecolor{idraet}{rgb}{0.96, 0.76, 0.76}
\definecolor{mad}{rgb}{0.55, 0.55, 0.55}
\newcommand*\laerer[2]{
\cellcolor{#2}
{\ifstrequal{#2}{dansk}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{matematik}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{tysk}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{engelsk}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{mad}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\textcolor{black}{\saenk{\MakeUppercase{#1}}}}}}}}}}
\newcommand*\lokale[2]{
\cellcolor{#2}
{\ifstrequal{#2}{dansk}
{\textcolor{white}{\saenk{\textbf{#1}}}}
{\ifstrequal{#2}{matematik}
{\textcolor{white}{\saenk{\textbf{#1}}}}
{\ifstrequal{#2}{tysk}
{\textcolor{white}{\saenk{\textbf{#1}}}}
{\ifstrequal{#2}{engelsk}
{\textcolor{white}{\saenk{\textbf{#1}}}}
{\ifstrequal{#2}{mad}
{\textcolor{white}{\saenk{\textbf{#1}}}}
{\textcolor{black}{\saenk{#1}}}}}}}}}
\newcommand*\fag[2]{%
\cellcolor{#2}%
{\ifstrequal{#2}{dansk}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{matematik}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{tysk}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{engelsk}
{\textcolor{white}{\saenk{\MakeUppercase{\textbf{#1}}}}}
{\ifstrequal{#2}{mad}
{\textcolor{white}{\saenk{\textbf{#1}}}}
{\textcolor{black}{\saenk{#1}}}}}}}}}
\newcommand*\nyLinje[2][c]{\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}
\pagestyle{empty}
\begin{document}
\LARGE
\begin{center}
\begin{tabular}{
|>{\RaggedRight\arraybackslash}m{6.51cm}
*{5}{|>{\RaggedRight\arraybackslash}m{1.67cm}
>{\RaggedRight\arraybackslash}m{2.08cm}
>{\RaggedRight\arraybackslash}m{1.67cm}}|
}
\hline
{\bfseries\backslashbox{Tidsrum}{Ugedag}}
& \mc{-0.3}{Mandag}
& \mc{-0.3}{Tirsdag}
& \mc{-0.3}{Onsdag}
& \mc{-0.3}{Torsdag}
& \mc{-0.3}{Fredag} \\
\hline
\verb||\hphantom{1}%
\tid{8:10}{8:55}
& \laerer{SA}{engelsk} & \fag{ENG}{engelsk} & \lokale{C-14}{engelsk}
& \laerer{MA}{geografi} & \fag{GEO}{geografi} & \lokale{C-14}{geografi}
& \laerer{KS}{dansk} & \fag{DAN}{dansk} & \lokale{C-14}{dansk}
& \laerer{JO}{idraet} & \fag{IDR}{idraet} & \lokale{Gym}{idraet}
& \laerer{MØ}{matematik} & \fag{MAT}{matematik} & \lokale{C-14}{matematik} \\[\mlr ex]
\hline
\verb||\hphantom{1}%
\tid{8:55}{9:40}
& \laerer{ME}{tysk} & \fag{TYS}{tysk} & \lokale{C-14}{tysk}
& \laerer{MA}{geografi} & \fag{GEO}{geografi} & \lokale{C-14}{geografi}
& \laerer{KS}{dansk} & \fag{DAN}{dansk} & \lokale{C-14}{dansk}
& \laerer{JO}{idraet} & \fag{IDR}{idraet} & \lokale{Gym}{idraet}
& \laerer{MØ}{matematik} & \fag{MAT}{matematik} & \lokale{C-14}{matematik} \\[\mlr ex]
\hline
\verb||\hphantom{1}%
\tid{9:40}{10:00}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}} \\[\mlr ex]
\hline
\tid{10:00}{10:45}
& \laerer{NP}{biologi} & \fag{BIO}{biologi} & \lokale{C-35}{biologi}
& \laerer{SA}{engelsk} & \fag{ENG}{engelsk} & \lokale{C-14}{engelsk}
& \laerer{MØ}{fysik} & \fag{F/K}{fysik} & \lokale{C-29}{fysik}
& \laerer{MØ}{matematik} & \fag{MAT}{matematik} & \lokale{C-14}{matematik}
& \laerer{\nyLinje{KS\\[-0.5ex]RK}}{dansk}
& \fag{\nyLinje{DAN\\[-0.5ex](STJ)}}{dansk}
& \lokale{C-14}{dansk} \\[\mlr ex]
\hline
\tid{10:45}{11:30}
& \laerer{NP}{biologi} & \fag{BIO}{biologi} & \lokale{C-35}{biologi}
& \laerer{JO}{historie} & \fag{HIS}{historie} & \lokale{C-14}{historie}
& \laerer{\nyLinje{MØ\\[-0.5ex]JJ}}{fysik}
& \fag{\nyLinje{F/K\\[-0.5ex](STJ)}}{fysik}
& \lokale{C-29}{fysik}
& \laerer{\nyLinje{MØ\\[-0.5ex]MA}}{matematik}
& \fag{\nyLinje{MAT\\[-0.5ex](STJ)}}{matematik}
& \lokale{C-14}{matematik}
& \laerer{KS}{dansk} & \fag{DAN}{dansk} & \lokale{C-14}{dansk} \\[\mlr ex]
\hline
\tid{11:30}{12:15}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}} \\[\mlr ex]
\hline
\tid{12:15}{13:00}
& \laerer{MØ}{matematik} & \fag{MAT}{matematik} & \lokale{C-14}{matematik}
& \laerer{ME}{tysk} & \fag{TYS}{tysk} & \lokale{C-14}{tysk}
& \laerer{SA}{engelsk} & \fag{ENG}{engelsk} & \lokale{C-14}{engelsk}
& \laerer{KS}{dansk} & \fag{DAN}{dansk} & \lokale{C-14}{dansk}
& \laerer{ME}{tysk} & \fag{TYS}{tysk} & \lokale{C-14}{tysk} \\[\mlr ex]
\hline
\tid{13:05}{13:45}
& \laerer{\nyLinje{KS\\[-0.5ex]NP}}{dansk}
& \fag{\nyLinje{DAN\\[-0.5ex](STJ)}}{dansk}
& \lokale{C-14}{dansk}
& & &
& \laerer{NB}{mad} & \fag{MAD}{mad} & \lokale{B-82}{mad}
& \laerer{\nyLinje{KS\\[-0.5ex]RK}}{dansk}
& \fag{\nyLinje{DAN\\[-0.5ex](STJ)}}{dansk}
& \lokale{C-14}{dansk}
& & & \\[\mlr ex]
\hline
\tid{13:45}{13:5}
& & &
& & &
& \mc{0}{\saenk{Pause}}
& \mc{0}{\saenk{Pause}}
& & & \\[\mlr ex]
\hline
\tid{13:55}{14:40}
& & &
& & &
& \laerer{NB}{mad} & \fag{MAD}{mad} & \lokale{B-82}{mad}
& \laerer{JO}{historie} & \fag{HIS}{historie} & \lokale{C-14}{historie}
& & & \\[\mlr ex]
\hline
\end{tabular}
\end{center}
\end{document}
问题
现在每个单元格看起来都像这样
我想要实现的是
每个单元格中有两行文本,其中顶部文本居中(且字体较大),底部文本左右对齐。(更新:(三个组合的)单元格中的文本彼此重叠并不重要;我可以重写它以将其全部放在一行上。)
此外,代码非常繁琐(在我看来),所以如果可能的话,我也想改进它。
答案1
也许看看tabularray
包可以更容易地处理单元格颜色,换行,对齐等。
我简化了您的方法,该方法根据单元格的背景颜色来决定字体应该是白色还是黑色。我通过以下方式定义了一个逗号列表etoolbox
选取所有较暗的颜色,并与单元格的背景颜色进行对比。如果单元格的背景颜色在该列表中,则单元格的文本将打印为白色。
我还增强了用于打印时间的宏。它会检查第一个参数的冒号前面是否只有一个或多个字符。如果只有一个,它会添加一个数字宽度的水平空格,以使表格中的所有内容整齐对齐。
\documentclass{article}
\usepackage[a3paper, margin=0.77cm, landscape]{geometry}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{diagbox}
\definecolor{dansk}{rgb}{0.0, 0.0, 1.0}
\definecolor{matematik}{rgb}{0.89, 0.0, 0.13}
\definecolor{historie}{rgb}{1.0, 0.55, 0.0}
\definecolor{engelsk}{rgb}{0.6, 0.4, 0.8}
\definecolor{tysk}{rgb}{0.0, 0.5, 0.0}
\definecolor{biologi}{rgb}{0.55, 0.71, 0.0}
\definecolor{geografi}{rgb}{0.68, 0.85, 0.9}
\definecolor{fysik}{rgb}{0.99, 0.99, 0.59}
\definecolor{idraet}{rgb}{0.96, 0.76, 0.76}
\definecolor{mad}{rgb}{0.55, 0.55, 0.55}
\forcsvlist{\listadd\darkcolors}{dansk, matematik, tysk, engelsk, mad}
\ExplSyntaxOn
\NewDocumentCommand\tid{ m m }{%
\tl_set:Nn \l__temp_tl { #1 }%
\regex_replace_once:nnN { :\d+ } { } \l__temp_tl%
\if_int_compare:w \str_count:N \l__temp_tl = 1%
\hphantom{0}%
\fi:%
#1\,--\,#2%
}
\ExplSyntaxOff
\NewTableCommand\laerer[1]{
\SetCell{font=\large,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\NewTableCommand\fag[1]{
\SetCell{font=\Huge,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\NewTableCommand\lokale[1]{
\SetCell{font=\large,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\begin{document}
\LARGE
\begin{center}
\begin{tblr}{
colspec = {
Q[l,m,6.51cm]
*{5}{Q[c,f,1.67cm]
Q[c,h,2.08cm]
Q[c,f,1.67cm]}
},
hlines,
vline{1,2,5,8,11,14,17} = {1-Z}{solid},
rows = {1.75cm, abovesep=10pt, belowsep=5pt},
row{1} = {font=\bfseries},
}
\SetCell{c,m} \diagbox{Tidsrum}{Ugedag}
& \SetCell[c=3]{c,m} Mandag & &
& \SetCell[c=3]{c,m} Tirsdag & &
& \SetCell[c=3]{c,m} Onsdag & &
& \SetCell[c=3]{c,m} Torsdag & &
& \SetCell[c=3]{c,m} Fredag & & \\
\tid{8:10}{8:55}
& \laerer{engelsk} SA & \fag{engelsk} ENG & \lokale{engelsk} C-14
& \laerer{geografi} MA & \fag{geografi} GEO & \lokale{geografi} C-14
& \laerer{dansk} KS & \fag{dansk} DAN & \lokale{dansk} C-14
& \laerer{idraet} JO & \fag{idraet} IDR & \lokale{idraet} Gym
& \laerer{matematik} MØ & \fag{matematik} MAT & \lokale{matematik} C-14 \\
\tid{8:55}{9:40}
& \laerer{tysk} ME & \fag{tysk} TYS & \lokale{tysk} C-14
& \laerer{geografi} MA & \fag{geografi} GEO & \lokale{geografi} C-14
& \laerer{dansk} KS & \fag{dansk} DAN & \lokale{dansk} C-14
& \laerer{idraet} JO & \fag{idraet} IDR & \lokale{idraet} Gym
& \laerer{matematik} MØ & \fag{matematik} MAT & \lokale{matematik} C-14 \\
\tid{9:40}{10:00}
& \SetCell[c=3]{c,m,font=\bfseries} Pause & &
& \SetCell[c=3]{c,m,font=\bfseries} Pause & &
& \SetCell[c=3]{c,m,font=\bfseries} Pause & &
& \SetCell[c=3]{c,m,font=\bfseries} Pause & &
& \SetCell[c=3]{c,m,font=\bfseries} Pause & & \\
\tid{10:00}{10:45}
& \laerer{biologi} NP & \fag{biologi} BIO & \lokale{biologi} C-35
& \laerer{engelsk} SA & \fag{engelsk} ENG & \lokale{engelsk} C-14
& \laerer{fysik} MØ & \fag{fysik} F/K & \lokale{fysik} C-29
& \laerer{matematik} MØ & \fag{matematik} MAT & \lokale{matematik} C-14
& \laerer{dansk} { KS \\ RK } & \fag{dansk} { DAN \\ (STJ) } & \lokale{dansk} C-14 \\
\end{tblr}
\end{center}
\end{document}
如果您希望将内容排列在两行中,则需要重新排列所有内容。遗憾的是,使用时嵌套并不是一个好主意tabularray
:
\documentclass{article}
\usepackage[a3paper, margin=0.77cm, landscape]{geometry}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{diagbox}
\definecolor{dansk}{rgb}{0.0, 0.0, 1.0}
\definecolor{matematik}{rgb}{0.89, 0.0, 0.13}
\definecolor{historie}{rgb}{1.0, 0.55, 0.0}
\definecolor{engelsk}{rgb}{0.6, 0.4, 0.8}
\definecolor{tysk}{rgb}{0.0, 0.5, 0.0}
\definecolor{biologi}{rgb}{0.55, 0.71, 0.0}
\definecolor{geografi}{rgb}{0.68, 0.85, 0.9}
\definecolor{fysik}{rgb}{0.99, 0.99, 0.59}
\definecolor{idraet}{rgb}{0.96, 0.76, 0.76}
\definecolor{mad}{rgb}{0.55, 0.55, 0.55}
\forcsvlist{\listadd\darkcolors}{dansk, matematik, tysk, engelsk, mad}
\ExplSyntaxOn
\NewDocumentCommand\tid{ m m }{%
\tl_set:Nn \l__temp_tl { #1 }%
\regex_replace_once:nnN { :\d+ } { } \l__temp_tl%
\if_int_compare:w \str_count:N \l__temp_tl = 1%
\hphantom{0}%
\fi:%
#1\,--\,#2%
}
\ExplSyntaxOff
\NewTableCommand\fag[1]{
\SetCell[c=2]{font=\Huge,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\NewTableCommand\laerer[1]{
\SetCell{font=\large,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\NewTableCommand\lokale[1]{
\SetCell{font=\large,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\begin{document}
\LARGE
\begin{center}
\begin{tblr}{
colspec = {
Q[l,m,6.5cm]
*{10}{Q[c,f,1.75cm]}
},
hline{1,2,4,6,8,10,12} = {1-Z}{solid},
vline{1,2,4,6,8,10,12} = {1-Z}{solid},
rows = {1cm, abovesep=10pt, belowsep=5pt},
row{1} = {font=\bfseries},
}
\SetCell{c,m} \diagbox{Tidsrum}{Ugedag}
& \SetCell[c=2]{c,m} Mandag &
& \SetCell[c=2]{c,m} Tirsdag &
& \SetCell[c=2]{c,m} Onsdag &
& \SetCell[c=2]{c,m} Torsdag &
& \SetCell[c=2]{c,m} Fredag & \\
\SetCell[r=2]{m} \tid{8:10}{8:55}
& \fag{engelsk} ENG &
& \fag{geografi} GEO &
& \fag{dansk} DAN &
& \fag{idraet} IDR &
& \fag{matematik} MAT & \\
& \laerer{engelsk} SA & \lokale{engelsk} C-14
& \laerer{geografi} MA & \lokale{geografi} C-14
& \laerer{dansk} KS & \lokale{dansk} C-14
& \laerer{idraet} JO & \lokale{idraet} Gym
& \laerer{matematik} MØ & \lokale{matematik} C-14 \\
\end{tblr}
\end{center}
\end{document}
但是,在这种情况下,最简单的方法可能是不使用细分单元格,而只使用换行符并\hfill
在单元格内排列三个小文本部分:
\documentclass{article}
\usepackage[a3paper, margin=0.77cm, landscape]{geometry}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{diagbox}
\definecolor{dansk}{rgb}{0.0, 0.0, 1.0}
\definecolor{matematik}{rgb}{0.89, 0.0, 0.13}
\definecolor{historie}{rgb}{1.0, 0.55, 0.0}
\definecolor{engelsk}{rgb}{0.6, 0.4, 0.8}
\definecolor{tysk}{rgb}{0.0, 0.5, 0.0}
\definecolor{biologi}{rgb}{0.55, 0.71, 0.0}
\definecolor{geografi}{rgb}{0.68, 0.85, 0.9}
\definecolor{fysik}{rgb}{0.99, 0.99, 0.59}
\definecolor{idraet}{rgb}{0.96, 0.76, 0.76}
\definecolor{mad}{rgb}{0.55, 0.55, 0.55}
\forcsvlist{\listadd\darkcolors}{dansk, matematik, tysk, engelsk, mad}
\ExplSyntaxOn
\NewDocumentCommand\tid{ m m }{%
\tl_set:Nn \l__temp_tl { #1 }%
\regex_replace_once:nnN { :\d+ } { } \l__temp_tl%
\if_int_compare:w \str_count:N \l__temp_tl = 1%
\hphantom{0}%
\fi:%
#1\,--\,#2%
}
\ExplSyntaxOff
\NewTableCommand\fag[1]{
\SetCell{font=\Huge,bg=#1}
\ifinlist{#1}{\darkcolors}{
\SetCell{fg=white}
}{}
}
\begin{document}
\LARGE
\begin{center}
\begin{tblr}{
colspec = {
Q[l,m,6.5cm]
*{5}{Q[c,m,5cm,rightsep=10pt,leftsep=10pt]}
},
hlines,
vlines,
rows = {1cm, abovesep=10pt, belowsep=5pt},
row{1} = {font=\bfseries},
}
\SetCell{c,m} \diagbox{Tidsrum}{Ugedag}
& Mandag
& Tirsdag
& Onsdag
& Torsdag
& Fredag \\
\SetCell{m} \tid{8:10}{8:55}
& \fag{engelsk} { ENG \\ { \large SA \hfill C-14 } }
& \fag{geografi} { GEO \\ { \large MA \hfill C-14 } }
& \fag{dansk} { DAN \\ { \large KS \hfill C-14 } }
& \fag{idraet} { IDR \\ { \large JO \hfill Gym } }
& \fag{matematik} { MAT \\ { \large MØ \hfill C-14 } } \\
\end{tblr}
\end{center}
\end{document}
附言:我不确定为什么\diagbox
不能很好地对齐,所以我只是将它放在单元格的中心。