当前位置:   article > 正文

LaTeX 颜色

latex 颜色

LaTeX 颜色

原  文:Using colours in LaTeX
译  者:Xovee
翻译时间:2022年10月17日

介绍

本文介绍如何使用colorxcolor包来在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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述
我们首先导入xcolor

\usepackage{xcolor}
  • 1

命令\color{blue}设置了当前区域的文字颜色为蓝色。在这里例子中,颜色生效的区域为itemize环境。

最下面的红色的水平线是红色的,我们使用分隔符{ }来将命令的生效范围限定在大括号之中。

xcolor中所支持的预设颜色

xcolor文档中所述,下面列出的颜色是不需要引入任何包就可以使用的:
在这里插入图片描述
你也可以使用xcolor包来获得更多预设的颜色,在引入包的时候使用下面的参数:

  • dvipsnames:支持68种颜色(CMYK)
  • svgnames:支持151种颜色(RGB)
  • xllnames:支持317种颜色(RGB)

例如:

\usepackage[dvipsnames]{xcolor}
  • 1

你就可以使用下面这些颜色:

在这里插入图片描述
阅读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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

例子中介绍了两个新的命令:

  • \textcolor{red}{easily}:第一个参数是颜色的名字,第二个参数是要改变颜色的文字内容。
  • \colorbox{BurntOrange}{orange background}:第一个参数是文字背景颜色,第二个参数是要改变背景颜色的文字内容。

使用color包来加载预设颜色

你也可以使用color包来加载预设颜色,它支持usenamesdvipsnames两个参数:

\usepackage[usenames,dvipsnames]{color}
  • 1

下面的代码使用了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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

旧版本TeX情况下的颜色使用

参考Drivers and color

创建你自己的新颜色

你可以创建自定义的颜色。下面的例子定义了四个新颜色:

\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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述
命令definecolor接收三个参数:新颜色的名字,颜色模式,以及颜色的值。

  • rgb:红色、绿色、蓝色。你可以输入三个从0到1的值,每个值分别代表对应的颜色。
  • RGB:与rgb相似,不过值的范围是0到255。
  • cmyk:与RGB的红绿蓝不同,这次定义的是Cyan、Magenta、黄色和黑色。值位于0到1。CMYK模式通常用于商业打印机
  • gray:灰度颜色,单个位于0到1 。

在这个例子中,mypink1mypink2mypink3从不同的模式定义了相同的颜色。你会发现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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述
我们在这个例子中定义了三个新颜色:

  • \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} yellow
  • hsb:色调Hue、饱和度Saturation、亮度Brightness
  • HTML: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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在这里插入图片描述

命令\pagecolor{black}设置了页面颜色为黑色。这是个转换命令,意味着整个文档的页面颜色都会变为黑色,除非你又使用了另一个转换命令。你可以使用\nopagecolor来将页面的背景颜色恢复为正常。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/521935
推荐阅读
相关标签
  

闽ICP备14008679号