如果在表格最后一行和 \end{longtblr} 之间插入任何内容(addlinespace、bottomrule 等),则 longtblr 将返回错误

如果在表格最后一行和 \end{longtblr} 之间插入任何内容(addlinespace、bottomrule 等),则 longtblr 将返回错误

我有一张宽表格,需要在横向模式下横跨多页。我的文档格式是表格标题位于表格下方。这给我带来了一些问题,例如我无法在表格下方看到“继续”消息,但我可以忍受。

我现在面临的问题是,如果我尝试在表格的最后一行和 \end{longtblr} 之间放置任何命令,它会将错误放入我的日志中,并且 pdf 无法编译。

\documentclass{article}
\usepackage{csquotes}                                                  % must load BEFORE babel
\usepackage[english]{babel}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{amsfonts}
\usepackage[a4paper,layout=a4paper,
        bindingoffset=2cm,left=2cm,right=2cm,
        head=2.5cm, bmargin=2cm]{geometry}      %get the page layout right

\usepackage{microtype}                              %better typesetting, affects justification and punctuation
\usepackage{verbatim}
\usepackage{graphicx}
\usepackage{url}                                    % typeset URL's reasonably
\usepackage[round]{natbib}                          % default bracket type is round
\usepackage{listings}
\usepackage{booktabs}                               %typeset tables better
\usepackage{multirow}                               %allow merge look across table rows
%\usepackage{placeins}                              %fix table floats!
\RequirePackage[l2tabu, orthodox]{nag}
\usepackage[table]{xcolor}                          % must be imported BEFORE tikz
\usepackage{tikz}                                   %write flowcharts etc
\usetikzlibrary{arrows, shapes, chains, fit, backgrounds, calc, decorations.pathreplacing, matrix, calligraphy, positioning}
\usepackage{hyperref}
\usepackage[flushleft]{threeparttable}
\usepackage[inline, shortlabels]{enumitem} 
\usepackage{tikzlings}
\usepackage{array}
%\usepackage{rotating} % replaced with pdflscape, check for \begin{sidewaysfigure}
\usepackage{subcaption}
\usepackage{cleveref}                             %sensible cross-referencing *****MUST BE PLACED AFTER HYPERREF PACKAGE IF THAT PACKAGE IS USED*****
\usepackage{varwidth}
\usepackage{pdflscape}
\usepackage{mfirstuc}
\usepackage{afterpage}
\usepackage{pgfplotstable}
\usepackage{soul}                                   % needed for strikethrough
\usepackage{siunitx}            % for SI units so they don't format badly
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

