# Colored text in a dcolumn table

I want to highlight text in a table using my own color definitions. The table is a dcolumn table. It doesn't work, I get the error messages ! Missing } inserted. and ! Extra }, or forgotten \$. three times.

Below is a min example for the beamer class (I did not check out other classes).

\documentclass{beamer}

\usepackage[utf8]{inputenc}
\usepackage{default}
\usepackage{color, colortbl}
\usepackage{booktabs, dcolumn}
# Now I create my own color to highlight text
\definecolor{red4}{rgb}{0.545,0.000,0.000}
\newcommand{\mcdr}[1]{{{\bf \color{red4}#1}}}

\begin{document}
#-----------
\begin{frame}

\begin{table}
\begin{tabular}{p{1cm} D{.}{.}{3.5}@{} }
\toprule
average & {\mcdr{-0.01}}     \\
\end{tabular}
\end{table}

\end{frame}
#-----------
\end{document}


However, embedding \mcdr in a normal table, everything works fine:

  \begin{center}
\begin{tabular}{l | l }
early& {\mcdr{0.1}}  \tabularnewline
late& \textit{0.11} \tabularnewline
\end{tabular}
\end{center}

• Welcome to TeX.SE! When posting some code, please do check that it's free of syntax errors, such as stating \bein{table} and using # instead of % as the comment symbol. – Mico Jan 9 '15 at 20:29

D column type is consist from two (sub)columns. For example, if you put \color{red4} before number -0,01 you will see, that only first part of number (-1) become red4 colored. Closing this number in any macro hide "." which is separator between (sub) columns.

The solution close to this what you (as I understand) like can be:

\documentclass{beamer}

%\usepackage[utf8]{inputenc}
%\usepackage{default}
\usepackage{color, colortbl}
\usepackage{booktabs, dcolumn}
% Now I create my own color to highlight text
\definecolor{red4}{rgb}{0.545,0.000,0.000}
\newcommand{\mcdr}[1]{{{\bf \color{red4}#1}}}
\newcolumntype{F}[3]{>{\color{red4}\DC@{#1}{#2}{#3}}c<{\DC@end}}
\newcolumntype{d}[1]{F{.}{.}{#1}}

\begin{document}
%-----------
\begin{frame}

\begin{table}
\begin{tabular}{p{1cm} D{.}{.}{3.5}@{} }
\toprule
average & \mcdr{-0}.\mcdr{01}     \\
\bottomrule
\end{tabular}
\end{table}

\end{frame}
%-----------
\end{document}


• The - has the spacing of a binary (not unary) operator. Rather use {-}. – Werner Jan 9 '15 at 21:04

Instead of loading the dcolumn package and its D column type, I recommend (for this application at least) loading the siunitx package and using its S column type. Separately, I think you should eiter use color or bold, but not both simultaneously; I'd go for color to highlight items in a beamer document.

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{xcolor, booktabs, siunitx}
% Now I create my own color to highlight text
\definecolor{red4}{rgb}{0.545,0.000,0.000}
\newcommand{\mcdr}{\color{red4}}

\begin{document}
\begin{frame}
\begin{table}
\begin{tabular}{p{1.5cm}
S[table-format=3.5, group-digits=false]
@{} }
\toprule
average    & \mcdr -0.01      \\
difference & \mcdr -12.34567  \\
\end{tabular}
\end{table}
\end{frame}
\end{document}

• This seems to be a very good package. Only, I cannot change because my tables come from the R package stargazer(). – MERose Jan 13 '15 at 16:42
• @MERose - It's important to mention this when you posted the query. – Mico Jan 13 '15 at 17:07
• ...in which case I would not have learned of siunitx! – MERose Jan 13 '15 at 21:19