![Tabularray 超出线宽](https://linux22.com/image/462375/Tabularray%20%E8%B6%85%E5%87%BA%E7%BA%BF%E5%AE%BD.png)
我正在使用tabularray
创建一个具有各种 colspan 和 rowspan 的表格。该表格具有width = \linewidth
和,但它超出了行宽,并且不遵守colspec = {|X[1]|X[1]|X[2]|}
列宽要求:X
我怎样才能让表格适应线宽,并尊重我给出的列宽?
最小工作示例:
\documentclass{article}
\usepackage{multicol}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage{tabularray}
\usepackage{pgffor, etoolbox}
% headers and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear existing header/footer entries
\fancyhead[C]{\footnotesize test}
\begin{document}
\noindent
\begin{tblr}{
width = \linewidth,
colspec = {|X[1]|X[1]|X[2]|}
}
%\begin{tblr}{|p{0.2\linewidth}|p{0.2\linewidth}|p{0.4\linewidth}|}
\hline
\SetCell[c=3]{c} \textbf{DOCUMENTATION INFORMATION PAGE} & &
\\
\hline
\textbf{1. DATE} \newline date & \textbf{2. DOCUMENT TYPE} \newline document type & \textbf{3. DOCUMENT NUMBER / REV} \newline document number \space document version
\\
\hline
\SetCell[c=2]{l} \textbf{4. Title \newline title } & & \textbf{5. PROJECT CODE} \newline project code
\\
\hline
\SetCell[c=2]{l} \textbf{6. AUTHOR} \newline author & & \textbf{7. CUSTOMER} \newline customer
\\
\hline
\SetCell[c=2,r=2]{l} {
\textbf{8. CONTACT ADDRESS} \\
Lorem \\
Ipsum \\
Dolor \\
Sit \\
Amet
}
&
&
\textbf{9. CONTACT PHONE} \newline telephone number \vspace{0.4em}
\\
\hline
&
&
\textbf{10. CONTACT EMAIL} \newline author email
\\
\hline
\SetCell[c=2]{l} \textbf{11. TELEPHONE NUMBER} \newline telephone number
&
&
\textbf{12. NAME OF RESPONSIBLE PERSON} \newline author
\\
\hline
\end{tblr}
\end{document}
答案1
不要将经典表格的语法与tabularray
语法混合:而\newline
应该使用\\
。
编辑:如果将“colspec˙”更改为:,则可以获得更好的单元格文本间距:
colspec = {X[0.9, l,h] X[1.1, l,h] X[2,l,h]},
在之前的 MWE 版本中考虑到这一点
修正后的 MWE 为:
\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}% For dummy text. Don't use in a real document
\usepackage{multicol}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage{tabularray}
\usepackage{pgffor, etoolbox}
% headers and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear existing header/footer entries
\fancyhead[C]{\footnotesize test}
\begin{document}
\noindent%
\begin{tblr}{hlines, vlines,
colspec = {X[0.9, l,h] X[1.1, l,h] X[2,l,h]},
row{1} = {font=\bfseries},
cell{3-Z}{1} = {c=2}{},
vspan=even
}
\SetCell[c=3]{c} DOCUMENTATION INFORMATION PAGE
& & \\
{\textbf{1. DATE}\\ date}
& {\textbf{2. DOCUMENT TYPE}\\ document type}
& {\textbf{3. DOCUMENT NUMBER/REV}\\ document number\\ document version} \\
{\textbf{4. Title\\ title}}
& & {\textbf{5. PROJECT CODE}\\ project code} \\
{\textbf{6. AUTHOR}\\ author}
& & {\textbf{7. CUSTOMER}\\ customer} \\
\SetCell[c=2, r=2]{l} {\textbf{8. CONTACT ADDRESS} \\
Lorem \\
Ipsum \\
Dolor \\
Sit \\
Amet}
& & {\textbf{9. CONTACT PHONE}\\ telephone number} \\
& & {\textbf{10. CONTACT EMAIL}\\ author email} \\
{\textbf{11. TELEPHONE NUMBER}\\ telephone number}
& & {\textbf{12. NAME OF RESPONSIBLE PERSON}\\ author} \\
\end{tblr}
\end{document}
答案2
使用\\
而不是\newline
(并{}
环绕相关单元格内容),问题就解决了。我真的不知道为什么\newline
在这里不起作用(事实上它只在最后一行的第一个单元格中出现错误)。无论如何,使用 是\\
包手册告诉你插入换行符的方式:
\documentclass{article}
\usepackage{tabularray}
% headers and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear existing header/footer entries
\fancyhead[C]{\footnotesize test}
\begin{document}
\noindent
\begin{tblr}{
width = \linewidth,
colspec = {|X[1]|X[1]|X[2]|}
}
%\begin{tblr}{|p{0.2\linewidth}|p{0.2\linewidth}|p{0.4\linewidth}|}
\hline
\SetCell[c=3]{c} \textbf{DOCUMENTATION INFORMATION PAGE} & &
\\
\hline
{ \textbf{1. DATE} \\ date } &
{ \textbf{2. DOCUMENT TYPE} \\ document type } &
{ \textbf{3. DOCUMENT NUMBER / REV} \\ document number \space document version }
\\
\hline
\SetCell[c=2]{l}
{ \textbf{4. Title} \\ title } & &
{ \textbf{5. PROJECT CODE} \\ project code }
\\
\hline
\SetCell[c=2]{l}
{ \textbf{6. AUTHOR} \\ author } & &
{ \textbf{7. CUSTOMER} \\ customer }
\\
\hline
\SetCell[c=2,r=2]{l}
{
\textbf{8. CONTACT ADDRESS} \\
Lorem \\
Ipsum \\
Dolor \\
Sit \\
Amet
}
&
&
{ \textbf{9. CONTACT PHONE} \\ telephone number } \vspace{0.4em}
\\
\hline
&
&
{ \textbf{10. CONTACT EMAIL} \\ author email }
\\
\hline
\SetCell[c=2]{l}
{ \textbf{11. TELEPHONE NUMBER} \\ telephone number }
&
&
{ \textbf{12. NAME OF RESPONSIBLE PERSON} \\ author }
\\
\hline
\end{tblr}
\end{document}