当 fancyheader 包含 tabulary 时,longtable 出现问题

当 fancyheader 包含 tabulary 时,longtable 出现问题

对于下面显示的文档,我收到以下错误:

! 额外的对齐标签已更改为 \cr。

l.103 \end{longtable}

输出是乱码。然而,这仅有的如果发生任何一个下列说法正确的是:

  • 表格实际上分为多页
  • 该表位于\chapter 章节样式已重新设置为fancy通过\assignpagestyle{\chapter}{fancy}

标题也必须包含tabulary如图所示的;当有tabular(x)\includegraphics或者其他我能想到的任何东西时,问题就不会发生。

我很困惑,有什么想法吗?

我的文件:

\documentclass[twoside,a4paper,11pt]{report}    

\usepackage{longtable}
\usepackage{tabulary}

\usepackage[pagestyles]{titlesec}
\usepackage{fancyhdr}
    
% All pages in "main matter" will get this header:
\newcommand\mainmatter{
    \fancyhead[CE,CO]{
        \begin{tabulary}{6cm}{C}
            \huge\fontencoding{T1}\fontfamily{phv}\selectfont PROJECT \\ \hline
            \large Title\\
        \end{tabulary}
    }
}

% A titlesec command I use in the "real" document overwrites the 
% chapter style, removing headers and footers. But I want those 
% also on the chapter page, so: 
\assignpagestyle{\chapter}{fancy}


\begin{document}

    % Start "main matter" part
    \mainmatter
    \chapter{Some Chapter}

    % Multi-page table
    \begin{longtable}{llll}

        \caption{Example of a problematic long table} \\

        % Header on first page
        \hline
        \textbf{Head 1}&\multicolumn{3}{l}{\textbf{Head 2}} \\ \hline
        \endfirsthead

        % Header on subsequent pages
        \multicolumn{4}{c}%
        {{\textbf { \tablename\ \thetable{} } \tiny(continued from previous page)}} \\
        \hline
        \textbf{Head 1} & \multicolumn{3}{l}{\textbf{Head 2}} \\ \hline
        \endhead

        % Footer on all ut last page
        \multicolumn{4}{r}{{\tiny{Continued on next page}}} \\ \hline
        \endfoot

        % Footer on last page
        \hline\hline
        \endlastfoot

        % Table content
        \textbf{data\_230}  & \multicolumn{3}{l}{content\_1\_60}                     \\ \hline
        \textbf{data\_240}  & \multicolumn{3}{l}{content\_1\_60}                     \\ \hline
        \textbf{data\_250}  & \multicolumn{3}{l}{content\_1\_590}                    \\ \hline
        \textbf{data\_260}  & content\_1\_80   & content\_1\_240  & content\_1\_500  \\
                            & content\_1\_110  & content\_1\_440  & content\_1\_580  \\
                            & content\_1\_160  & content\_1\_500  &                  \\ \hline
        \textbf{data\_270}  & \multicolumn{3}{l}{content\_1\_650}                    \\ \hline
        \textbf{data\_280}  & content\_1\_90   & content\_1\_180  & content\_1\_450  \\
                            & content\_1\_120  & content\_1\_250  & content\_1\_510  \\
                            & content\_1\_170  & content\_1\_260  & content\_1\_600  \\ \hline
        \textbf{data\_290}  & content\_1\_740  & content\_1\_750  &                  \\ \hline
        \textbf{data\_300}  & content\_1\_20   & content\_1\_30   & content\_1\_430  \\ \hline
        \textbf{data\_310}  & \multicolumn{3}{l}{content\_1\_730}                    \\ \hline
        \textbf{data\_530}  & content\_2\_20   & content\_2\_100  & content\_2\_130  \\
                            & content\_2\_90   & content\_2\_110  & content\_2\_260  \\ \hline
        \textbf{data\_550}  & content\_1\_550  & content\_2\_150  & content\_2\_200  \\
                            & content\_2\_80   & content\_2\_160  & content\_2\_210  \\
                            & content\_2\_120  & content\_2\_170  & content\_2\_220  \\
                            & content\_2\_130  & content\_2\_180  & content\_2\_230  \\
                            & content\_2\_140  & content\_2\_190  & content\_2\_240  \\ \hline
        \textbf{data\_560}  & content\_1\_560  & content\_2\_260  &                  \\ \hline
        \textbf{data\_580}  & content\_1\_10   & content\_2\_60   & content\_2\_320  \\
                            & content\_1\_520  & content\_2\_300  & content\_2\_330  \\
                            & content\_1\_680  & content\_2\_310  &                  \\ \hline
        \textbf{data\_600}  & content\_2\_280  & content\_2\_290  &                  \\ \hline
        \textbf{data\_610}  & \multicolumn{3}{l}{content\_2\_270}                    \\ \hline
        \textbf{data\_670}  & \multicolumn{3}{l}{content\_1\_860}                    \\ \hline
        \textbf{data\_780}  & \multicolumn{3}{l}{content\_1\_850}                    \\ \hline
        \textbf{data\_800}  & content\_1\_520  & content\_1\_700  & content\_2\_300  \\
                            & content\_1\_680  & content\_2\_60   & content\_2\_320  \\
                            & content\_1\_690  & content\_2\_300  &                  \\ \hline
        \textbf{data\_810}  & content\_1\_870  & content\_1\_890  & content\_1\_910  \\
                            & content\_1\_880  & content\_1\_900  &                  \\ \hline
        \textbf{data\_820}  & content\_1\_870  & content\_1\_890  & content\_1\_910  \\
                            & content\_1\_880  & content\_1\_900  &                  \\ \hline
        \textbf{data\_830}  & content\_1\_710  & content\_1\_840  &                  \\ \hline
        \textbf{data\_840}  & content\_2\_380  & content\_2\_520  &                  \\ \hline
        \textbf{data\_850}  & content\_2\_380  & content\_2\_390  &                  \\ \hline
        \textbf{data\_860}  & \multicolumn{3}{l}{content\_1\_760}                    \\ \hline
        \textbf{data\_860}  & \multicolumn{3}{l}{content\_1\_770}                    \\ \hline
        \textbf{data\_930}  & \multicolumn{3}{l}{content\_2\_340}                    \\ \hline
        \textbf{data\_950}  & \multicolumn{3}{l}{content\_1\_660}                    \\ \hline
        \textbf{data\_980}  & \multicolumn{3}{l}{content\_1\_670}                    \\ \hline
        \textbf{data\_990}  & \multicolumn{3}{l}{content\_1\_720}                    \\ \hline
        \textbf{data\_1000} & \multicolumn{3}{l}{content\_1\_720}                    \\ \hline

    \end{longtable}


