我尝试使用 LaTeX 来实现这一点,代码如下:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\bfseries\arraybackslash }X}
\usepackage[pdftex,landscape,margin=1cm]{geometry}
\begin{document}
\begin{tabularx}{\textwidth}{@{\rule[-3ex]{0pt}{7ex}}|*{7}{C|}}
\hline
Monday \\ 21st March & Tuesday \\ 22nd March & Wednesday \\ 23rd March & Thursday 24th March & Friday \\ 25th March & Saturday \\ 26th March & Sunday \\ 27th March \\
\hline
\end{tabularx}
\end{document}
其结果如下:
我尝试搜索,但找不到太多帮助。我应该在代码中做哪些更改才能获得所需的结果?
请随意编辑我的问题/标签以提高清晰度/准确性
问候。
答案1
\\
在 tabularx 环境中是为了新行,而不是单元格中的中断。
对于单元格中的中断,您可以使用\linebreak
:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash\bfseries}X}
\usepackage[pdftex,landscape,margin=1cm]{geometry}
\begin{document}
\noindent\begin{tabularx}{\textwidth}{|*{7}{C|}}
\hline
Monday \linebreak 21st March &
Tuesday \linebreak 22nd March &
Wednesday \linebreak 23rd March &
Thursday \linebreak 24th March &
Friday \linebreak 25th March &
Saturday \linebreak 26th March &
Sunday \linebreak 27th March \\
\hline
\end{tabularx}
\end{document}
答案2
在tabularx
(和其他“经典”)表格中,不要使用\\
内部单元格。对于您的情况,您可以使用makecell
包和thead
命令,这样就可以使用\\
。
\documentclass{article}
\usepackage[landscape,margin=1cm]{geometry}
\usepackage{makecell, tabularx}
\newcolumntype{C}{>{\centering\bfseries\arraybackslash }X}
\begin{document}
\noindent%
\begin{tabularx}{\textwidth}{|*{7}{C|}}
\hline
\thead{Monday\\ 21st March}
& \thead{Tuesday\\ 22nd March}
& \thead{Wednesday\\ 23rd March}
& \thead{Thursday\\ 24th March}
& \thead{Friday\\ 25th March}
& \thead{Saturday\\ 26th March}
& \thead{Sunday\\ 27th March} \\
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\hline
\end{tabularx}
\end{document}
附录:
使用该tabularray
包可以简化代码并提供更好的结果。其语法允许手动拆分单元格的内容{text1 \\ text2}
:
\documentclass{article}
\usepackage[landscape,margin=1cm]{geometry}
\usepackage{tabularray}
\begin{document}
\noindent%
\begin{tblr}{hlines, vlines,
colspec = {*{7}{X[c]}},
row{1} = {font=\bfseries}
}
{Monday\\ 21st March}
& {Tuesday\\ 22nd March}
& {Wednesday\\ 23rd March}
& {Thursday\\ 24th March}
& {Friday\\ 25th March}
& {Saturday\\ 26th March}
& {Sunday\\ 27th March} \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\end{tblr}
\end{document}
答案3
您还可以将内容拆分为两个表格行,一行显示日期,另一行显示日期:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash\bfseries}X}
\usepackage[pdftex,landscape,margin=1cm]{geometry}
\begin{document}
\noindent\begin{tabularx}{\textwidth}{|*{7}{C|}}
\hline
Monday & Tuesday & Wednesday & Thursday & Friday & Saturday & Sunday \\
21st March & 22nd March & 23rd March & 24th March & 25th March & 26th March & 27th March \\
\hline
\end{tabularx}
\end{document}