如何使用 pgfplotstable 和 longtable 实现具有多个标题的可靠表格?

如何使用 pgfplotstable 和 longtable 实现具有多个标题的可靠表格?

对于我正在从事的一个项目(另请参阅我的问题在 pgfplots 中程序化设置列类型),我想创建一个包装器宏\fancyTable,它pgfplotstable在后台调用。包装器宏适用于不太熟悉 LaTeX 并应自动执行表格设置过程的用户。
一个不错的功能是表格可以跨越多页(我已经通过 实现了longtable)。为了提高可读性,最好在表格开头和每个新页面的顶部添加标题。如果表格标题在每一页上重复,也会很方便;但根据我对手册的理解,这本身pgfplotstable无法实现。pgfplotstable

初始点

我看了不同的建议(这里用户 11232/David Carlisle 的回答) 在 tex.stackexchange 上。从后者,我能够构建一个 MWE:

\documentclass{scrreprt} % standalone or minimal do not work well with longtable

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}

\usepackage{longtable}
\usepackage{multicol} % required for letting additional caption span over all cols
\usepackage{colortbl}
\usepackage{booktabs}

\usepackage[%
    paperheight=6.5cm,paperwidth=13cm, %
    left=0.5cm,right=0.5cm,top=0.25cm,%
    bottom=0.25cm, includeheadfoot %
]{geometry} % used to make page size smaller and therewith facilitate page break testing

% Formatting the caption
\usepackage[labelfont={bf}]{caption}
\captionsetup{format=plain,labelformat=simple,font=small,labelsep = colon}

\begin{filecontents}{pgfplotstable_longtable_multicap.csv}
    date, time, time zone, event
    1, 00:00, CET, server installation finished
    2, 00:05, CET, server successfully booted
    3, 00:06, CET, starting xyz daemon
    4, 00:10, CET, admin login
    5, 23:58, CET, sudo reboot
    6, 23:59, CET, server reboot
\end{filecontents}

\begin{document}
    % source: https://tex.stackexchange.com/questions/297412/define-and-use-column-styles-in-pgfplotstable
    \pgfplotstableset{
        center bold column/.style={%
            /pgfplots/table/display columns/#1/.style={%
                column type={>{\fontseries{bx}\selectfont}c},%
            }
        },
        left bold column/.style={%
            /pgfplots/table/display columns/#1/.style={%
                column type={>{\fontseries{bx}\selectfont}l},%
            }
        },
    }
    
    \pgfplotstabletypeset[
        begin table=\begin{longtable},% force pgfplotstable
        end table=\end{longtable},% to use the longtable environment
        col sep=comma,%
        header=true, % specific header data
        % source: https://tex.stackexchange.com/questions/66504/pgfplotstable-longtable-with-caption-and-repeating-header
        every head row/.append style={
            before row={\caption{The caption above the start of the table.}\\[0.5cm]},
            after row=\midrule\endfirsthead
        },
        every first row/.append style={
            before row={
                \multicolumn{\pgfplotstablecols}{c}{}\\ \caption[]{Caption on top of every new page.}\\ % setting caption to span over all columns
                \toprule
            },%
            after row=\endhead
        },
        every odd row/.style={before row={\rowcolor[gray]{0.9}}},% highlighting of every second data row
        string type,% necessary for correct processing of table content
        center bold column/.list={0,1,2}, % setting all columns bold
        left bold column/.list={3}, % setting all columns bold
        postproc cell content/.append style={
            /pgfplots/table/@cell content/.add={\fontseries{\seriesdefault}\selectfont}{} % removing bf from all table content 
        }
    ]{pgfplotstable_longtable_multicap.csv}
\end{document}

编译后产生以下文件: 在此处输入图片描述

和预期一样,每页都排版了具有不同内容的标题。但是,rowcolor第二页上的突出显示功能没有按预期工作。更重要的是,表格数据被“随机”更改:第一个条目(应该在第 1 页第 1 行)被移至第二页第 1 行。对于我期望使用计划的包装器宏处理的数据类型,这是不可接受的。

尝试 1:将标头视为普通数据

由于该header=true选项限制了可能的标题数据类型,我决定将标题视为标准字符串:

header=false, % header treated as data
every head row/.style={%
    output empty row,%
},%
every first row/.append style={%
    before row={%
        \caption{The caption above the start of the table.}\\[1.25cm] 
        \endfirsthead 
        %
        \multicolumn{\pgfplotstablecols}{c}{\caption[]{Caption on top of every new page.}} \\% what if caption style changes?
        \midrule 
        \endhead
    },%
    after row={%
        \midrule
    }%
},% 

现在,该文档无法编译。出现以下错误消息:

! Misplaced \noalign.
\caption ->\noalign 
                    {\global \@captionabovefalse }\scr@caption 
l.80    ]{pgfplotstable_longtable_multicap.csv}

