pgfplotstable:每隔一行都是多列

pgfplotstable:每隔一行都是多列

我正在尝试使用 制作表格pgfplotstable,但遇到了困难,因为csv' 是锯齿状的。大多数行有 4 列,标题也是如此,但有些行只有 1 列,我希望这些 1 单元格行能够横跨表格的宽度。

我如何pgfplotstable根据 csv 动态创建多列,或者使用 csv 中的数据将每隔一行设置为多列?

mwe(没有产生正确的布局):

\documentclass{article}
\usepackage{pgfplotstable,multirow,booktabs}

\begin{filecontents}{data.csv}
    ColumnHeader1, ColumnHeader2, ColumnHeader3, ColumnHeader4
    item1, string1, string2, string3
    some-text-goes-here
    item2, string1, string2, string3
    some-more-text-goes-here
\end{filecontents}

\begin{document}

\pgfplotstabletypeset[
    col sep = comma,
    column type = l,
    string type,
    multicolumn names,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    %every other row/.style = {\multicolumn{4}{l}{...}}
]{data.csv}

\end{document}

答案1

如果您希望默认拥有多列,除了标题行和可能的其他例外,您可以从列中破解“排版单元格”。

默认值为:

\ifnum\pgfplotstablecol=\pgfplotstablecols%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
\fi%

然后你就成功了:

\pgfplotstableset{
typeset cell/.code={%% 
\ifnum\pgfplotstablerow=-1% Head Row (Exception)
  \ifnum\pgfplotstablecol=\pgfplotstablecols%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
  \else%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
   \fi%
\else
\ifnum\pgfplotstablerow=2% Another Exception Row ...
  \ifnum\pgfplotstablecol=\pgfplotstablecols%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
  \else%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
  \fi%
\else%
\ifnum\pgfplotstablecol=\pgfplotstablecols% Standard
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
\else%
\ifnum\pgfplotstablecol=1%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{4}{c}{#1}}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi\fi\fi\fi%
},%%
}

在此处输入图片描述

梅威瑟:

\documentclass[margin=5pt,]{standalone}
%\documentclass{article}
\usepackage{pgfplotstable,booktabs, filecontents}
\pgfplotsset{compat=1.12} 

\begin{filecontents*}{\jobname-data.csv}
A,B,C,D
Some Text goes here in a multicolum.,    ,   ,    
string1 (bad), string2, string3, string4
string1 (good), string2, string3, string4
Some more Text goes here in a multicolum.,    ,   ,    
Some Text goes here in a multicolum.,    ,   ,    
\end{filecontents*}

\pgfplotstableset{
typeset cell/.code={%% 
\ifnum\pgfplotstablerow=-1% Head Row (Exception)
  \ifnum\pgfplotstablecol=\pgfplotstablecols%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
  \else%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
   \fi%
\else
\ifnum\pgfplotstablerow=2% Another Exception Row ...
  \ifnum\pgfplotstablecol=\pgfplotstablecols%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
  \else%
  \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
  \fi%
\else%
\ifnum\pgfplotstablecol=\pgfplotstablecols% Standard
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
\else%
\ifnum\pgfplotstablecol=1%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{4}{c}{#1}}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi\fi\fi\fi%
},%%
}

\begin{document}
\pgfplotstabletypeset[col sep = comma,
string type, 
column type = l,
multicolumn names,
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule},
]{\jobname-data.csv}
\end{document}

相关内容