2

I have a table where the columns are narrow, except for the column headers. I am typesetting the table using pgfplots, and I need a way to specify that certain header columns should be merged.

MWE

\documentclass[twocolumn]{article} 

\usepackage{pgfplotstable}
\usepackage{booktabs}


% set column separator for our csv loading package
\pgfplotstableset{col sep=comma}

\begin{document}

\begin{table*}
\centering
\scriptsize
\caption{caption}
\label{tab:hierarchical_vs_soa}
\pgfplotstabletypeset[
    begin table=\begin{tabular},
    header=true,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    every first row/.style={after row=\midrule},
    string type,
    columns/1/.style={column name=},
    columns/3/.style={column name=},
    columns/4/.style={column name=},
    columns/6/.style={column name=},
    columns/7/.style={column name=},
    columns/9/.style={column name=},
    end table=\end{tabular}
    ]{data.csv}
\end{table*}


\end{document}

with a sample CSV file

header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE, 
,A,B,C,A,B,C,A,B,C
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1

How can I achieve this simply?

Example output

2 Answers 2

1

I don't know how to do it automatically, but it's possible to declare a \multicolumn header within before row option.

As you can see, first row (widetext header) has been commented and second row acts a header.

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

\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
    every head row/.style={
        before row={%
            \toprule
            & \multicolumn{3}{c}{Widetext} & \multicolumn{3}{c}{Widetext more} & \multicolumn{3}{c}{Widetext here}\\
        },
        after row=\midrule,
    },
    every last row/.style={
        after row=\bottomrule},
    columns/0/.style ={column name={}},
    col sep=comma, row sep=\\,
    string type,
]{
%header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,\\ 
,A,B,C,D,E,F,G,H,I\\
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
}
\caption{A very nice table}
\end{table}
\end{document}

enter image description here

3
  • Thanks for your answer. It is not ideal but it works. Mar 27, 2015 at 10:31
  • Actually I just noticed this approach as a problem because the next row are A B C A B C but you changed the CSV data to have unique names instead - the table renders as " A B C A-index4 B-index5 ... " Mar 27, 2015 at 10:56
  • @user1207217 Yes, I changed because I didn't know how to solve it. I supose pgfplots doesn't like different columns with similar name and adds -index # to distinguish. Probably this behavior can be changed, but I don't know how to do it.
    – Ignasi
    Mar 27, 2015 at 11:04
0

There was a problem with this solution (given by @Ignasi):

\documentclass[twocolumn]{article} 

\usepackage{pgfplotstable}
\usepackage{booktabs}


\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
    every head row/.style={
        before row={%
            \toprule
            & \multicolumn{3}{c}{Widetext} & \multicolumn{3}{c}{Widetext more} & \multicolumn{3}{c}{Widetext here}\\
        },
        after row=\midrule,
    },
    every last row/.style={
        after row=\bottomrule},
    columns/0/.style ={column name={}},
    col sep=comma, row sep=\\,
    string type,
]{
%header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,\\ 
,A,B,C,A,B,C,A,B,C\\
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
}
\caption{A very nice table}
\end{table}
\end{document}

Where the "header" rows were getting "-index*" appended in order to be unique. I worked around it like this:

\documentclass[twocolumn]{article} 

\usepackage{pgfplotstable}
\usepackage{booktabs}

\begin{document}
\begin{table}
\centering
\caption{A very nice table}
\pgfplotstabletypeset[
    header=false,
    every head row/.style={after row={\toprule header & \multicolumn{3}{c}{Widetext} & \multicolumn{3}{c}{Widetext more} & \multicolumn{3}{c}{Widetext here}\\ \midrule}},
    every first row/.style={after row=\midrule},
    every last row/.style={after row=\bottomrule},
    string type,
    columns/0/.style={column name=},
    columns/1/.style={column name=},
    columns/2/.style={column name=},
    columns/3/.style={column name=},
    columns/4/.style={column name=},
    columns/5/.style={column name=},
    columns/6/.style={column name=},
    columns/7/.style={column name=},
    columns/8/.style={column name=},
    columns/9/.style={column name=},
    col sep=comma, row sep=\\,
    string type,
]{
%header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,\\ 
,A,B,C,A,B,C,A,B,C\\
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
}
\end{table}
\end{document}

But I dislike the solution because extra space is left at the top of the table due to invisible blank headers, and having to specify each column name.

1
  • Just a suggestion, I think you should write a follow-up question instead of answering yourself. There will be more opportunities to find someone who reads (and perhaps answer) it.
    – Ignasi
    Mar 28, 2015 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .