# pgfplotstable: centering colums and adding vertical lines

I'd like table to have centrally aligned columns and a vertical line between columns 2 and 3. This was possible before I included the 'dec sep align' code but afterwards the columns shifted to the left and the vertical line disappeared. Any help would be great. Here is my attempt at inputting these commands.

\documentclass[a4paper,12pt]{report}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{multirow}
\usepackage{amssymb}
\usepackage{pgfplots,pgfplotstable,filecontents}
\usepackage{array}

\begin{document}

\begin{table}[H]
\centering

\pgfkeys{/pgf/number format/.cd,fixed, fixed zerofill}
\pgfplotstabletypeset[col sep=comma,every head row/.style={before      row=\toprule,after row=\midrule},every row no 2/.style={after row=\midrule},every last row/.style={after row=\bottomrule},
columns/NG/.style={string type,column name=$M_n~(g\,m^{-2})$,column type=c,dec sep   align,precision=0},
columns/ID/.style={column name=$\rho_i\,(g\,cm^{-3})$,dec sep align},
columns/R2/.style={column type=|C,column name=$r^2$,dec sep align},
columns/IN/.style={column type=C,column name=$Intercept\,(cN/tex)$,dec sep     align},
columns/GR/.style={column type=C,column name=$Gradient\,\times10^{-4}$,dec sep    align
},
]
{% data from file
NG,ID,R2, IN, GR
0, 0.60, 0.79, -9.30, -5.81
, 0.67, 0.87, -7.10, -4.76
, 0.71, 0.94, -6.60, -4.57
12, 0.60, 0.38, 6.01, 0.02
, 0.67, 0.08, 6.41, 0.01
, 0.71, 0.42, 6.65, -0.02
}

\end{table}

\end{document}


Also, I am unsure how to insert code into a Tex post without having to space bar 4 times for every line.

• For your last question, you can highlight the code block with the mouse then use the {} button in the editor view. – David Carlisle Jun 7 '13 at 13:31
• I get ERROR: Package PGF Math Error: Could not parse input '0 0.60 0.79 -9.30 -5.81' as a floating point number, sorry. The unreadable part was near '.79 -9.30 -5.81'.. when I try to compile your code...:-(. – John Wickerson Jun 7 '13 at 14:27
• @JohnWickerson: That happens because this site converts tabs to spaces. I've edited the table to use a different column separator. – Jake Jun 7 '13 at 14:32
• @Katie I think your table looks nice without any vertical lines. To quote the booktabs documentation: "You will not go far wrong if you remember two simple guidelines at all times: 1. Never, ever use vertical rules. 2. Never use double rules." – John Wickerson Jun 7 '13 at 14:38
• @John, thanks. However, I'd like the vertical line for the differentiation of headings and data. Have I used double rules? If so, could you explain what that means please? thank you – Katie Jun 7 '13 at 14:44

Using Jake's answer to another question, (column separator in header line in column with dec sep align), I have been able to add a vertical rule for you.

I'm afraid I don't know how to center the columns when dec sep align is active.

I have taken the liberty of neatening up the formatting of your table for you, in two ways:

• Units should be typeset in normal "roman" font, rather than italics. So I wrapped them in \mathrm{...}.

• You shouldn't write whole words in math mode like $this$, because the kerning between the letters looks wrong. (It's as if you're multiplying t,h,i and s together.) You should write $\mathit{this}$ instead.

## Code

\documentclass[a4paper,12pt]{report}
\usepackage{tabularx}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{multirow}
\usepackage{amssymb}
\usepackage{pgfplots,pgfplotstable,filecontents}
\usepackage{array}

\begin{document}

\begin{table}[H]
\centering

\pgfkeys{/pgf/number format/.cd,fixed, fixed zerofill}
\pgfplotstabletypeset[col sep=comma,every head row/.style={before      row=\toprule,after row=\midrule},every row no 2/.style={after row=\midrule},every last row/.style={after row=\bottomrule},
columns/NG/.style={string type,column name=$M_n~(\mathrm{g\,m^{-2}})$,column type=c,dec sep align,precision=0},
columns/ID/.style={column name=$\rho_i\,(\mathrm{g\,cm^{-3}})$,dec sep align={c|},column type/.add={}{|}},
columns/R2/.style={column type=C,column name=$r^2$, dec sep align},
columns/IN/.style={column type=C,column name=$\mathit{Intercept}\,(\mathrm{cN/tex})$,dec sep align},
columns/GR/.style={column type=C,column name=$\mathit{Gradient}\,\times10^{-4}$,dec sep align
}]
{% data from file
NG,ID,R2, IN, GR
0, 0.60, 0.79, -9.30, -5.81
, 0.67, 0.87, -7.10, -4.76
, 0.71, 0.94, -6.60, -4.57
12, 0.60, 0.38, 6.01, 0.02
, 0.67, 0.08, 6.41, 0.01
, 0.71, 0.42, 6.65, -0.02
}

\end{table}
\end{document}


• Thank you very much. This has answered my question for the vertical line. However, the columns are still to the left. I would like to accept this as answered but would still like help with that part. – Katie Jun 7 '13 at 16:20
• Great, glad to help. I'm afraid I can't work out how to get the centering, but somebody should know. I tweaked your formatting a bit -- see my updated answer. – John Wickerson Jun 7 '13 at 16:42