[Tex/LaTex] pfgplotstable and siunitx, unit column from .csv file

pgfplotstablesiunitx

I have some data in a .csv file that I would like to typeset with a combination of pgfplotstable and siunitx. Consider the following (not so extremely minimal) MWE who throws the error

Undefined control sequence. \micro ->\ERROR l.49 ]{mytable.csv}\par

Why does this go wrong?

\documentclass[norsk,10pt,a4paper]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx,parskip,url,xcolor,tabularx,array,adjustbox,booktabs}
\usepackage[showframe]{geometry}
\usepackage[hang,bf,small]{caption}
\usepackage{capt-of,pgfplotstable}
\usepackage{siunitx}
\sisetup{exponent-product = {\cdot},output-decimal-marker={,}, per-mode=symbol}

\begin{filecontents*}{mytable.csv}
Chem.;         Avg. Conc.;    Avg. Conc. Norm.;   Conc. Unit;                   Mass sum;      Mass unit
ammonium;      159083,33;     114450,21;          \micro\gram\per\liter;        2839,463;      \kilo\gram
\end{filecontents*}



\begin{document}

Some text here

\captionof{table}{Some caption text}
{\centering
\pgfplotstabletypeset[%
    col sep=semicolon,
    read comma as period=true,
    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 column/1/.style={
        },
    display column/2/.style={
        },
    display column/3/.style={%
        string type, column type={s},%
        },%
    display column/4/.style={
        },
    display column/5/.style={%
        string type, column type={s},
        },%
    ]{mytable.csv}\par 
}

\end{document}

Best Answer

Your MWE has bunch of errors, the most of them I succeeded to eliminate, so the MWE is now at least possible to compile ...

Edit (1): Meanwhile I dug through pgfplotstable manual and SE in searching of not resolved issues in my answer. This was quite fruitful; I was able to found my mistakes, which I introduced when correcting errors in your MWE ... The resulting new code and image of obtained table are given below.

\documentclass[margin=5mm,preview]{standalone}
    \usepackage{siunitx} % Formats the units and values
\sisetup{                           % setup siunitx ...
        round-mode      = places,   % rounds numbers
        round-precision = 2,        % to 3 places
        per-mode        = symbol,   % kg/dm^3 instead kgm^{-3}
      group-four-digits = true,     %   for 1 234,567       
        }
    \usepackage{booktabs}           % for table rules

    \usepackage{pgfplotstable}      % Generates table from .csv
    \usepackage{filecontents}       % <--- important: enable table 
                                    % refreshing at each compilation

    \usepackage[hang,bf,small]{caption}                                    
%---------------------------------------------------------------%
\begin{filecontents*}{mytable.csv}
Chem.; Avg. Conc.; Avg. Conc. Norm.; Conc. unit; Mass sum; Mass unit
Ammonium ; 159083.33; 114450.21; \si{\micro\gram\per\liter}; 2839.463; \si{\kilo\gram}
Ammonium* ; 1234.123; 4567.890;  \si{\micro\gram\per\liter}; 2839.46; \si{\kilo\gram}
\end{filecontents*}
%---------------------------------------------------------------%

    \begin{document}
Test of use \verb+siunitx+ units syntax in text \si{\micro\gram\per\liter} and \si{\kilo\gram},

\captionof{table}{Some caption text}
\pgfplotstabletypeset[
    multicolumn names,
    col sep=semicolon,  % 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},
display columns/0/.style={string type},  
display columns/1/.style={column type={S[table-format=7.3]}},% use
display columns/2/.style={column type={S[table-format=7.3]}},% siunitx
display columns/3/.style={string type}, % units as string
display columns/4/.style={column type={S[table-format=5.3]}},% for formating
display columns/5/.style={string type}, % units as string
    ]{mytable.csv}
    \end{document}

As you can see, in table I introduced S column type of column and used siunitx engine for number formatting in columns.

Warning: Due to lengthy column heads, the table is wider than usual \textwidth in article document-class.

enter image description here

Edit (2): Meanwhile, Stefan Pinnow in his comment described alternative solution (which for unknown reason didn't work before, now I cannot remember, what was wrong :-( ). However, corrected code is given below.

\documentclass[margin=5mm,preview]{standalone}
    \usepackage{siunitx} % Formats the units and values
\sisetup{                           % setup siunitx ...
        round-mode      = places,   % rounds numbers
        round-precision = 2,        % to 3 places
        per-mode        = symbol,   % kg/dm^3 instead kgm^{-3}
      group-four-digits = true,     %   for 1 234,567       
        }
    \usepackage{booktabs}           % for table rules

    \usepackage{pgfplotstable}      % Generates table from .csv
    \usepackage{filecontents}       % <--- important: enable table 
                                    % refreshing at each compilation

    \usepackage[hang,bf,small]{caption}                                    
%---------------------------------------------------------------%
\begin{filecontents*}{mytable.csv}
Chem.; Avg. Conc.; Avg. Conc. Norm.; Conc. unit; Mass sum; Mass unit
Ammonium ; 159083.33; 114450.21; \micro\gram\per\liter; 2839.463; \kilo\gram
Ammonium* ; 1234.123; 4567.890;  \micro\gram\per\liter; 2839.46; \kilo\gram
\end{filecontents*}
%---------------------------------------------------------------%

    \begin{document}
Test of use \verb+siunitx+ units syntax in text \si{\micro\gram\per\liter} and \si{\kilo\gram},

\captionof{table}{Some caption text}
\pgfplotstabletypeset[
    multicolumn names,
    col sep=semicolon,  % 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},
display columns/0/.style={string type},  
display columns/1/.style={column type={S[table-format=7.3]}},% use
display columns/2/.style={column type={S[table-format=7.3]}},% siunitx
display columns/3/.style={column type={s}}, % for units
display columns/4/.style={column type={S[table-format=5.3]}},% for formating
display columns/5/.style={column type={s}}, % for units 
    ]{mytable.csv}
    \end{document}

Result is the same as before!

Related Question