pgfplotstable 不显示列名

pgfplotstable 不显示列名

我正在尝试使用 longtable 和 pgfplotstable 从 txt 文件创建多页表格。出于某种原因,每当我使用

"display columns/0/.style={column name={Col0}}" 

例如,列名不会打印。我是初学者,所以我有点困惑。

我已经阅读了 pgfplotstable 的文档并尝试调试了几个小时,但我还是无法弄清楚。

我的 MWE:

\documentclass{article}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
\usepackage{siunitx} % Formats the units and values
\usepackage{pgfplotstable} % Generates table from .csv
\usepackage{longtable} % To display tables on several pages
\usepackage{array}
\usepackage{bm}
\usepackage{hyperref}

\pgfplotsset{compat=1.14} %I was getting backwards compatibility errors

\begin{document}

%%% Code from Dr. Christian for not using headers
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
    \pgfplotstableset{
        empty header/.style={
          every head row/.style={output empty row},
        }
    }
}

\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}

\pgfplotstabletypeset[
    empty header,
    col sep=space,
    column type/.add={|}{},
    every head row/.append style={
        before row={%
            \caption{The caption}
            \label{tab:DataTable}
            \\ \toprule
            \\ \midrule
            \endfirsthead
            %
            \multicolumn{3}{c}{{\bfseries \tablename\ \thetable{} -- continued from previous page}}
            \\ \toprule
            \\ \midrule
            \\ \endhead
            %
            \midrule 
            \multicolumn{3}{r}
            {{Continued on next page}} 
            \\ \bottomrule
            \endfoot
            %
            \midrule
            \multicolumn{3}{r}{} 
            \\ \bottomrule
            \endlastfoot
        }
    },
    display columns/0/.style={
        dec sep align,
        sci, sci zerofill,
        column name={RA}
    },
    display columns/1/.style={
        dec sep align,
        sci, sci zerofill,
        column name={Dec}
    },
    display columns/2/.style={
        dec sep align,
        sci, sci zerofill,
        column type/.add={}{|},
        column name={S}
    }
]{ra_dec_aveflux_Jy.txt}

\end{document}

更新: 这是我的 txt 文件的示例:

1e+01 2e+01 3e+01
4e+01 5e+01 6e+01
7e+01 8e+01 9e+01

答案1

我认为也许您需要在样式中指定标题every head row,而不是添加column name。也就是说,在的标题说明中longtable,添加一行RADecS

但有一点需要注意:dec sep align您用于三列的选项实际上在生成的 中创建了两列tabular。因此,您需要使用例如\multicolumn{2}{c}{RA}

另一件需要注意的事情是,您定义的“上一页继续”标题比仅包含数字的表格要宽。(至少对于您显示的值而言。)这反过来会弄乱最后一列的水平对齐,我不知道如何修复。下面介绍的解决方法是将该文本拆分为两行,使其足够窄。

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs} % For \toprule, \midrule and \bottomrule
\usepackage{siunitx} % Formats the units and values
\usepackage{pgfplotstable} % Generates table from .csv
\usepackage{longtable} % To display tables on several pages
\usepackage{array}
\usepackage{bm}
\usepackage{hyperref}

\pgfplotsset{compat=1.14} %I was getting backwards compatibility errors

\begin{document}

%%% Code from Dr. Christian for not using headers
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
    \pgfplotstableset{
        empty header/.style={
          every head row/.style={output empty row},
        }
    }
}

\pgfplotstableset{
begin table=\begin{longtable},
end table=\end{longtable},
}

\pgfplotstabletypeset[
    empty header,
    col sep=space,
%    column type/.add={|}{}, % don't use vertical rules along with booktabs rules
    every head row/.append style={
        before row={%
            \caption{The caption}
            \label{tab:DataTable}
            \\ \toprule
            % the dec sep align options makes two tabular columns, so we need multicolumn for the header
            \multicolumn{2}{c}{RA} & \multicolumn{2}{c}{Dec} & \multicolumn{2}{c}{S}
            \\ \midrule
            \endfirsthead
            %
            % split this over two lines, so that it doesn't make the table too wide
            \multicolumn{6}{l}{{\bfseries \tablename\ \thetable{} -- continued from}} \\
            \multicolumn{6}{l}{{\bfseries previous page}}
            \\ \toprule
            \multicolumn{2}{c}{RA} & \multicolumn{2}{c}{Dec} & \multicolumn{2}{c}{S}
            \\ \midrule
             \endhead
            %
            \midrule 
            \multicolumn{6}{c}
            {{Continued on next page}} 
            \\ \bottomrule
            \endfoot
            %
%            \midrule
%            \multicolumn{6}{r}{} 
            \\ \bottomrule
            \endlastfoot
        }
    },
    display columns/0/.style={
        dec sep align,
        sci, sci zerofill,
%        column name={RA}
    },
    display columns/1/.style={
        dec sep align,
        sci, sci zerofill,
%        column name={Dec}
    },
    display columns/2/.style={
        dec sep align,
        sci, sci zerofill,
        column type/.add={}{}, % removed the |
%        column name={S}
    }
]{data.dat}

\end{document}

相关内容