% try and get a lower case first letter for the section hyperlinks working
\newcommand{\lnameref}[1]{%
\bgroup
\let\nmu\MakeLowercase
\nameref{#1}\egroup}
\newcommand{\fnameref}[1]{%
\bgroup
\def\nmu{\let\nmu\MakeLowercase}%
\nameref{#1}\egroup}
\newcommand{\nmu}{}

% define position of caption in tabularray so it sits under the table
\DefTblrTemplate{firsthead,middlehead,lasthead}{default}{}
\DefTblrTemplate{firstfoot,middlefoot}{default}{}  % <---
\DefTblrTemplate{lastfoot}{default}%               % <--- 
{
    \UseTblrTemplate{note}{default}
    \UseTblrTemplate{caption}{default}

}

\newcommand{\vp}[2]{%
  \begin{varwidth}{#1}\raggedright #2\end{varwidth}%
}

% Uncomment the next line if you want subsubsections to be numbered
\setcounter{secnumdepth}{3}
% Uncomment the next line if you want subsubsections to be appear in the table of     contents
\setcounter{tocdepth}{3}

% set up table alignment
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

% greyscale for table rows
\definecolor{LightGray}{gray}{0.9}

\begin{document}

 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate velit vitae risus euismod rutrum eu eget tortor. Fusce in ex eu ante sagittis interdum. Phasellus bibendum hendrerit massa elementum tincidunt. Fusce cursus odio sit amet varius aliquam. Suspendisse imperdiet posuere orci congue congue. Aliquam leo quam, sodales ut sapien vel, viverra convallis felis.


 \afterpage{%
\begin{landscape}
\begin{longtblr}[
caption = {Demographics associated with becoming an infected household contact, SARS-CoV-A, western countries. Only demographics included in the synthetic population shown.},
label = {tab:SARS},
note{a} = {\footnotesize Follow-up period for detecting infected contacts.},
note{b} = {\footnotesize Number of households assumed to be the same as the number of index cases.},
note{s} = {\footnotesize Statistically significant.},
]{
    colspec = {l l l l l l l l},
    rowhead = 2,
    column{7}={5cm},
    stretch=0,
    row{1-Z} = {font=\small},
} 
\toprule
\SetCell[r=2]{c}Study
& \SetCell[r=2]{c}Country
& Number of  
& \SetCell[c=3]{c}Secondary attack rate
& & & \SetCell[r=2]{c}Analysis method
& \SetCell[r=2]{c}Demographics examined \\
& & households & period\TblrNote{a} & households & contacts & & \\
\midrule 

\SetCell[r=6]{l}\cite{Julin1} & \SetCell[r=6]{l}Norway &  \SetCell[r=6]{l}65 & \SetCell[r=6]{l}14 days & \SetCell[r=6]{l}66\% & \SetCell[r=6]{l}49.6\%  & \SetCell[r=6]{l}Mixed-effect logistic regression & Age of index case\TblrNote{s} \\
        & & & & & & & Sex of index case \\
        & & & & & & & Age of contact \\
        & & & & & & & Sex of contact \\
        & & & & & & & Number in household\TblrNote{s} \\
        & & & & & & & Number of bathrooms \\  
        
        \addlinespace[.3em] \\
        
\SetCell[r=5]{l}\cite{Stich1}&\SetCell[r=5]{l}Germany & \SetCell[r=5]{l}405 & \SetCell[r=5]{l}n.s. & \SetCell[r=5]{l}n.s & \SetCell[r=5]{l}32.8\% & \SetCell[r=5]{l}Multivariable linear mixed-effect logistic regression & 
        Age of index case\TblrNote{s} \\
        & & & & & & & Sex of index case \\
        & & & & & & &Age of contact\TblrNote{s} \\
        & & & & & & & Sex of contact \\
        & & & & & & & Number in household \\ 
        
        \addlinespace[.3em] \\
        
\SetCell[r=5]{l}\cite{Telle1} & \SetCell[r=5]{l}Norway &  \SetCell[r=5]{l}7548\TblrNote{b} & \SetCell[r=5]{l}7 days & \SetCell[r=5]{l}n.s & \SetCell[r=5]{l}21\%  & \SetCell[r=5]{l}Wilson score interval & Age of index case\TblrNote{s} \\
        & & & & & & & Sex of index case \\
        & & & & & & & Age of contact\TblrNote{s} \\
        & & & & & & & Sex of contact\TblrNote{s} \\
        & & & & & & & Number in household\TblrNote{s} \\
        
        \addlinespace[.3em] \\
        
\SetCell[r=6]{l}\cite{Cerami1} & \SetCell[r=6]{l}USA & \SetCell[r=6]{l}91 & \SetCell[r=6]{l}28 days & \SetCell[r=6]{l}69\% & \SetCell[r=6]{l}32\% & \SetCell[r=6]{l}Chi-squared/Mann-Whitney U test & Age of index case \\
        & & & & & & & Sex of index case \\
        & & & & & & & Age of contact \\
        & & & & & & & Sex of contact \\
        & & & & & & & Contact relationship to index case \\ 
        & & & & & & &  Household density \\ 

\addlinespace[.3em] \\

  \SetCell[r=4]{l}\cite{Kolodziej1} &  \SetCell[r=4]{l}Netherlands & \SetCell[r=4]{l}85 & \SetCell[r=4]{l}42 days & \SetCell[r=4]{l}88.2\% & \SetCell[r=4]{l}64.3\% &    \SetCell[r=4]{l}Logistic regression & Age of index case \\
       & & & & & & & Sex of index case \\
       & & & & & & & Age of contact \\
       & & & & & & & Sex of contact \\ 
       
       \addlinespace[.3em] \\
       
   \SetCell[r=6]{l}\cite{Derqui1} &   \SetCell[r=6]{l}UK &  \SetCell[r=6]{l}279 &  \SetCell[r=6]{l}28 days &  \SetCell[r=6]{l}n.s. &  \SetCell[r=6]{l}36.7\% &  \SetCell[r=6]{l}Chi-squared & Age of contact \\*
     & & & & & & & Sex of contact \\*
     & & & & & & &  Contact relationship to index case \\*
     & & & & & & &  Number in household \\*
     & & & & & & & Household density \\*
     & & & & & & & Vaccinated\TblrNote{s} \\  *
     
        
 \bottomrule %  <- causes an error
 %\toprule <- also causes an error
 % anything I insert in here causes an error
 % \midrule
 % \addlinespace[.3em]


 \end{longtblr}
\end{landscape}
\clearpage   %% <------- important

} \{文档结束}

我尝试过\bottomrule带结尾\\和不带结尾的etc \\,但都不起作用。我想问题可能是我对其中一项参考研究进行了拆分,所以我添加了,以*阻止研究信息被拆分到两页上。但问题不是出在这里。

所有命令均在表格顶部、标题行下方、表格行之间起作用,但不在表格末尾起作用。

我需要做什么来修复表格?这是我的论文的一部分,因此在文档开头加载了大量的包。

编辑:我已将整个序言包含在内,因为它是我文档的一部分。我不能从序言中删除软件包,否则我的论文的其他部分将失败。要求我删除序言中与此特定表格无关的所有内容的“修复”不是修复。

答案1

总的来说,tabularray它旨在简化制作自定义表格的代码,而你却创建了大量不必要的复杂代码。我强烈建议阅读文档。手册清晰易懂,尽管longtblr一开始你可能会发现与 相关的部分有点难以理解。无论如何,你仍然应该对tabularray工作原理有更好的理解。

首先,你可以把定义和内容分开。所有这些合并可以在表格规范中设置,然后在具有上下文定义的部分中拥有干净的代码。此外,tabulararray还允许您在一个单元格中插入多行。这需要用花括号保护:{Aaaa\\Bbbb\\...\\Zzz}。这些只是几个例子。

下面是呈现表格的精简代码,我相信它能满足您的期望

\documentclass{article}
\usepackage[
  a4paper,
  layout=a4paper,
  bindingoffset=2cm,
  left=2cm,
  right=2cm,
  head=2.5cm,
  bmargin=2cm,
]{geometry}
\usepackage{pdflscape}
\usepackage{tabularray}
\usepackage{kantlipsum}   % Dummy text

\UseTblrLibrary{booktabs,siunitx}

% define position of caption in tabularray so it sits under the table
\DefTblrTemplate{firsthead,middlehead,lasthead}{default}{}
\DefTblrTemplate{firstfoot,middlefoot}{default}{
  \itshape(Cotinues on next page ...)
}
\DefTblrTemplate{lastfoot}{default}{
  \UseTblrTemplate{note}{default}
  \UseTblrTemplate{caption}{default}
}
\DefTblrTemplate{note}{default}{
  \MapTblrNotes{
    \noindent\footnotesize
    \UseTblrTemplate{note-tag}{default}
    \UseTblrTemplate{note-sep}{default}
    \UseTblrTemplate{note-text}{default}\par
  }
}


\begin{document}
\kant[1]

\begin{landscape}
  \begin{longtblr}[
      caption = {Demographics associated with becoming an infected household contact, SARS-CoV-A, western countries. Only demographics included in the synthetic population shown.},
      label = {tab:SARS},
      note{a} = {Follow-up period for detecting infected contacts.},
      note{b} = {Number of households assumed to be the same as the number of index cases.},
      note{s} = {Statistically significant.},
    ]{
      stretch = 0,
      colspec = {@{} *8{Q[l]} @{}},
      rowhead = 2,
      %
      row{3-Z} = {rowsep=6pt},
      row{3} = {abovesep=3pt},
      row{Z} = {belowsep=3pt},
      %
      cells = {font=\small},
      cell{1}{1,2,3,7,8} = {r=2}{},
      cell{1}{4} = {c=3}{c},
      cell{1-Z}{8} = {}{m},
    }
    \toprule
    Study
    & Country
    & Number of
    & Secondary attack rate & &
    & Analysis method
    & Demographics examined \\
    & & households
    & period\TblrNote{a}
    & households
    & contacts
    & & \\
    \midrule
    \cite{Julin1}
    & Norway
    & 65
    & 14 days
    & 66\%
    & 49.6\%
    & Mixed-effect logistic regression
    & {Age of index case\TblrNote{s}\\Sex of index case\\Age of contact\\Sex of contact\\Number in household\TblrNote{s}\\Number of bathrooms} \\
    \cite{Stich1}
    & Germany
    & 405
    & n.s.
    & n.s
    & 32.8\%
    & Multivariable linear mixed-effect logistic regression
    & {Age of index case\TblrNote{s}\\Sex of index case\\Age of contact\TblrNote{s}\\Sex of contact\\Number in household} \\
    \cite{Derqui1}
    & UK
    & 279
    & 28 days
    & n.s.
    & 36.7\%
    & Chi-squared
    & {Age of contact\\Sex of contact\\Contact relationship to index case\\Number in household\\Household density\\Vaccinated\TblrNote{s}} \\
    \bottomrule
  \end{longtblr}
\end{landscape}

\kant[2]
\end{document}

答案2

没有人要求您在论文中删除软件包。但是,如果您寻求(免费)帮助来调试问题,您不应该简单地将所有软件包以及数千行不相关的代码都转储到问题中。即使没有太多的 LaTeX 技能,您也可以在很短的时间内减少序言并构建更短的测试文档。

就您而言,由于您遇到了 longtblr 问题,您可以(在新的测试文档中!)测试是否还会遇到错误,如果您只加载包和 booktabs 库并复制表格(您确实这样做了)。然后,由于您已经知道在末尾添加某些内容会导致错误,因此您可以尝试通过删除前面的行来缩短表格。

通过这种方法我只花了几分钟就得到了这个:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}

\begin{longtblr}{colspec = {l l l l l l l l},} 
& & & & & & & Vaccinated\TblrNote{s} \\ *  

\bottomrule %  <- causes an error
\end{longtblr}
\end{document}

然后你可以问一个问题,有人会告诉你 tabularray 似乎不喜欢在 a 后面有一个空行\\*(在我看来这看起来像是一个错误)并且可以编译:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}

\begin{longtblr}{colspec = {l l l l l l l l},} 
& & & & & & & Vaccinated\TblrNote{s} \\*  
\bottomrule % 
\end{longtblr}
\end{document}

相关内容