\end{document}

答案1

您可以通过在安全的框中执行来消除错误ty,尽管出于某种原因,它只出现在第 1 页。请注意,单列确实没有意义,tabulary它让 Tex 花费大量精力来找到列宽,因此它们的总宽度为 6 厘米,但如果只有一列,您可以使用 6 厘米宽的 parbox。

\newsavebox\headerbox
\newcommand\mainmatter{%
    \fancyhead[CE,CO]{\usebox\headerbox}%
    }


% A titlesec command I use in the "real" document overwrites the 
% chapter style, removing headers and footers. But I want those 
% also on the chapter page, so: 
\assignpagestyle{\chapter}{fancy}


\begin{document}
\sbox\headerbox{%
\begin{tabulary}{6cm}{C}
            \huge\fontencoding{T1}\fontfamily{phv}\selectfont PROJECT \\ \hline
            \large Title\\
        \end{tabulary}}

    % Start "main matter" part
    \mainmatter

答案2

我建议使用这个解决方案,不使用tabulary,而是使用一个简单的tabulareqparbox包,以模拟最大宽度 6cm。我使用titleps来定义main页面样式。您可以在添加章节页面时使用此样式\thispagestyle{main}(可以将其合并到\titleformat)。在长表中,我还用\hlines规则替换了,以便booktabs为这些行添加一些填充:

\documentclass[twoside,a4paper,11pt]{report}
\usepackage[verbose, headheight = 22pt]{geometry} %
\usepackage{longtable}
\usepackage{,eqparbox, booktabs}

\usepackage[pagestyles]{titlesec}

% All pages in "main matter" will get this header:
\newcommand\mainmatter{\pagestyle{main}}%
\newpagestyle{main}{%
\headrule\eqsetmaxwidth{HDR}{6cm}
\sethead{}{\fontencoding{T1}\fontfamily{phv}\selectfont%
\begin{tabular}[b]{@{\,}c@{\,}}
\eqmakebox[HDR]{\huge PROJECT} \\ \midrule %
\eqmakebox[HDR]{\large Title\medskip}%
\end{tabular}
}{}
\setfoot{}{\thepage}{}
}%

\begin{document}

