3

I am using "pgf-plots-table" to create tables from a csv file. Multicolumn option is used to get different alignment for the head row. The issue is, the table does not have vertical lines in the header, hence it looks ugly.

First row of the table does not have vertical lines

Please help to add vertical lines in the head row.

My code is:

\documentclass{article}
\usepackage{pgfplotstable}

\begin{filecontents*}{jobname.csv}
M,0.4,0.7,1.0,1.3
0.201,1.001,1.801,4.601,3.401
0.200,1.000,1.800,2.600,3.400
\end{filecontents*}

\begin{document}

\pgfplotstabletypeset[
    multicolumn names,
    col sep= comma,  % the separator in our .csv file
    string type,        % added in hopes of enabling alphabetic input.
    header=has colnames,
    every head row/.style={before row=\hline, after row=\hline},
    every last row/.style={after row=\hline},
    column type/.add={|}{},
    every last column/.style={column type/.add={}{|}},
]{jobname.csv}

\end{document}
2
  • 1
    Wouldn't it be better to not have vertical lines at all? Commented Sep 24, 2017 at 8:33
  • Tables without vertical lines are the new trend, but when irregular lines are present in the columns, vertical lines are needed to create better looking tables.
    – Ashok
    Commented Sep 25, 2017 at 9:46

1 Answer 1

2

You can say multicolumn name={<column spec>} to determine the type of column specification used in the header, hence multicolumn names={l|} gets you almost there, as it will add the vertical rule on the right side of every header cell. (Of course, replace l with c or r depending on your need.)

For the first cell though, you need to do a bit more, in order to add the vertical rule on both sides:

columns/M/.style={
   assign column name/.style={
      /pgfplots/table/column name={\multicolumn{1}{|l|}{##1}}
   }
},

assign column name can be used to specify the formatting for the column header. Because we only want to change this for the first column, it's placed inside columns/M/.style.

I suppose your complete table is more complex, but seeing as this specific table looks better (in my eyes) without vertical rules, I added an example of that in addition.

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

\begin{filecontents*}{jobname.csv}
M,0.4,0.7,1.0,1.3
0.201,1.001,1.801,4.601,3.401
0.200,1.000,1.800,2.600,3.400
\end{filecontents*}

\begin{document}

\pgfplotstabletypeset[
    multicolumn names={l|},
%    display columns/0/.style={ % this line is an alternative to the next one, if you prefer to access the column by index instead of name
    columns/M/.style={
      assign column name/.style={
         /pgfplots/table/column name={\multicolumn{1}{|l|}{##1}}
      }
    },
    col sep= comma,  % the separator in our .csv file
    string type,        % added in hopes of enabling alphabetic input.
    header=has colnames,
    every head row/.style={before row=\hline, after row=\hline},
    every last row/.style={after row=\hline},
    column type/.add={|}{},
    every last column/.style={column type/.add={}{|}},
]{jobname.csv}

\bigskip

\pgfplotstabletypeset[
    multicolumn names={l},
    col sep= comma,  % the separator in our .csv file
    string type,        % added in hopes of enabling alphabetic input.
    header=has colnames,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
]{jobname.csv}


\end{document}

output of code

0

You must log in to answer this question.

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