1

I am currently experimenting with the pgfplotstable package and currently I am working on the default settings I want to use for my tables.

My problem arises if my table contains text as well as numeric values. I managed to differentiate between the two cases and the only problem I am left with is the alignment of the text in my columns.
The problem is that I want to use the dec sep align option in order to format my numeric values properly.

With help of this question I managed to get my text into the table without messing with any of the other cells.
The problem is that the text is now always placed in the left "subcell" and in case there is a number in the same column the text will be shifted to the left in order to make room for the decimals of that number.

I figured that I could avoid this problem by simply making my text-cell spanning over two columns (so that it uses both "sub-cells") via \multicolumn{2}{c}{#1} (see below). However when I try to do this the inserted & does not get interpreted as part of the table and LaTex tells me this:

Extra alignment tab has been changed to \cr. 

This is the code I am using:

\documentclass{article}

\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{array}
\usepackage{xstring}
\usepackage{multicol}

\pgfplotstableset{
    col sep = comma,
    dec sep align={c},
    every head row/.style={before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule},
    /pgfplots/table/assign cell content/.code={
        \IfDecimal{#1}{
            % format as number
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfutilensuremath{\pgfmathprintnumber{#1}}}
        }{
            % use as String -> add second col in order to unmess dec sep align
            \ifnum\pgfplotstablepartno=0%
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{2}{c}{#1}}
                \pgfkeys{/pgfplots/table/@cell content/.add={}{&}}
            \fi
        }
    },
}

\begin{document}
    \begin{filecontents}{Sample.csv}
        Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7
        1.34, 2.73, 358000, 0.003, 5, 1500, 0.000374
        1.34, 2.73, 358000, 0.003, 7, 1325.4, 0.012356
        1.34, 2.734, 358000, 0.003, 12, 998.331, 0.12345
        1.34, 5.073, 358000, 0.003, 17, 12353.274583, 0.0505134567
        1.34, 22.77, 358000, test, 356, 9999.99, 0.3
    \end{filecontents}

    \pgfplotstabletypeset {Sample.csv}
\end{document}

So my question is: How can I get my text in the center of the column (in the center of both "sub-cells")?

1

I found the issue: when using \multicol I don't need the extra alignment tab at all.

Therefore the code has to look like this:

\documentclass{article}

\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{array}
\usepackage{xstring}
\usepackage{multicol}

\pgfplotstableset{
    col sep = comma,
    dec sep align={c},
    every head row/.style={before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule},
    /pgfplots/table/assign cell content/.code={
        \IfDecimal{#1}{
            % format as number
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfutilensuremath{\pgfmathprintnumber{#1}}}
        }{
            % use as String -> add second col in order to unmess dec sep align
            \ifnum\pgfplotstablepartno=0%
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{2}{c}{#1}}
            \fi
        }
    },
}

\begin{document}
    \begin{filecontents}{Sample.csv}
        Col 1, Col 2, Col 3, Col 4, Col 5, Col 6, Col 7
        1.34, 2.73, 358000, 0.003, 5, 1500, 0.000374
        1.34, 2.73, 358000, 0.003, 7, 1325.4, 0.012356
        1.34, 2.734, 358000, 0.003, 12, 998.331, 0.12345
        1.34, 5.073, 358000, 0.003, 17, 12353.274583, 0.0505134567
        1.34, 22.77, 358000, test, 356, 9999.99, 0.3
    \end{filecontents}

    \pgfplotstabletypeset {Sample.csv}
\end{document}

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.