% Start "main matter" part
\mainmatter
\chapter{Some Chapter}
\thispagestyle{main}
% Multi-page table
\begin{longtable}{llll}
  \caption{Example of a problematic long table} \\
  % Header on first page
  \toprule
  \textbf{Head 1}&\multicolumn{3}{l}{\textbf{Head 2}} \\ \midrule
  \endfirsthead
  % Header on subsequent pages
  \multicolumn{4}{c}%
  {{\textbf { \tablename\ \thetable{} } \tiny(continued from previous page)}} \\
  \addlinespace[0.6ex]
  \toprule
  \textbf{Head 1} & \multicolumn{3}{l}{\textbf{Head 2}} \\ \midrule
  \endhead
  % Footer on all ut last page
  \multicolumn{4}{r}{{\tiny{Continued on next page}}} \\\midrule
  \endfoot
  % Footer on last page
  \addlinespace[-1.8\aboverulesep]
  \bottomrule
  \endlastfoot
  % Table content
  \textbf{data\_230} & \multicolumn{3}{l}{content\_1\_60} \\ \midrule
  \textbf{data\_240} & \multicolumn{3}{l}{content\_1\_60} \\ \midrule
  \textbf{data\_250} & \multicolumn{3}{l}{content\_1\_590} \\ \midrule
  \textbf{data\_260} & content\_1\_80 & content\_1\_240 & content\_1\_500 \\
                     & content\_1\_110 & content\_1\_440 & content\_1\_580 \\
                     & content\_1\_160 & content\_1\_500 & \\ \midrule
  \textbf{data\_270} & \multicolumn{3}{l}{content\_1\_650} \\ \midrule
  \textbf{data\_280} & content\_1\_90 & content\_1\_180 & content\_1\_450 \\
                     & content\_1\_120 & content\_1\_250 & content\_1\_510 \\
                     & content\_1\_170 & content\_1\_260 & content\_1\_600 \\ \midrule
  \textbf{data\_290} & content\_1\_740 & content\_1\_750 & \\ \midrule
  \textbf{data\_300} & content\_1\_20 & content\_1\_30 & content\_1\_430 \\ \midrule
  \textbf{data\_310} & \multicolumn{3}{l}{content\_1\_730} \\ \midrule
  \textbf{data\_530} & content\_2\_20 & content\_2\_100 & content\_2\_130 \\
                     & content\_2\_90 & content\_2\_110 & content\_2\_260 \\ \midrule
  \textbf{data\_550} & content\_1\_550 & content\_2\_150 & content\_2\_200 \\
                     & content\_2\_80 & content\_2\_160 & content\_2\_210 \\
                     & content\_2\_120 & content\_2\_170 & content\_2\_220 \\
                     & content\_2\_130 & content\_2\_180 & content\_2\_230 \\
                     & content\_2\_140 & content\_2\_190 & content\_2\_240 \\ \midrule
  \textbf{data\_560} & content\_1\_560 & content\_2\_260 & \\ \midrule
  \textbf{data\_580} & content\_1\_10 & content\_2\_60 & content\_2\_320 \\
                     & content\_1\_520 & content\_2\_300 & content\_2\_330 \\
                     & content\_1\_680 & content\_2\_310 & \\ \midrule
  \textbf{data\_600} & content\_2\_280 & content\_2\_290 & \\ \midrule
  \textbf{data\_610} & \multicolumn{3}{l}{content\_2\_270} \\ \midrule
  \textbf{data\_670} & \multicolumn{3}{l}{content\_1\_860} \\ \midrule
  \textbf{data\_780} & \multicolumn{3}{l}{content\_1\_850} \\ \midrule
  \textbf{data\_800} & content\_1\_520 & content\_1\_700 & content\_2\_300 \\
                     & content\_1\_680 & content\_2\_60 & content\_2\_320 \\
                     & content\_1\_690 & content\_2\_300 & \\ \midrule
  \textbf{data\_810} & content\_1\_870 & content\_1\_890 & content\_1\_910 \\
                     & content\_1\_880 & content\_1\_900 & \\ \midrule
  \textbf{data\_820} & content\_1\_870 & content\_1\_890 & content\_1\_910 \\
                     & content\_1\_880 & content\_1\_900 & \\ \midrule
  \textbf{data\_830} & content\_1\_710 & content\_1\_840 & \\ \midrule
  \textbf{data\_840} & content\_2\_380 & content\_2\_520 & \\ \midrule
  \textbf{data\_850} & content\_2\_380 & content\_2\_390 & \\ \midrule
  \textbf{data\_860} & \multicolumn{3}{l}{content\_1\_760} \\ \midrule
  \textbf{data\_860} & \multicolumn{3}{l}{content\_1\_770} \\ \midrule
  \textbf{data\_930} & \multicolumn{3}{l}{content\_2\_340} \\ \midrule
  \textbf{data\_950} & \multicolumn{3}{l}{content\_1\_660} \\ \midrule
  \textbf{data\_980} & \multicolumn{3}{l}{content\_1\_670} \\ \midrule
  \textbf{data\_990} & \multicolumn{3}{l}{content\_1\_720} \\ \midrule
  \textbf{data\_1000} & \multicolumn{3}{l}{content\_1\_720} \\ \midrule
\end{longtable}

\end{document} 

enter image description here

相关内容