我正在尝试将 nicematrix 包用于我的表,这里是 MWE:
\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{unicode-math}
\setmainfont{Times New Roman}
\usepackage{microtype}
\usepackage[left=2cm, right=2cm, top=3cm, bottom=3cm, headheight=2cm, headsep=0.5cm,
showframe]{geometry}
\usepackage{lipsum}
\usepackage{nicematrix, tikz}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{}
\chead{\begingroup
\fontsize{8pt}{10pt}\selectfont
\noindent
\begin{NiceTabularX}{\textwidth}{clX[12]X[20]l}[hvlines]
\Block{2-1}{\includegraphics[height=1.3cm, width=1.3cm, valign=c]{example-image-a}} & \Block[l]{}{Organization} & \Block[l]{2-1}{Document type: \\ Document description} & \Block[l]{2-1}{File name: \\ Here-is-long-file-name-with-version\_1.0} & \Block[l]{}{Date: \\ 01.01.2000} \\
& \Block[l]{}{Some text \\ 1234567890} & & & Page \thepage\ of \pageref{LastPage} \\
\end{NiceTabularX}
\endgroup}
\begin{document}
\lipsum[1-3]
\begingroup
\fontsize{8pt}{10pt}\selectfont
\noindent
\begin{NiceTabularX}{\textwidth}{clX[12]X[20]l}[hvlines]
\Block{2-1}{\includegraphics[height=1.3cm, width=1.3cm, valign=c]{example-image-a}} & \Block[l]{}{Organization} & \Block[l]{2-1}{Document type: \\ Document description} & \Block[l]{2-1}{File name: \\ Here-is-long-file-name-with-version\_1.0} & \Block[l]{}{Date: \\ 01.01.2000} \\
& \Block[l]{}{Some text \\ 1234567890} & & & Page \thepage\ of \pageref{LastPage} \\
\end{NiceTabularX}
\endgroup
\end{document}
我使用 LuaLaTeX 编译了 3 次。可以看到表格的宽度与文本宽度不匹配,并且标题中的表格比文本中的表格略窄。
我也尝试使用 tabularray 包,它按预期工作,但 tabularray 在颜色表方面存在问题(边框线在 pdf 阅读器中会消失,所以我决定研究 nicematrix)。nucematrix 的宽度问题可以修复吗?
谢谢。
答案1
问题在于,在编译过程中,的宽度\pageref{LastPage}
会发生变化:首先,\pageref{LastPage}
等于??
。然后,它等于1
。nicematrix
没有意识到最后一列的宽度已经改变,因此出现错误。
我建议通过以下方式固定元素的宽度\pageref{LastPage}
:
\makebox[6mm][l]{\pageref{LastPage}}
\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{unicode-math}
\setmainfont{Times New Roman}
\usepackage{microtype}
\usepackage[left=2cm, right=2cm, top=3cm, bottom=3cm, headheight=2cm, headsep=0.5cm,showframe]{geometry}
\usepackage{lipsum}
\usepackage{nicematrix, tikz}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{}
\chead{\begingroup
\fontsize{8pt}{10pt}\selectfont
\noindent
\begin{NiceTabularX}{\textwidth}{clX[12]X[20]l}[hvlines]
\Block{2-1}{\includegraphics[height=1.3cm, width=1.3cm, valign=c]{example-image-a}} & \Block[l]{}{Organization} & \Block[l]{2-1}{Document type: \\ Document description} & \Block[l]{2-1}{File name: \\ Here-is-long-file-name-with-version\_1.0} & \Block[l]{}{Date: \\ 01.01.2000} \\
& \Block[l]{}{Some text \\ 1234567890} & & & Page \thepage\ of \makebox[6mm][l]{\pageref{LastPage}} \\
\end{NiceTabularX}
\endgroup}
\begin{document}
\lipsum[1-3]
\begingroup
\fontsize{8pt}{10pt}\selectfont
\noindent
\begin{NiceTabularX}{\textwidth}{clX[12]X[20]l}[hvlines]
\Block{2-1}{\includegraphics[height=1.3cm, width=1.3cm, valign=c]{example-image-a}} & \Block[l]{}{Organization} & \Block[l]{2-1}{Document type: \\ Document description} & \Block[l]{2-1}{File name: \\ Here-is-long-file-name-with-version\_1.0} & \Block[l]{}{Date: \\ 01.01.2000} \\
& \Block[l]{}{Some text \\ 1234567890} & & & Page \thepage\ of \makebox[6mm][l]{\pageref{LastPage}} \\
\end{NiceTabularX}
\endgroup
\end{document}
答案2
使用最新版本nicematrix
(2021-08-20 的 v. 6.1),您可以直接(经过多次编译)获得预期的结果。
\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{unicode-math}
\setmainfont{Times New Roman}
\usepackage{microtype}
\usepackage[left=2cm, right=2cm, top=3cm, bottom=3cm, headheight=2cm, headsep=0.5cm,showframe]{geometry}
\usepackage{lipsum}
\usepackage{nicematrix, tikz}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{}
\chead{\begingroup
\fontsize{8pt}{10pt}\selectfont
\noindent
\begin{NiceTabularX}{\textwidth}{clX[12]X[20]l}[hvlines]
\Block{2-1}{\includegraphics[height=1.3cm, width=1.3cm, valign=c]{example-image-a}} & \Block[l]{}{Organization} & \Block[l]{2-1}{Document type: \\ Document description} & \Block[l]{2-1}{File name: \\ Here-is-long-file-name-with-version\_1.0} & \Block[l]{}{Date: \\ 01.01.2000} \\
& \Block[l]{}{Some text \\ 1234567890} & & & Page \thepage\ of \makebox[6mm][l]{\pageref{LastPage}} \\
\end{NiceTabularX}
\endgroup}
\begin{document}
\lipsum[1-3]
\begingroup
\fontsize{8pt}{10pt}\selectfont
\noindent
\begin{NiceTabularX}{\textwidth}{clX[12]X[20]l}[hvlines]
\Block{2-1}{\includegraphics[height=1.3cm, width=1.3cm, valign=c]{example-image-a}} & \Block[l]{}{Organization} & \Block[l]{2-1}{Document type: \\ Document description} & \Block[l]{2-1}{File name: \\ Here-is-long-file-name-with-version\_1.0} & \Block[l]{}{Date: \\ 01.01.2000} \\
& \Block[l]{}{Some text \\ 1234567890} & & & Page \thepage\ of \makebox[6mm][l]{\pageref{LastPage}} \\
\end{NiceTabularX}
\endgroup
\end{document}