5

Edit: This question is related to this one: How to change only the color of numbers (and not units) with numprint?

I tried the solution given here, but it enlarges the line spacing of the table as shown in the second example in the screenshot below.

capture

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

\begin{document}

Tabularx table \textbf{without} any color: \textbf{the spacing is normal.}


\begin{tabularx}{5cm}{|*{11}{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}
\bigskip

Tabularx table \textbf{with} colors: \textbf{the line spacing is enlarged.}

\begin{tabularx}{5cm}{|*{11}{>{\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}
\bigskip


Table with individually placed colours. \textbf{No problem at all.}

\begin{tabularx}{5cm}{|*{11}{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
&&&\textcolor{blue}{4,}&&&& \\
&&&\textcolor{blue}{4}&\textcolor{blue}{0}&\textcolor{blue}{0}&\textcolor{blue}{0}& \textcolor{blue}{0},\\
&\textcolor{blue}{0,}&\textcolor{blue}{0}&\textcolor{blue}{4}&&&& \\
\hline
\end{tabularx}

\end{document}

How to color the text without coloring the rest of the table?

  • Excuse me very much. Why you use \rm in mathmode? Very beautiful your hat :) – Sebastiano Dec 27 '18 at 12:17
  • Actually, I don't remember why, it's an old document and I'm trying to change the color of the text. – AndréC Dec 27 '18 at 12:19
  • 2
    >{\leavevmode\color{blue}}X|} instead of >{\color{blue}}X|} – moewe Dec 27 '18 at 12:22
  • @moewe great, Can you turn this comment into a answer? – AndréC Dec 27 '18 at 12:23
  • 1
    @moewe Yes, you're right. – AndréC Dec 27 '18 at 12:27
3

As explained in Color changes cell height in tabular and Xcolor breaks tabular cell alignment when using the p option the \color command can be used between paragraphs and therefore does not switch the mode TeX is in. That means that in the table the \colour command is added to the vertical list and the colour actually starts 'before' the paragraph containing the contents of the cell and leaves a bit of vertical space.

If you add \leavevmode before the \color command you force TeX out of vertical mode and cause the colour to start at the beginning of the paragraph for the cell content. No vertical space is inserted because the vertical list is empty.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularx}
\usepackage{siunitx}

\begin{document}
Tabularx table \textbf{with} colors and \texttt{\string\leavevmode}: \textbf{normal again.}

\begin{tabularx}{5cm}{|*{11}{>{\leavevmode\color{blue}}X|}}
\hline
\multicolumn{2}{|c|}{\si{\metre\squared}} & 
\multicolumn{2}{c|}{\si{\deci\metre\squared}} & 
\multicolumn{2}{c|}{\si{\centi\metre\squared}} & 
\multicolumn{2}{c|}{\si{\milli\metre\squared}} \\
\hline
&&&4,&&&& \\
&&&4&0&0&0& 0,\\
&0,&0&4&&&& \\
\hline
\end{tabularx}
\end{document}

Like Christian Hupfer in his answer I recommend using siunitx at least for the units (if only to get rid of the two-letter font command \rm, see Will two-letter font style commands (\bf , \it , …) ever be resurrected in LaTeX?).

6

A preliminary solution, using siunitx and a regular tabular instead of tabularx and applying the S - columntype.

The S - columntype has the advantage of aligning decimal numbers as well as using the appropriate number formatting.

The number-color= option colorizes the numbers only, in order to prevent powers of units to be colored as well, use power-font=unit and in order to keep the comma as decimal marker, use copy-decimal-marker.

Please note that |, i.e. vertical lines are arguable with respect to design and readability of tables, but I kept them inside.

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

\usepackage{siunitx}

\sisetup{number-color=blue,power-font=unit,copy-decimal-marker}

\begin{document}

Tabularx table \textbf{without} any color: \textbf{the spacing is normal.}


\begin{tabular}{|*{8}{S|}}
\hline
\multicolumn{2}{|c|}{\si{\metre\squared}} & 
\multicolumn{2}{c|}{\si{\deci\metre\squared}} & 
\multicolumn{2}{c|}{\si{\centi\metre\squared}} & 
\multicolumn{2}{c|}{\si{\milli\metre\squared}} \tabularnewline
\hline
&&&4,&&&& \tabularnewline
&&&4&0&0&0& 0,\tabularnewline
&0&0&4&&&& \tabularnewline
\hline
\end{tabular}
\bigskip
\end{document}

enter image description here

  • Side note: the small 'debris' above the table are cut-outs from the screen-shot. They have nothing to to with the table itself – user31729 Dec 27 '18 at 17:12
  • Is it possible not to put 0 after the comma?0, and not 0,0. Children are taught to write only one number per column. The comma is only used to teach children that it is the unit marker. – AndréC Dec 27 '18 at 17:20
  • 2
    @AndréC: Perhaps it is possible, but that is bad style. I am a teacher as well and you should not teach children such styles! – user31729 Dec 27 '18 at 17:29
  • In a table I only allow one number per column. Putting two is a problem. The comma is only placed for educational purposes to teach them how to use a conversion table. The rest of the time, they are not allowed to put a comma without numbers after it. – AndréC Dec 27 '18 at 17:34

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.