这个问题与这个问题相关: 在 tabularx 环境中仅为文本着色
我想用蓝色(仅数字)写出一系列相等的数字,以便表格的每一行在其右侧都有一条相应的相等线。
\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}
在 numprint 手册中,有一个技巧可以让你为第 18-19 页的负数着色,我无法修改它以用蓝色写数字。
\makeatletter
\expandafter\renewcommand\csname nprt@sign@-\endcsname{%
\color{red}{\ensuremath{-}}}
\makeatother
答案1
您可以进行本地修补\numprint
以设置\color{black}
可选参数。
请注意,这tabularx
不是适合该工作的工具。
\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}
和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}
答案2
以下是使用单个tabular
以获得更好的垂直对齐的选项:
\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}
整个\numprint
-column 已设置\color{blue}
,每个单元也已设置\color{black}
(以及=
)。