2

I have a table where the last row needs to be bold. Currently, I can make it bold by following this question. However, when I make it bold, I lose all my number formatting.

Here are the 2 images: enter image description here

enter image description here

Below is my whole file which produces a page with that table. I comment out the second to last row to produce the picture without bold.

\documentclass[11pt]{report}
\usepackage{arydshln}
\usepackage{amsmath}
\usepackage{array}
\usepackage[landscape]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{fancyhdr}
\usepackage{datetime}
\usepackage{anyfontsize}
\usepackage{wallpaper}
\usepackage{fontspec}
\usepackage{pgfplots,pgfplotstable}
\usepackage{numprint}
\npthousandsep{,}
\npdecimalsign{.}
\usetikzlibrary{plotmarks}

\begin{filecontents}{test.dat}
NAME,DOLKWH,YEAR1MAV,YEAR1TAV
Without Solar,21.587089894172,149.80185755952,1797.6222907142
Solar Payment,15.44620773416,128.15382726042,1537.845927125
Remaining Utility,-0.011600973866237,1.5746741682751,18.896090019302
New Bill,15.44620773416,277.95568481994,1556.7420171443
Savings,6.1408821600121,-128.15382726042,240.88027356994
\end{filecontents}


\pgfplotstableread[col sep=comma]{test.dat} \savingsTable

\newcommand\displ[1]{%  Indicates the number of rows of the data file that we want to display in a given table         
\ifnum\pgfplotstablerow<#1\relax%
\else\pgfplotstableuserowfalse\fi}

%%  TABLES  %%

