在LaTeX中使用颜色 Using colours in LaTeX

Using colours in LaTeX

There are several elements in LATEX whose colour can be changed to improve the appearance of the document. Colours can be manually defined to a desired tone using several models, this article explains how.

Contents

Introduction

The simplest manner to use colours in your LATEX document is by importing the package color or xcolor. Both packages provide a common set of commands for colour manipulation, but the latter is more flexible and supports a larger number of colour models. Below an example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
 
\usepackage{color}
 
\begin{document}
 
This example shows different examples on how to use the \texttt{color} 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}

Note: In all the examples the package xcolor can be used instead of color

In this example, the package color is imported with

\usepackage{color}

then the command \color{blue} sets the blue colour for the current block of text. In this case for theitemize environment.

The colour of a second block of text, delimited by { and }, is set to red with the command \color{red}, then a 0.5mm-thick horizontal ruler is inserted by \rule{\linewidth}{0.5mm}.

The amount of available colour names depends on the driver, usually the next colours can be used with any driver: white, black, yellow, green, blue, purple cyan and magenta.

See the reference guide for more colours supported by other drivers.

Open an example of the color package in ShareLaTeX

Basic usage

The colour system provided by the packages color and xcolor is built around the idea of colour models, the colour mode and the colour names supported by a driver vary.

The model based on colour names is very intuitive, even though the list of available names is limited, usually provides enough options. Below an example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
 
\usepackage[usenames, dvipsnames]{color}
 
\begin{document}
 
This example shows different examples on how to use the \texttt{color} package
to change the colour of elements in \LaTeX.
 
\begin{itemize}
\color{ForestGreen}
\item First item
\item Second item
\end{itemize}
 
\noindent
{\color{RubineRed} \rule{\linewidth}{0.5mm} }
 
The background colour of some text can also be \textcolor{red}{easily} set. For
instance, you can change to orange the background of \colorbox{BurntOrange}{this
text} and then continue typing.
 
\end{document}

There are a few changes in this example compared to the one presented in the introduction. First, the command to import the color package has two additional parameters:

  • usenames Makes the names in the corresponding driver name model available. This option can be omitted in xcolor.
  • dvipsnames Makes the colour names for the driver dvips available, if the package color is imported, this option must be used in conjunction with usenames. From this new set of colour names, the example uses: ForestGreenRubineRed and BurntOrange. See the reference guide for a complete list of possible colours.

Other possible drivers are: xdvi, dvipdf, pdftex, dvipsone, dviwin, emtex, truetex and xtex.

Two new commands are also presented in the example:

\textcolor{red}{easily}
Changes the colour of inline text. Takes two parameters, the colour to use and the text whose colour is changed. In the example the word easily is printed in red
\colorbox{BurntOrange}{this text}
Changes the background colour of the text passed as second parameter. In the example the words this text are printed in BurntOrange.

Open an example of the color package in ShareLaTeX

Creating your own colours

It is possible to define your own colours, the manner in which the colour is defined depends on the preferred model. Below an example using the 4 colour models typically supported by any driver.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
 
\usepackage[usenames, dvipsnames]{color}
 
\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}

The command \definecolor takes three parameters: the name of the new colour, the model, and the colour definition. Roughly speaking, each number represent how much of each colour you add to the mix that makes up the final colour.

  • rgb: Red, Green, Blue. Three comma-separated values between 0 and 1 define the components of the colour.
  • RGB: The same as rgb, but the numbers are integers between 0 and 255.
  • cmyk: Cyan, Magenta, Yellow and blacK. Comma-separated list of four numbers between 0 and 1 that determine the colour according to the additive model used in most printers.
  • gray: Grey scale. A single number between 0 and 1.

In the example, mypink1mypink2 and mypink3 define the same colour but for different models. You can actually see that the one defined by cmyk is slightly different.

Colours defined by either model can later be used within your document not only to set the colour of the text, but for any other element that takes a colour as parameter, for instance tables (you must add the parameter table to the preamble), graphic elements created with TikZplotsvertical rulers in multicolumn documents and code listings.

Open an example of the color package in ShareLaTeX

xcolor-only colour models

There are some additional commands that are only available with the package xcolor, these enable support for more colour models and friendly colour mixing.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
 
\usepackage[dvipsnames]{xcolor}
\colorlet{LightRubineRed}{RubineRed!70!}
\colorlet{Mycolor1}{green!10!orange!90!}
\definecolor{Mycolor2}{HTML}{00F9DE}
 
\begin{document}
 
This document present several examples on how to use the \texttt{color} package
to change the colour of elements in \LaTeX.
 
\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} }

Three new colours are defined in this example, each one in a different manner.

\colorlet{LightRubineRed}{RubineRed!70!}
A new colour named LightRubineRed is created, this colour has 70% the intensity of the original RubineRedcolour. You can think of it as a mixture of 70% RubineRed and 30% white. Defining colours in this way is great to obtain different tones of a main colour, common practice in corporate brands. In the example, you can see the original RubineRed and the new LightRubineRed used in two consecutive horizontal rulers.
\colorlet{Mycolor1}{green!10!orange!90!}
A colour named Mycolor1 is created with 10% green and 90%orange. You can use any number of colours to create new ones with this syntax.
\definecolor{Mycolor2}{HTML}{00F9DE}
The colour Mycolor2 is created using the HTML model. Colours in this model must be created with 6 hexadecimal digits, the characters A,B,C,D,E and F must be upper-case.

