2

I have a table that has alternating rows of percentages and counts.

How can I conditionally apply SIunitx to every second row? I want the row of percentages to be left as it is (to one decimal place) and need the row of counts to be rounded to integers, italicised and surrounded by brackets.

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{listings}
\usepackage{pgfplotstable}

 \begin{filecontents*}{test.csv}
{} &22.3\\
15000.9999 & 23568.44485\\
45.1 &22.3\\
15000.9999 & 23568.44485\\
{} &22.3\\
15000.9999 & 23568.44485\\
45.1 &22.3\\
15000.9999 & 23568.44485\\
 \end{filecontents*}


\begin{document}

\pgfplotstableread[col sep=&,header=false]{test.csv}{\mytable}
\begin{table}[!htb]
\begin{center}
\resizebox{1.0\textwidth}{!}{%
\pgfplotstabletypeset[
every first row/.style={before row=\toprule},
string type,
every head row/.style={output empty row},
every last row/.style={after row=\bottomrule},
postproc cell content/.code={
 \ifnum\pgfplotstablecol=1
        \ifodd\pgfplotstablerow\relax
        \pgfkeysalso{@cell content={(\num[math-rm=\mathit,round-mode=places,round-precision=0]{#1}})}%
        \fi
\else\fi
},
]{\mytable}
}
\end{center}
\end{table}
\end{document}

The following is not working as I can't specify the latex-type format to `\pgftabletypeset'. The end-of-line markings are stopping it from compiling.

I would also like to insert a space after every second row, which I was able to do with the following workaround (in pseudocode) when just using tabular.

\global\let\restorecr=\\ 
. . . 
\global\let\oldcr=\\
\global\def\crsng{\global\let\\=\crdbl\oldcr}
\global\def\crdbl{\global\let\\=\crsng\oldcr[4pt]}
\global\let\\=\crdbl
. . . 
\global\let\restorecr=\\ 

How can I achieve this with pgfplotstable?

2

It sounds like you'd like an automated approach, which makes me think of the pgfplotstable package.

In the code below, I've used:

\pgfplotstabletypeset[
    every head row/.style={output empty row},
    postproc cell content/.code={
        \ifodd\pgfplotstablerow\relax
        \pgfkeysalso{@cell content={(\num[math-rm=\mathit,round-mode=places,round-precision=0]{#1}})}%
        \fi
    },
]{45.1 22.3
    15000.9999  23568.44485
    45.1 22.3
    15000.9999  23568.44485
    45.1 22.3
    15000.9999  23568.44485
    45.1 22.3
    15000.9999  23568.44485
}

which gives the following output:

screenshot

Note that I've used the \num command from the siunitx package, with options round-mode=places,round-precision=0 to give your requested precision.

I believe that this is very close to your desired output. It sounds like you would like an S-type column, which aligns the bracketed numbers, and ignores the percentage numbers; this would be quite tricky, as the S-type column aligns numbers, and will throw errors if ( and/or ) are present.

Here's the complete code to play with:

% arara: pdflatex
% arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{siunitx}

\begin{document}

\pgfplotstabletypeset[
    every head row/.style={output empty row},
    postproc cell content/.code={
        \ifodd\pgfplotstablerow\relax
            \pgfkeysalso{@cell content={(\num[math-rm=\mathit,round-mode=places,round-precision=0]{#1}})}%
        \fi
    },
]{
    45.1 22.3
    15000.9999  23568.44485
    45.1 22.3
    15000.9999  23568.44485
    45.1 22.3
    15000.9999  23568.44485
    45.1 22.3
    15000.9999  23568.44485
}
\end{document}
| improve this answer | |
  • This is working fine. Except that I get an error when I read the data from a text file and each line ends in `\`. – user2146441 Nov 16 '15 at 20:57
  • If you update your question, I'll take a look. In the future, it's best to include all such details from the beginning :) – cmhughes Nov 16 '15 at 21:09
0

I'm not sure, if I correctly understand your question, so I will help myself with the following picture:

enter image description here

Is this what you looking for? Above picture is generated by:

\documentclass{article}
    \usepackage{booktabs,multirow}
    \usepackage{siunitx}

\newcommand{\ml}[1]{\multicolumn{1}{l}{(\itshape #1\upshape\,\,\%)}}

\begin{document}
    \begin{tabular}{l*{2}{S[table-format = 5.3,
                            round-mode = places,
                            round-precision = 2]}}
    \cmidrule[\heavyrulewidth]{2-3}
                                & {Case A}      &   {Case B}        \\
    \midrule
\multirow{2}*{measurement 1}    &   \ml{45.1}   &   \ml{22.3}       \\
                                &   15000.9999  &   23568.44485     \\
    \addlinespace
\multirow{2}*{measurement 2}    &   \ml{45.1}   &   \ml{22.3}       \\
                                &   15000.9999  &   23568.44485     \\
    \addlinespace
\multirow{2}*{measurement 3}    &   \ml{45.1}   &   \ml{22.3}       \\
                                &   15000.9999  &   23568.44485     \\
    \addlinespace
\multirow{2}*{measurement 4}    &   \ml{45.1}   &   \ml{22.3}       \\
                                &   15000.9999  &   23568.44485     \\
    \bottomrule

    \end{tabular}
\end{document}

Probably you looking for something else, but I'm afraid that with standard tabular environment other solution is not possible.

Edit: For emphasize benefits above solution I enrich above table with some formatting possibilities which is (so far as I know) not all possible with pgfplotstable (it is not intended for this). Of course, for simple table without multicolumn, multirow and separating lines groups with extra vertical spaces etc, use of pgfplotstable -- as show @cmhughes in his answer -- is right soluton.

If you can leave with more simple number formatting as enables siunitx, than solution proposed here is the starting point for searching for solution of your problem.

| improve this answer | |
  • The data is being generated elsewhere. I do not want to apply SIunitx to the odd rows. I would like to apply Siunitx to the even rows only. It is possible to apply colours to every second row, so I presumed it would be possible to apply SIUnitx to every second row also. – user2146441 Nov 15 '15 at 15:49
  • 1
    Colors and number formatting are very different things ... latter I will see, if I can find a solution for your wish. – Zarko Nov 15 '15 at 16:43
  • I see that meanwhile @cmhughes propose very elegant solution with pgfplotstable, however this solution make trouble if you like to exclude some column from rule for formatting rows. – Zarko Nov 15 '15 at 19:56

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.