\pgfplotstableset{ % Makes bold a whole row of a table
    highlightrow/.style={
        postproc cell content/.append code={
           \count0=\pgfplotstablerow
            \advance\count0 by1
            \ifnum\count0=#1
            \pgfkeysalso{@cell content=\textbf{##1}}
            \fi
        },
    },
}

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1cm}}  % Right aligned, centered column
\newcolumntype{L}[1]{>{\arraybackslash\raggedright}p{#1cm}} % Left aligned, centered column

%%   COLORS SCHEME  %%

\definecolor{YellowBar}{HTML}{FFE300}

\everymath{\textstyle}

\begin{document}
\begin{minipage}[t]{.48\linewidth}
{\def\arraystretch{1.85}
\newcommand\tabw{2.8}
\pgfplotstabletypeset[
/pgf/number format/fixed zerofill=true,
row predicate/.code={\displ{5}},
create on use/TITLE/.style={%
  create col/set list={%
    \begin{tabular}{@{}l}
    Without Solar\\[-2.5ex]\tiny
    Estimated Annual increase 3.5\%
    \end{tabular},
    \begin{tabular}{@{}l}
    Solar Payment\\[-2.5ex]\tiny
    Annual Escalator 2.9\%
    \end{tabular},
    Remaining Utility,New Bill,Savings
  }
},
columns={TITLE,DOLKWH,YEAR1MAV,YEAR1TAV},
columns/TITLE/.style={string type, column name=,column type=|L{\tabw}},
%
columns/DOLKWH/.style={%
  /pgf/number format/fixed,precision=1,
  column name=\scriptsize\textcent/kWh,column type=|R{\tabw},
  postproc cell content/.append style={/pgfplots/table/@cell content/.add={}{\textcent}}
},
%
columns/YEAR1MAV/.style={
  column name={%
      \scriptsize\parbox[b]{2cm}{%
        \flushright\vskip-1.5ex Year 1\\[-1pt]Monthly\\[-1pt]Average\\[-4pt]
      }
    },
    column type=|R{\tabw},
    postproc cell content/.append style={/pgfplots/table/@cell content/.add={\$}{}
  }
},
%
columns/YEAR1TAV/.style={
   /pgf/number format/set decimal separator={.},thousands separator={,},precision=2,
  column name={%
    \scriptsize\parbox[b]{2cm}{%
      \flushright\vskip-1.5ex Year 1\\[-1pt]Total\\[-4pt]}
  },
  column type=|R{\tabw}|,postproc cell content/.append style={/pgfplots/table/@cell content/.add={\$}{}},
},
after row={\hline},
every head row/.style={before row={\hline\rowcolor{gray!10}}},
every row no 3/.style={before row={\rowcolor{yellow!50}}},
every row no 4/.style={before row={\rowcolor{YellowBar}}},
highlightrow={4},highlightrow={5}
]\savingsTable
}
\end{minipage}

\end{document}

UPDATE: everything is working now thanks to @Zarko, however the font on the last 2 rows changes from the rest of the document:

enter image description here

  • Please provide a complete example which produces the output you show in your question and want help with. – cfr Jan 6 '16 at 23:25
  • @cfr I stripped away everything else and put an entire file above. Thanks for your help. – ajon Jan 7 '16 at 0:02
  • You should include your table in your MWE. Give it as separate text claim unnecessary spending time to make Your MWE working. – Zarko Jan 7 '16 at 1:09
  • @Zarko as requested I have included the data in the file. – ajon Jan 7 '16 at 1:27
  • Thank you to upgrade MWE, however, where is defined \preSolarEscalator, I receive bunch of errors compiling your MWE. – Zarko Jan 7 '16 at 1:35
2

Your problem is wrong approach in definition of highlightrow. If you replace it with

\pgfplotstableset{ % Makes bold a whole row of a table
    highlightrow/.style={
        postproc cell content/.append code={
           \count0=\pgfplotstablerow
            \advance\count0 by1
            \ifnum\count0=#1
            \pgfkeysalso{@cell content/.add={$\bf}{$}}
            \fi
        },
    },
}

You will get desired result:

enter image description here

Addendum:

Edit: Since this problem is very interesting in general, I adopt your MWE for my archive. This adopted code has changes in column header cells (I like heads with only two lines), in the first column, where the unnecessary nested tables is omitted, the precision of numbers in second column is increased to 3.

I also omit for this table unnecessary packages and instead of report use standalone documentclass. This code

\documentclass[border=3mm]{standalone}
    \usepackage{array}
    \usepackage[table]{xcolor}
% XeLaTeX
    \usepackage{anyfontsize}
    \usepackage{fontspec}
% units and pgf-plots-table
    \usepackage{siunitx}
    \usepackage{pgfplotstable}

\begin{filecontents}{test.dat}
NAME,               DOLKWH,             YEAR1MAV,           YEAR1TAV
Without Solar,      21.587089894172,    149.80185755952,    1797.6222907142
Solar Payment,      15.44620773416,     128.15382726042,    1537.845927125
Remaining Utility,  -0.011600973866237,   1.5746741682751,    18.896090019302
New Bill,           15.44620773416,     277.95568481994,    1556.7420171443
Savings,             6.1408821600121,  -128.15382726042,    240.88027356994
\end{filecontents}
\pgfplotstableread[col sep=comma]{test.dat}\savingsTable

\newcommand\displ[1]{%  Indicates the number of rows of the data file
                     %  that we want to display in a given table
\ifnum\pgfplotstablerow<#1\relax%
\else\pgfplotstableuserowfalse\fi}

%%  TABLES  %%
\pgfplotstableset{ % Makes bold a whole row of a table
    highlightrow/.style={
        postproc cell content/.append code={
           \count0=\pgfplotstablerow
            \advance\count0 by1
            \ifnum\count0=#1
            \pgfkeysalso{@cell content/.add={$\bf}{$}}
            \fi
        },
    },
}
\newcolumntype{R}[1]{>{\raggedleft \arraybackslash}m{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}} %column

%%   COLORS SCHEME  %%
\definecolor{YellowBar}{HTML}{FFE300}
%%   MATH APPERANCE %%
\everymath{\textstyle}

    \begin{document}
%---------------------------------------------------------------%
% \begin{table}\centering\caption{..}\label{...}
    \def\arraystretch{1.5}
    \newlength\tabw
    \setlength\tabw{28mm}

\pgfplotstabletypeset[
    /pgf/number format/fixed zerofill = true,
                  row predicate/.code = {\displ{5}},
           create on use/TITLE/.style = {create col/set list={%
% first column
    Without Solar\newline\scriptsize
    Estimated Annual increase \SI{3.5}{\%}
    ,
    Solar Payment\newline\scriptsize
    Annual Escalator \SI{2.9}{\%}
    ,
    Remaining Utility, New Bill, Savings}
                                        },% end of first column
    columns={TITLE,DOLKWH,YEAR1MAV,YEAR1TAV},
    columns/TITLE/.style={string type,column name=,column type=|L{\tabw}},
% second column
    columns/DOLKWH/.style={/pgf/number format/fixed,precision=3,
                            column name=~\newline\textcent/kWh,
                            column type=|R{0.5\tabw},
                            postproc cell content/.append style={%
                                /pgfplots/table/@cell content/.add={}{\textcent}}
                            },
% third column
    columns/YEAR1MAV/.style={column name={\hfill Year 1\newline  Monthly Average},
                             column type=|R{\tabw},
                             postproc cell content/.append style={%
                                    /pgfplots/table/@cell content/.add={\$}{}}
                             },
% fourth column
    columns/YEAR1TAV/.style={column name={\hfill Year 1\newline Total},
                             column type=|R{0.6\tabw}|,
                             /pgf/number format/set decimal separator={.},
                             precision=2,
                             postproc cell content/.append style={%
                                    /pgfplots/table/@cell content/.add={\$}{}},
},
% rows styles
after row={\hline},
every head row/.style={before row={\hline\rowcolor{gray!10}}},
every row no 3/.style={before row={\rowcolor{yellow!50}}},
every row no 4/.style={before row={\rowcolor{YellowBar}}},
highlightrow={4},highlightrow={5}
        ]\savingsTable
%\end{table}
%---------------------------------------------------------------%
    \end{document} 

In above example I didn't bother with vertical and horizontal lines. Many people consider, that the looks of table without vertical rules and middle horizontal rules are more beautiful.

  • This is working except, I am using a different font, and the font of the bold rows is different. That's why I didn't notice it in your example. Do you know why that might be? – ajon Jan 8 '16 at 0:49
  • @ajon, I do not understand: what you not notice? Regarding font, I use your font. Solution work with any font, which has bold version. To have different font for boldface texts as for other text is not good idea. Nor nice, nor simple. – Zarko Jan 8 '16 at 1:09
  • I have 1 font throughout my whole document, including in this picture. However when I add the highlight on the last 2 rows, the text in those rows becomes bold, but the font on the last 2 rows changes. See the image I just posted. – ajon Jan 8 '16 at 1:11
  • I see. The difference is, that in my case is used for all table the same font (which is used in math) , in your case you use in last two rows sans serif font which seems to be default text font shape. If this is not, than I don't know, what is going wrong. If you like to have bold fonts larger or sans serif or both, this is easy to add in my solution: in definition for highlightrow instead \pgfkeysalso{@cell content/.add={$\bf}{$}} you write \pgfkeysalso{@cell content/.add={$\large\sffamily\bf}{$}} (not tested, but should work). – Zarko Jan 8 '16 at 1:22
  • @ajon, I decide to upload version of code, which I made from yours for my archive. It is quit more concise and to my test generate nicer looking table. From picture is clear, that all text use the some fonts. – Zarko Jan 8 '16 at 2:56
0

Given that your table is quite small in size, the use of TikZ is an overkill. It takes considerable time to load the package and, well, render everything (on my trusty EeePC 1001 HA it is not affordable). Sometimes it much easier to do a small table by hand. If you look at this paper by Donald Knuth himself, you will see that when he has 2 citations, he writes them explicitly as [1] and [2], no \cite trickery. He keeps it simple: no BibTeX, no bibliography environment, only one compilation. Just a couple of commands for nice manual formatting.

So, following in his footsteps, I propose the following remedy:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{textcomp}
\usepackage{booktabs}
% Dirty hack to eliminate white bars cause by booktabs' space handling
% Due to http://tex.stackexchange.com/a/11215/19642
\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\setlength{\extrarowheight}{1ex}
\setlength{\tabcolsep}{0.75em}

\newcommand{\mytinytab}[1]{{\footnotesize\bfseries% Locally reverting the hack
\setlength{\extrarowheight}{0ex}\begin{tabular}{@{}r@{}}#1\end{tabular}}}

\begin{document}

\begin{tabular}{lrrr} \toprule
\rowcolor[gray]{0.9}
\rule[-3.5ex]{0pt}{8ex} % Without this strut, the header row looks squished
 & \mytinytab{~ \\ \textcent/kWh \\ ~} %
 & \mytinytab{Year 1 \\ Monthly \\ Average} %
 & \mytinytab{Year 1 \\ Total} \\ \midrule
Without Solar & 21.6\textcent & \$149.80 & \$1797.62 \\
Solar Payment & 15.4\textcent & \$128.15 & \$1537.85 \\
Remaining Utility & 0.0\textcent & \$1.57 & \$18.90 \\
\rowcolor[rgb]{1,0.975,0.75}
\textbf{New Bill} & \textbf{15.4\textcent} & \textbf{\$277.96} & \textbf{\$1556.74} \\
\rowcolor[rgb]{1,0.95,0.5}
\textbf{Savings} & \textbf{6.1\textcent} & \textbf{\$$-$128.15} & \textbf{\$240.88} \\ \bottomrule
\end{tabular}

\end{document}

Formatted table rows

Those tiny comments under row names may be rewritten in a better way, like being put under the table as footnotes, with explicitly writtern star footnote marks and simple text notes due to \footnote being a fragile command... again, these notes should go after the table because they are relevant. I just don’t think it is correct to give summary statistics under column names in tiny font size. You should consider adding the “Annual growth” column and report those growth rates there (with cell values like “2.9%”, “4.5% (est.)” etc.).

Update. If you have multiple tables and you want to avoid menial labour, you can use external tools (such as the the open-source R statistical software) to round the values with arbitrary precision (you can search in column names with regular expressions) and then output the result to tables with LaTeX column/row separators and any LaTeX code appended (again, string manipulation). You can use one template, like the one suggested above, to rule them all (pun intended). Its parsing capabilities are enormous, too.

P.S. By the way, vertical table lines are considered ugly by many. They should be avoided as much as possible.

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.