赞
踩
原 文:Using colours in LaTeX
译 者:Xovee
翻译时间:2022年10月17日
本文介绍如何使用color
和xcolor
包来在LaTeX文档中使用颜色。
这两个包都提供了许多有关颜色的命令,相比之下,xcolor
的功能更强大,所以我们推荐你使用xcolor
。
我们首先来看一个例子:
\documentclass{article}
\usepackage{xcolor}
\begin{document}
This example shows some instances of using the \texttt{xcolor} package
to change the colour of elements in \LaTeX.
\begin{itemize}
\color{blue}
\item First item
\item Second item
\end{itemize}
\noindent
{\color{red} \rule{\linewidth}{0.5mm}}
\end{document}
我们首先导入xcolor
包
\usepackage{xcolor}
命令\color{blue}
设置了当前区域的文字颜色为蓝色。在这里例子中,颜色生效的区域为itemize
环境。
最下面的红色的水平线是红色的,我们使用分隔符{ }
来将命令的生效范围限定在大括号之中。
xcolor
中所支持的预设颜色如xcolor
文档中所述,下面列出的颜色是不需要引入任何包就可以使用的:
你也可以使用xcolor
包来获得更多预设的颜色,在引入包的时候使用下面的参数:
dvipsnames
:支持68种颜色(CMYK)svgnames
:支持151种颜色(RGB)xllnames
:支持317种颜色(RGB)例如:
\usepackage[dvipsnames]{xcolor}
你就可以使用下面这些颜色:
阅读xcolor
包的文档来获取其他参数所支持的颜色。
下面的例子使用了dvipsnames
:
\documentclass{article} \usepackage[dvipsnames]{xcolor} \begin{document} This example shows how to use the \texttt{xcolor} package to change the colour of \LaTeX{} page elements. \begin{itemize} \color{ForestGreen} \item First item \item Second item \end{itemize} \noindent {\color{RubineRed} \rule{\linewidth}{0.5mm}} The background colour of text can also be \textcolor{red}{easily} set. For instance, you can change use an \colorbox{BurntOrange}{orange background} and then continue typing. \end{document}
例子中介绍了两个新的命令:
\textcolor{red}{easily}
:第一个参数是颜色的名字,第二个参数是要改变颜色的文字内容。\colorbox{BurntOrange}{orange background}
:第一个参数是文字背景颜色,第二个参数是要改变背景颜色的文字内容。color
包来加载预设颜色你也可以使用color
包来加载预设颜色,它支持usenames
和dvipsnames
两个参数:
\usepackage[usenames,dvipsnames]{color}
下面的代码使用了color
包来加载了我们在上个例子中使用过的颜色:
\documentclass{article} \usepackage[usenames,dvipsnames]{color} %using the color package, not xcolor \begin{document} This example shows how to use the \texttt{\bfseries color} package to change the colour of \LaTeX{} page elements. \begin{itemize} \color{ForestGreen} \item First item \item Second item \end{itemize} \noindent {\color{RubineRed} \rule{\linewidth}{0.5mm}} The background colour of text can also be \textcolor{red}{easily} set. For instance, you can change use an \colorbox{BurntOrange}{orange background} and then continue typing. \end{document}
你可以创建自定义的颜色。下面的例子定义了四个新颜色:
\documentclass{article} \usepackage[dvipsnames]{xcolor} \definecolor{mypink1}{rgb}{0.858, 0.188, 0.478} \definecolor{mypink2}{RGB}{219, 48, 122} \definecolor{mypink3}{cmyk}{0, 0.7808, 0.4429, 0.1412} \definecolor{mygray}{gray}{0.6} \begin{document} User-defined colours with different colour models: \begin{enumerate} \item \textcolor{mypink1}{Pink with rgb} \item \textcolor{mypink2}{Pink with RGB} \item \textcolor{mypink3}{Pink with cmyk} \item \textcolor{mygray}{Gray with gray} \end{enumerate} \end{document}
命令definecolor
接收三个参数:新颜色的名字,颜色模式,以及颜色的值。
rgb
:红色、绿色、蓝色。你可以输入三个从0到1的值,每个值分别代表对应的颜色。RGB
:与rgb
相似,不过值的范围是0到255。cmyk
:与RGB的红绿蓝不同,这次定义的是Cyan、Magenta、黄色和黑色。值位于0到1。CMYK
模式通常用于商业打印机gray
:灰度颜色,单个位于0到1 。在这个例子中,mypink1
、mypink2
和mypink3
从不同的模式定义了相同的颜色。你会发现cmyk
模型的颜色会有一些不同。
定义好的颜色名字可以在随后的文档中进行使用,不仅仅是文字,还可以使用在表格(将table
参数传递给xcolor
),TikZ
、垂直线和代码高亮等环境中使用颜色。
xcolor
专属的颜色模式xcolor
包还提供了额外的命令来支持更多的颜色模式以及友好的颜色混合方法:
\documentclass{article} \usepackage[dvipsnames]{xcolor} \colorlet{LightRubineRed}{RubineRed!70} \colorlet{Mycolor1}{green!10!orange} \definecolor{Mycolor2}{HTML}{00F9DE} \begin{document} This document presents several examples showing how to use the \texttt{xcolor} package to change the colour of \LaTeX{} page elements. \begin{itemize} \item \textcolor{Mycolor1}{First item} \item \textcolor{Mycolor2}{Second item} \end{itemize} \noindent {\color{LightRubineRed} \rule{\linewidth}{1mm}} \noindent {\color{RubineRed} \rule{\linewidth}{1mm}} \end{document}
我们在这个例子中定义了三个新颜色:
\colorlet{LightRubineRed}{RubineRed!70}
:我们创建了一个新的颜色LightRubineRed
,它的颜色密度是原RubineRed
的70%。你可以把新颜色理解为70%的RubineRed
和30%的白色。这种方法经常被各大商业公司使用,可以让你从一种主颜色中创建许多相似的颜色。\colorlet{Mycolor1}{green!10!orange}
:创建了名为Mycolor1
的新颜色,它由10%的绿色和90%的橘色混合而成。\definecolor{Mycolor2}{HTML}{00F9DE}
:使用HTML
模式创建了名为Mycolor2
的新颜色。这种模式的输入是6个16进制的数字,每两个数字代表RGB中的一个(
16
×
16
=
256
16\times 16=256
16×16=256即为RGB中从0到155的颜色范围)。字母ABCDEF必须大写。xcolor
支持的专属颜色模式包括:
cmy
:
c
y
a
n
\textcolor{cyan}{cyan}
cyan、
m
a
g
e
n
t
a
\textcolor{magenta}{magenta}
magenta、
y
e
l
l
o
w
\textcolor{yellow}{yellow}
yellowhsb
:色调Hue、饱和度Saturation、亮度BrightnessHTML
:RRGGBB,红绿蓝Gray
:值为1到15的灰度wave
:波长,值的大小为从363到814。This is Xovee Xu . \textcolor{orange}{\text{This is Xovee Xu}.} This is Xovee Xu.
e = m c 2 \textcolor{purple}{e=mc^2} e=mc2
整个页面的背景颜色可以通过pagecolor
来修改:
\documentclass{article} \usepackage[dvipsnames]{xcolor} \colorlet{LightRubineRed}{RubineRed!70} \colorlet{Mycolor1}{green!10!orange} \definecolor{Mycolor2}{HTML}{00F9DE} \begin{document} \pagecolor{black} \color{white}% set the default colour to white This document presents several examples showing how to use the \texttt{xcolor} package to change the colour of \LaTeX{} page elements. \begin{itemize} \item \textcolor{Mycolor1}{First item} \item \textcolor{Mycolor2}{Second item} \end{itemize} \noindent {\color{LightRubineRed} \rule{\linewidth}{1mm}} \noindent {\color{RubineRed} \rule{\linewidth}{1mm}} \end{document}
命令\pagecolor{black}
设置了页面颜色为黑色。这是个转换命令,意味着整个文档的页面颜色都会变为黑色,除非你又使用了另一个转换命令。你可以使用\nopagecolor
来将页面的背景颜色恢复为正常。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。