How to change only the color of numbers (and not units) with numprint?

Here's an option using a single tabular for better vertical alignment:

enter image description here

\documentclass{article}

\usepackage{numprint,xcolor}

\begin{document}

\begin{tabular}{| *{8}{>{\color{blue}}l|} >{\color{blue}} l }
  \cline{1-8}
  \multicolumn{2}{|c|}{$\mathrm{m}^2$\rule{0pt}{\normalbaselineskip}} & % added some vertical padding
    \multicolumn{2}{c|}{$\mathrm{dm}^2$} & 
    \multicolumn{2}{c|}{$\mathrm{cm}^2$} & 
    \multicolumn{2}{c|}{$\mathrm{mm}^2$} \\
  \cline{1-8}
  \phantom{0,} &    &   & 4, &   &   &   &    & {}    \phantom{=}   \numprint[\color{black}dm^2]{4}     \\
               &    &   & 4  & 0 & 0 & 0 & 0, & {} {\color{black}=} \numprint[\color{black}mm^2]{40000} \\
               & 0, & 0 & 4  &   &   &   &    & {} {\color{black}=} \numprint[\color{black}m^2]{0,04}   \\
  \cline{1-8}
\end{tabular}

\end{document}

The entire \numprint-column is set in \color{blue} with each unit set in \color{black} (as well as the =).


You can locally patch \numprint to set \color{black} for the optional argument.

Note that tabularx is not the right tool for the job.

\documentclass{article}
\usepackage{amsmath,numprint,array,xcolor,xpatch}

\begin{document}

\begingroup
\makeatletter
\xpatchcmd{\numprint}
 {\def\nprt@oarg{#1}}
 {\def\nprt@oarg{\color{black}#1}}
 {}{}
\makeatother

\begin{tabular}{ |*{8}{>{\color{blue}}w{c}{1em}|}>{\color{blue}}l }
\cline{1-8}
\multicolumn{2}{|c|}{$\mathrm{m}^2\vphantom{\Big|}$} & 
\multicolumn{2}{c|}{$\mathrm{dm}^2$} & 
\multicolumn{2}{c|}{$\mathrm{cm}^2$} & 
\multicolumn{2}{c|}{$\mathrm{mm}^2$} \\
\cline{1-8}
&&&4\rlap{,}&&&&& \numprint[dm^2]{4} \\
&&&4&0&0&0& 0\rlap{,} &= \numprint[cm^2]{40000} \\
&0\rlap{,}&0&4&&&&& = \numprint[m^2]{0,04} \\
\cline{1-8}
\end{tabular}
\endgroup

\end{document}

enter image description here

With siunitx:

\documentclass{article}
\usepackage{amsmath,siunitx,xcolor}

\sisetup{
  output-decimal-marker={,},
  power-font=unit,
}

\begin{document}

\begingroup
\sisetup{
  number-color=blue,
}
\begin{tabular}{ |*{8}{>{\color{blue}}w{c}{1em}|}l }
\cline{1-8}
\multicolumn{2}{|c|}{\si{\square\meter}$\vphantom{\Big|}$} & 
\multicolumn{2}{c|}{\si{\square\deci\meter}} & 
\multicolumn{2}{c|}{\si{\square\centi\meter}} & 
\multicolumn{2}{c|}{\si{\square\milli\meter}} \\
\cline{1-8}
&&&4\rlap{,}&&&&& \SI{4}{\square\deci\meter} \\
&&&4&0&0&0& 0\rlap{,} &= \SI{40000}{\square\centi\meter} \\
&0\rlap{,}&0&4&&&&& = \SI{0,04}{\square\meter} \\
\cline{1-8}
\end{tabular}
\endgroup

\end{document}

enter image description here

Tags:

Color

Numprint