6

This question is related to this one: Colour only the text in a tabularx environment

I want to write as a succession of equality in blue (only the numbers) so that each line of the table has a corresponding equality line to its right.

capture

\documentclass{article}
\usepackage{amsmath,numprint,tabularx,xcolor}

\begin{document}
\begin{minipage}{.4\linewidth}
\begin{tabularx}{5cm}{|*{11}{>{\leavevmode\color{blue}}X|}}
\hline
\multicolumn{2}{|c|}{$\rm m^2$} & 
\multicolumn{2}{c|}{$\rm dm^2$} & 
\multicolumn{2}{c|}{$\rm cm^2$} & 
\multicolumn{2}{c|}{$\rm mm^2$} \\
\hline
&&&4,&&&& \\
&&&4&0&0&0& 0,\\
&0,&0&4&&&& \\
\hline
\end{tabularx}
\end{minipage}\quad
\begin{minipage}{.2\linewidth}
\begin{align*}
&\numprint[dm^2]{4}\\
&=\numprint[mm^2]{40000}\\
                    &=\numprint[m^2]{0,04}
\end{align*}
\end{minipage}

\end{document}

In the numprint manual, there is a hack that allows you to color the negative numbers on pages 18-19, I couldn't modify it to write the numbers in blue.

\makeatletter
\expandafter\renewcommand\csname nprt@sign@-\endcsname{%
\color{red}{\ensuremath{-}}}
\makeatother
AndréC
  • 24,137

2 Answers2

5

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 =).

Werner
  • 603,163
5

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

egreg
  • 1,121,712
  • Why is tabularx not appropriate here? However, it allows to obtain columns of the same width, which is necessary here. – AndréC Dec 27 '18 at 18:35
  • @AndréC Aren't the column the same width here? – egreg Dec 27 '18 at 18:37
  • Yes, they are, but generally with tabular, they adapt. Why are they the same width here with tabular? – AndréC Dec 27 '18 at 18:40
  • 1
    @AndréC I used w{c}{1em}, which is exactly how to make a 1em wide column with center alignment; (requires array). The comma is “hidden” by \rlap. – egreg Dec 27 '18 at 18:50
  • 1
    @AndréC In this case, tabularx is not the right tool because you don't know the target width; if you have several similar tables to this one, the width of the digit columns should be the same across the document – egreg Jan 03 '19 at 10:56
  • I am desperately looking for documentation on this w{}{} specificator, do you know where I can find it? – AndréC Apr 16 '21 at 06:47
  • @AndréC texdoc array – egreg Apr 16 '21 at 06:48
  • Many thanks :-) – AndréC Apr 16 '21 at 07:43