3

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}
3

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!

  • Sorry about the errors, I'm fighting with Overleaf as my laptop is on vacation (service). Thanks for kicking me in the right direction! :-) – Holene Jan 27 '16 at 13:50
  • But to get something out of this question: isn't it possible to specify the s column from siunitx with pgfplotstable? – Holene Jan 27 '16 at 13:51
  • 1
    I didn't find anything explicit about this in manual, so I don't know now. However, right syntax for siunitx column type is S (not s), and it enables different number formatting. This formatting is on someway integrated in pgfplotstable, so there is seems no need for this column type (you can see this, if you comment \sisetup{...}, the formating of numbers will change). I still learning this awesome (and sometimes seems to be complicated) package ... – Zarko Jan 27 '16 at 14:00
  • 1
    @Holene, see edit of my answer. I correct (hopefully) all my mistakes/errors in the first version of mine answer and resolve issues about use S column type in pgfplotstable. – Zarko Jan 27 '16 at 19:34
  • We ended up on quite the same page :-) Thank you for your effort! – Holene Jan 28 '16 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.