3

I am trying to highlight some cells in a pgfplotstable table, while at the same time using dec sep align to align numbers to their decimal point. I managed to do it using colors, but I would like to do it using bold text.

I found some information online, but never dealing with dec sep align and bold text together. Here is what I was able to do:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfcalendar}
\usepackage{pgfplotstable}
\pgfplotstableset{
    highlight red/.style = {
        postproc cell content/.append code={
            \edef\temp{%
                \noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}%
                    {\noexpand\color{red}\pgfkeysvalueof{/pgfplots/table/@cell content}}%
            }\temp%
        },
    },
    highlight bold 1/.style = {
        postproc cell content/.append code={
            \edef\temp{%
                \noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}%
                    {\noexpand\bfseries\pgfkeysvalueof{/pgfplots/table/@cell content}}%
            }\temp%
        },
    },
    highlight bold 2/.style = {
        postproc cell content/.style={
            @cell content/.add={$\bf}{$},
        },
    },
    highlight bold 3/.style = {
        postproc cell content/.append code={
            \ifnum0=\pgfplotstablepartno
                \pgfkeysalso{@cell content=\textbf{##1}}%
            \fi
        },
    },
}


\usepackage{filecontents}
\begin{filecontents}{table.dat}
date,value
2018-01-01,1.0
2018-01-02,2.445
2018-01-03,3.23
2018-01-04,-4.454
2018-01-05,5
2018-01-06,6
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{table.dat}\table

\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
        dec sep align,
    },
    every row 3 column 1/.style = {highlight red},
]\table
\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
    },
    every row 3 column 1/.style = {highlight red},
]\table

\bigskip

\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
        dec sep align,
    },
    every row 3 column 1/.style = {highlight bold 1},
]\table
\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
    },
    every row 3 column 1/.style = {highlight bold 1},
]\table

\bigskip

\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
        dec sep align,
    },
    every row 3 column 1/.style = {highlight bold 2},
]\table
\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
    },
    every row 3 column 1/.style = {highlight bold 2},
]\table

\bigskip

\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
        dec sep align,
    },
    every row 3 column 1/.style = {highlight bold 3},
]\table
\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
    },
    every row 3 column 1/.style = {highlight bold 3},
]\table

\end{document}

Output

  • In the first approach highlight bold 1 I just copied the code for color and changed to bold text, but it does not work at all.
  • The second approach highlight bold 2 works without dec sep align, otherwise the cell is not bold.
  • The third approach highlight bold 3 works again without dec sep align, otherwise it will input the entire number instead of just the integer part (and the same would be for the decimal part if I used \ifnum1=\pgfplotstablepartno).

Now I am out of ideas. Is it possible to do what I want?

1 Answer 1

4

The problem is, that pgfplotstable gives out the numbers in math mode, i.e. 4 will become $4$. This can be seen with the option outfile={somefile.tex}, which will write the table code generated by pgfplotstable to the given file. So you need to switch to bold math before the $, which can be done by adding \boldmath before the table content.

The code:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfcalendar}
\usepackage{pgfplotstable}
\pgfplotstableset{
    highlight bold 4/.style = {
        postproc cell content/.style={
            @cell content/.add={\boldmath}{},
        },
    },
}

\usepackage{filecontents}
\begin{filecontents}{table.dat}
date,value
2018-01-01,1.0
2018-01-02,2.445
2018-01-03,3.23
2018-01-04,-4.454
2018-01-05,5
2018-01-06,6
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{table.dat}\table
\pgfplotstabletypeset[outfile={highlight-b4.tex},
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
        dec sep align,
    },
    every row 3 column 1/.style = {highlight bold 4},
]\table
\pgfplotstabletypeset[
    columns/date/.style = {
        date type,
    },
    columns/value/.style = {
    },
    every row 3 column 1/.style = {highlight bold 4},
]\table
\end{document}

The result:

enter image description here

The code generated by pgfplotstable:

\begin {tabular}{cr<{\pgfplotstableresetcolortbloverhangright }@{}l<{\pgfplotstableresetcolortbloverhangleft }}%
date&\multicolumn {2}{c}{value}\\%
2018/01/01&$1$&$$\\%
2018/01/02&$2$&$.45$\\%
2018/01/03&$3$&$.23$\\%
2018/01/04&\boldmath $-4$&\boldmath $.45$\\%
2018/01/05&$5$&$$\\%
2018/01/06&$6$&$$\\%
\end {tabular}%

You must log in to answer this question.

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