错误肯定来自第二次调用\caption,因为如果我仅插入纯文本,MWE 将编译:

\documentclass{scrreprt} % standalone or minimal do not work well with longtable

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}

\usepackage{longtable}
\usepackage{multicol} % required for letting additional caption span over all cols
\usepackage{colortbl}
\usepackage{booktabs}

\usepackage[%
    paperheight=6.5cm,paperwidth=13cm, %
    left=0.5cm,right=0.5cm,top=0.25cm,%
    bottom=0.25cm, includeheadfoot %
]{geometry} % used to make page size smaller and therewith facilitate page break testing

% Formatting the caption
\usepackage[labelfont={bf}]{caption}
\captionsetup{format=plain,labelformat=simple,font=small,labelsep = colon}

\begin{filecontents}{pgfplotstable_longtable_multicap.csv}
    date, time, time zone, event
    1, 00:00, CET, server installation finished
    2, 00:05, CET, server successfully booted
    3, 00:06, CET, starting xyz daemon
    4, 00:10, CET, admin login
    5, 23:58, CET, sudo reboot
    6, 23:59, CET, server reboot
\end{filecontents}

\begin{document}
    % source: https://tex.stackexchange.com/questions/297412/define-and-use-column-styles-in-pgfplotstable
    \pgfplotstableset{
        center column/.style={%
            /pgfplots/table/display columns/#1/.style={%
                column type=c,%
            }
        },
        left column/.style={%
            /pgfplots/table/display columns/#1/.style={%
                column type=l,%
            }
        },
    }
    
    \pgfplotstabletypeset[
        begin table=\begin{longtable},% force pgfplotstable
        end table=\end{longtable},% to use the longtable environment
        col sep=comma,%
        header=false, % header treated as data
        every head row/.style={%
            output empty row,%
        },%
        every first row/.append style={%
            before row={%
                \caption{The caption above the start of the table.}\\[0.5cm] 
                \endfirsthead 
                %
                \multicolumn{\pgfplotstablecols}{c}{Caption on top of every new page.} \\[0.5cm]% what if caption style changes?
                \midrule 
                \endhead
            },%
            after row={%
                \midrule
            }%
        },%
        every odd row/.style={before row={\rowcolor[gray]{0.9}}},% highlighting of every second data row
        string type,% necessary for correct processing of table content
        center column/.list={0,1,2}, % setting all columns bold
        left column/.list={3}, % setting all columns bold
        % alternative way for getting bold "header"
        % source: https://tex.stackexchange.com/questions/130835/pgfplotstable-one-row-in-bold
        postproc cell content/.append code={
            \count0=\pgfplotstablerow
            \advance\count0 by1
            \ifnum\count0=1
            \pgfkeysalso{@cell content=\textbf{#1}}%
            \fi
        },
    ]{pgfplotstable_longtable_multicap.csv}
\end{document}

其结果如下: 在此处输入图片描述

数据现在按正确的顺序排版,但“标题”与文档的默认顺序不匹配

问题

  1. 有没有办法接听第二个电话\caption?例如\noalign(我无法接听)?
  2. 是否可以访问设置\usepackage{caption}来手动制作与真实宏完全相同的“标题” \caption

答案1

另一种解决方案是catchfiletabularray包装:

\documentclass{scrreprt}

\usepackage[
  paperheight=7.5cm, paperwidth=13cm,
  left=0.5cm, right=0.5cm, top=0.25cm,
  bottom=0.25cm, includeheadfoot
]{geometry} % used to make page size smaller and therewith facilitate page break testing

\usepackage{xcolor}
\usepackage{catchfile}
\usepackage{tabularray}

\begin{filecontents*}{mydata.csv}
    date, time, time zone, event
    1, 00:00, CET, server installation finished
    2, 00:05, CET, server successfully booted
    3, 00:06, CET, starting xyz daemon
    4, 00:10, CET, admin login
    5, 23:58, CET, sudo reboot
    6, 23:59, CET, server reboot
\end{filecontents*}

\CatchFileDef{\mydata}{mydata.csv}{\catcode`\^^M=\active}

\ExplSyntaxOn
\group_begin:
  \catcode`\^^M=\active %
  \cs_gset_protected:Npn \NormalizeData #1 {%
    \tl_greplace_all:Nnn #1 {^^M} {\\}%
    \tl_greplace_all:Nnn #1 {,} {&}%
  }%
\group_end:
\ExplSyntaxOff

\NormalizeData{\mydata}

\begin{document}

\begin{longtblr}[
  expand = \mydata,
  caption = {The Caption of the Table},
]{
  colspec = {clcl},
  hline{1,Z} = {0.08em},
  hline{2} = {0.05em},
  row{even} = {gray9},
  row{1} = {font=\bfseries},
  rowhead = 1,
}
 \mydata
\end{longtblr}

\end{document}

在此处输入图片描述

相关内容