The colour models that only xcolor support are:

  • cmy cyan, magenta, yellow
  • hsb hue, saturation, brightness
  • HTML RRGGBB
  • Gray Grey scale, a number between 1 and 15.
  • wave Wave length. Between 363 and 814.

Open an example of the xcolor package in ShareLaTeX

Setting the page background colour

The background colour of the entire page can be easily changed with \pagecolor. See the next example:

\pagecolor{black}
\color{white}

The command \pagecolor{black} set the page colour to black. This is a switch command, meaning it will take effect in the entire document unless another switch command is used to revert it. \nopagecolor will change the background back to normal.

Open an example of the color package in ShareLaTeX

Reference guide

Basic colour names available in LATEX

white, black, red, green, blue, cyan, magenta, yellow

Colour names available with the dvipsnames option

Other drivers have more colour names available, links to documentations in the further reading section.

Open an example of the xcolor package in ShareLaTeX

from: https://www.sharelatex.com/learn/Using_colours_in_LaTeX

时间: 2024-10-12 17:09:44

在LaTeX中使用颜色 Using colours in LaTeX的相关文章

Latex中插入矢量图片(带latex公式)

情景描述:在latex文件中,需要插入一张图片,图片内容包括几个圆圈.几条线和一些特殊的数学公式. 方案: 1.使用visio作图,然后插入数学公式.问题:导出图片的话,不是矢量,无法放大:导出pdf,进行裁剪,但是,word里的公式不是很美观,可能与latex的公式不兼容. 2.使用Inkscape,制作矢量图,在图上可以输入latex格式的公式,然后使用input将文件导入. 第2个方案保持了一致性和灵活性,因此,采用之. inkscape图片: latex源代码: 最终效果图:

python在linux中输出带颜色的文字的方法

在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信息. 我们可以通过对有用的信息设置不同颜色来达到醒目的效果,因为我平时都是在linux下开发,而linux终端中的颜色是用转义序列控制的,转义序列是以ESC开头,可以用\033完成相同的工作(ESC的ASCII码用十进制表示就是27,等于用八进制表示的33). 书写格式,和相关说明如下: 格式:\0

android中的颜色设置

1.在android中经常看到设置的颜色为八位的十六进制的颜色值,例如: 1 2 3 public static final class color {     public static final int lightblue=0x7f040000; } 或者在Java中tx.setTextColor(0xffff00f); 说明: 0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x表示颜色整数的标记,ff表示透明度,f00f表示色值,注意:0x后面ffff00ff

The usage method of reference with bibtex in Latex【latex中参考文献的使用方法】

如何在latex中以Bibtex文件方式引用参考文献? 以IEEEtran模板为例: 1.制作bibtex参考文献库.方法如下: ①建立myreference.bib文件: ②在Google scholar上找到某一篇文献的bibtex格式,如下图: ③复制该内容到myreference.bib文件中,类似的操作,直至制作好参考文献库. 2.在需要生成参考文献的地方插入“引用样式”和“文件名称”的代码 \bibliographystyle{IEEEtran} \bibliography{myre

python在linux中输出带颜色文字

为了使输出更美观.醒目,有些时候需要在python输出字符上加一些颜色.很简单的分享下方法: 格式:\033[显示方式;前景色;背景色m 前景色            背景色           颜色 --------------------------------------- 30                40              黑色 31                41              红色 32                42              绿

开放中常用颜色代码

顏色 英文名稱 RGB 16色 顏色 英文名稱 RGB 16色 Snow 255 250 250 #FFFAFA PaleTurquoise1 187 255 255 #BBFFFF GhostWhite 248 248 255 #F8F8FF PaleTurquoise2 174 238 238 #AEEEEE WhiteSmoke 245 245 245 #F5F5F5 PaleTurquoise3 150 205 205 #96CDCD Gainsboro 220 220 220 #DCD

Latex使用:在latex中添加算法模块

在Miktex下有三个latex algorithm包,分别为:algorithm,algorithmic,algorithm2e三个,其中algorithm,algorithmic经常成套使用: latex中algorithm模板为: latex文件中顶端加入的package: \usepackage{algorithm} \usepackage{algorithmic} \usepackage{setspace} 算法块代码: \begin{algorithm}[htb] \setstret

如何在latex 中插入EPS格式图片

如何在latex 中插入EPS格式图片 第一步:生成.eps格式的图片 1.利用visio画图,另存为pdf格式的图片 利用Adobe Acrobat裁边,使图片大小合适 另存为.eps格式,如下图所示: 注:这一步必须按照图示方式另存为,不能直接强行改后缀名,否则插入后无法显示. 2.利用matlab画图,直接另存为eps格式即可 第二步:插入代码 1.添加宏包   \usepackage{graphicx}     \usepackage{epstopdf} 2.将.eps图片和.tex文件

Latex中插入C语言代码

Latex是一个文本排版的语言,能排版出各种我们想要的效果.而且用代码排版的优点是易于修改板式,因此在文本内容的排版时,Latex应用十分广泛. 当我们需要在Latex中插入代码时,就需要用到 \usepackage{listings} 宏包.例如插入一个简单的C语言代码 #include <stdio.h> int main(int argc, char ** argv) { printf("Hello, world!\n"); return 0; } 要将上面 Hell