我想对大量的各个组进行不同的着色。例如
111222333444.555
我希望 111 为红色,222 为绿色,333 为蓝色,444.555 为黑色。
到目前为止,我用的是这个:
\newcommand{ \byte}[1]{{\textcolor[rgb]{0.0,0.0,0.0}{#1}}}
\newcommand{\kilobyte}[1]{{\textcolor[rgb]{0.0,0.0,0.5}{#1}}}
\newcommand{\megabyte}[1]{{\textcolor[rgb]{0.0,0.0,1.0}{#1}}}
\newcommand{\gigabyte}[1]{{\textcolor[rgb]{0.7,0.0,0.0}{#1}}}
\newcommand{\BBx}[1]{{\byte{#1}}}
\newcommand{\KBx}[2]{{\kilobyte{#1},\BBx{#2}}}
\newcommand{\MBx}[3]{{\megabyte{#1},\KBx{#2}{#3}}}
\def\BBrelax#1\relax{\BBx{#1}}
\def\KBrelax#1,#2\relax{\KBx{#1}{#2}}
\def\MBrelax#1,#2,#3\relax{\MBx{#1}{#2}{#3}}
\def\BB#1{\BBrelax#1\relax}
\def\KB#1{\KBrelax#1\relax}
\def\MB#1{\MBrelax#1\relax}
我会写
\MB{111,222,333.444} blah blah blah \KB{111,222}
但这有一个缺点,我自己必须确定数字有多大,并根据位数使用正确的命令,并且我必须手动用 , 分隔它们。 但是,现在我自动加载这些数据(csvreader),所以我需要一个命令来自动完成所有这些操作。
基本上我搜索一个命令实现,我可以说
\mynumber{111222333444.666, red, green, blue, black}
\mynumber{111222.333, red, green, blue, black}
它对我来说是正确的事情(包括小数点在内的最后3位数字是黑色,千位是蓝色,百万位是绿色,十亿位是红色)有一个上限就可以了(例如处理组的最大数量),但它必须能够处理大数和小数。
我尝试过的:我尝试使用\num 和\sepnum 命令,两者似乎都无法执行我想要的操作。我尝试自己用 fp 和一些巧妙的计算来实现它,但为此我需要浮点模和整数除法,这两者都不被该包支持。
有什么想法吗?提前致谢。
编辑: 我想这样使用它:
\documentclass{article}
\usepackage{csvsimple} % for csv reader
\RequirePackage{xcolor}
\begin{document}
\myno{.555}
\myno{111222333444.555}
\myno{111222333444}
\myno{444555666777888999.12345}
\begin{figure}[htp!]
\centering
\csvreader[tabular=r r, table head=Hello & World\\]{test.csv}{Foo=\foo,Bar=\bar}{\myno{\foo} & \myno{\bar}}
\caption{Baz}
\label{figure:baz}
\end{figure}
\end{document}
使用 csv 文件 (test.csv):
Foo,Bar
1,2
222,333
222333.444,11122233344555
最佳答案
请为您查找包含宏的示例 TeX 文件:
\documentclass{article}
\RequirePackage{xcolor}
\begin{document}
%111222333444.555
%I want
%111 to be red,
%222 to be green,
%333 to be blue, and
%444.555 to be black.
\makeatletter
% Two counters, one for each command and another for each digits.
\newcounter{myno}
\newcounter{mynod}
% Its nothing, but just to have \@nil command to parse the string.
\let\@nil\relax
% \myno will take the number and add .@@ after the number so that
% even if you have given a number without decimal it won't fail
\def\myno#1{%
\expandafter\myNo#1.@@\@nil%
}
% Storing the default value if there is no decimal value
\def\parse@@{@@}
% Defining \myNo which contains either 000.00 or 000.@@ as argument
\expandafter\def\expandafter\myNo#1.#2\@nil{\def\myNo@two{#2}%
% the counter of command is incremented
\stepcounter{myno}%
% the counter of digit is reset to 0
\setcounter{mynod}{0}%
% iterating through each digits and storing it in a control sequence
% also note that here #1 will be digits before decimal
\@tfor\xx:=#1\do{\stepcounter{mynod}%
\expandafter\xdef\csname myno@\themyno @\themynod\endcsname{\xx}%
}%
% setting the total digits defined to a temp counter a and setting another temp counter b to 0
\@tempcnta=\themynod%
\@tempcntb=0%
% make a loop if tempcnta>0
\loop\ifnum\@tempcnta>0%
\advance\@tempcntb by 1%
\xdef\myCtr{\the\@tempcnta}%
\xdef\myCtrb{\the\@tempcntb}%
% setting the right colour at right position
\textcolor{%
\ifnum\myCtr<4 black\else%
\ifnum\myCtr<7 blue\else%
\ifnum\myCtr<10 green\else%
\ifnum\myCtr<13 red\else%
cyan\fi\fi\fi\fi%
% putting the digit which was already stored
}{\csname myno@\themyno @\myCtrb\endcsname}%
\advance\@tempcnta by -1%
\repeat%
% if #2=@@ then there is no decimal stuff otherwise just strip .@@ which was already added in main macro
\ifx\myNo@two\parse@@\def\decimal{}\else%
\expandafter\myDec#2\@nil%
\fi%
% print decimal in black colour
\textcolor{black}{\decimal}%
}
% strip .@@ if it is there
\def\myDec#1.#2\@nil{\def\decimal{.#1}}
\makeatother
\myno{.555}
\myno{111222333444.555}
\myno{111222333444}
\myno{444555666777888999.12345}
\end{document}
关于formatting - 如何在 LaTeX 中为大数组着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38078164/