1

I have a csv file with four columns

Species; Phase; Chemical formula; Delta f H

which I'm trying to load into a table using pgfplotstable.

When I use this code

\documentclass[margin=2pt,12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{siunitx} 
\sisetup{per-mode = symbol, group-four-digits = true}

\usepackage{booktabs, pgfplotstable}
\usepackage{filecontents}

\usepackage{mhchem}                                  

\begin{filecontents}{inorganicsubstances.csv}
Species;Phase;Chemical formula;Delta f H
Aluminium;s;Al;0
Aluminium chloride;s;AlCl3;-705.63
Aluminium oxide;s;Al2O3;-1675.5
Aluminium hydroxide;s;Al(OH)3;-1277
\end{filecontents}

\begin{document}
\pgfplotstabletypeset[
multicolumn names,
col sep=semicolon,
string type,
header=has colnames,
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type, column type={l}},
display columns/1/.style={string type, column type={c}},
display columns/2/.style={string type, column type={l}},  
display columns/3/.style={column type={S[table-format=5.3]}},
columns/Chemical formula/.style={
postproc cell content/.append style={
/pgfplots/table/@cell content/.add={\ce{#1}} % does not work
}
}
]{inorganicsubstances.csv}
\end{document}

I get a table, which is almost correct:

Screenshot of incorrectly typeset table.

For the chemical formulae, I'm using mhchem, i.e. the \ce{} command which turns AlCl3 into AlCl₃. However, I can't figure out how to implement this automatically, without editing the csv file manually by adding the \ce{} command to each cell (a few hundred lines). On a small scale, adding \ce{} manually works.

For the fourth column, I'm using the S table format from SIunitx. I also can't figure out how to typeset the column header correctly, i.e. it should be "Δf H [kJ/mol]" instead of the placeholder.

7
  • Try verb string type for that column. Feb 19, 2021 at 12:35
  • @JohnKormylo that does not seem to work.
    – Max R
    Feb 19, 2021 at 12:44
  • Let the following answer inspire you: tex.stackexchange.com/a/112585/148845 Feb 19, 2021 at 13:07
  • I've tried something like \newcolumntype{ceq}{>{\ce{l<{}}}} but it doesn't seem to work, as I get a generic Missing \begin{document} error.
    – Max R
    Feb 19, 2021 at 13:28
  • 1
    @MaxR if you want feed a cell content to a command you can use the collcell package
    – cgnieder
    Feb 19, 2021 at 18:19

1 Answer 1

1

I found the solution to fix the typesetting of the chemical formulae.

Since there is a lot of data, I had to use a long table.

This code

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[margin=1cm,includefoot]{geometry}

\usepackage{siunitx} 
\sisetup{per-mode = symbol, group-four-digits = true}

\usepackage{booktabs, pgfplotstable,colortbl,array,longtable}
\usepackage{filecontents}

\usepackage{mhchem}
\usepackage[hang,bf,small]{caption}                                    

\begin{filecontents}{inorganicsubstances.csv}
Species;Phase;Chemical formula;Delta f H
Aluminium;s;Al;0
Aluminium chloride;s;AlCl3;-705.63
Aluminium oxide;s;Al2O3;-1675.5
Aluminium hydroxide;s;Al(OH)3;-1277
\end{filecontents}

\renewcommand{\arraystretch}{1.2} % add space in rows

\begin{document}
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
    \pgfplotstableset{
        empty header/.style={
          every head row/.style={output empty row},
        }
    }
}

\pgfplotstabletypeset[
every even row/.style={
multicolumn names,
before row={\rowcolor[gray]{0.9}}},
empty header,
begin table=\begin{longtable},
    every first row/.append style={before row={%
    \caption{Standard Enthalpies of Formation at \SI{298.15}{K} (\SI{25}{\celsius}) and \SI{1}{atm}.}%
    \label{tab:DataTable}\\\toprule
    Species & Phase & Chemical Formula & {$\Delta_\mathrm{f} H^{0}$}\\ \midrule    
    \endfirsthead
    %
    \multicolumn{4}{c}%
    {Table \thetable\ continued from previous page.} \\
    \toprule 
    %
    Species & Phase & Chemical Formula & {$\Delta_\mathrm{f} H^{0}$} \\ \midrule  
    \endhead
    %
    \midrule \multicolumn{4}{r}{{Continued on next page\ldots}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{4}{r}{{Concluded.}} \\ \bottomrule
    \endlastfoot
    }},%
%
end table=\end{longtable},
col sep=semicolon,
string type,
%every head row/.style={before row=\toprule,after row=\midrule},
%every last row/.style={after row=\bottomrule},
display columns/0/.style={string type, column type={l}},
display columns/1/.style={string type, column type={l}},
display columns/2/.style={string type, column type={l}},  
display columns/3/.style={string type, column type={S[table-format=5.5]}},
columns/Chemical formula/.style={%
postproc cell content/.code={%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\ce{##1}}%
}
}
]{inorganicsubstances.csv}
\end{document}

results in this (long) table, which is correct:

Correctly typeset table

You must log in to answer this question.

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