我想以不同的方式为大数字的各个组着色。例如
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
答案 0 :(得分:2)
请为您找到带有宏的示例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}