0

I am trying to make a table using pgfplotstable, and am having difficulties because the csv's are jagged. Most rows have 4 columns, as do the headers, but some rows are only 1 column and I would like these 1-cell rows to span the width of the table.

How do I either have pgfplotstable dynamically create multicolumns depending on the csv, or set every other row to be a multicolumn using data from the csv?

mwe (does not produce the proper layout):

\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
    Welcome to TeX.SX! Please add a minimal working example (MWE), not not just a fragment. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document} – albert Aug 27 '18 at 17:51
0

If you want to have multicolumns by default, with the exception of the head row and possibly other exceptions, you can hack "typeset cell" from the columns.

The default is:

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

And you make it out of it:

\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%
},%%
}

enter image description here

MWE:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.