LaTeX 颜色

article/2025/9/25 16:34:32

LaTeX 颜色

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

文章目录

  • LaTeX 颜色
  • 介绍
  • `xcolor`中所支持的预设颜色
    • 例子
  • 使用`color`包来加载预设颜色
  • 旧版本TeX情况下的颜色使用
  • 创建你自己的新颜色
    • `xcolor`专属的颜色模式
  • 设置页面的背景颜色

介绍

本文介绍如何使用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}

在这里插入图片描述
我们首先导入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包来加载预设颜色,它支持usenamesdvipsnames两个参数:

\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}

在这里插入图片描述

旧版本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}

在这里插入图片描述
命令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}

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

  • \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}

在这里插入图片描述

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


http://chatgpt.dhexx.cn/article/4uMl2PpW.shtml

相关文章

JQuery弹出窗口小插件ColorBox

JQuery弹出窗口小插件ColorBox 今天在博客园看到一篇《ASP.NET MVCColorbox做的一个Demo(一)》的文章。看了效果,给人很舒服的感觉。立马我也照着他的示例做了。但做了很久没有成功。后来我到官方下载了ColorBox,里面附带了示例。看了一会儿,…

Python 自定义色带 colorbar

1. 自定义颜色系 import matplotlib.pyplot as plt import matplotlib as mplfig, ax plt.subplots(figsize(6, 1)) fig.subplots_adjust(bottom0.5)cmap mpl.cm.viridis norm mpl.colors.Normalize(vmin5, vmax10)fig.colorbar(mpl.cm.ScalarMappable(normnorm, cmapcmap)…

关于ColorBox使用的一个简单例子

colorbox是一个很小的jquery插件,通过调用jquery.colorbox.js文件,我们可以用它来实现页面大图显示的功能。 脚本如下: 这里我们需要指出的几个注意事项: 1.必须先调用jquery.js文件,否则$符号没有办法被识别。 2. 项…

colorUI使用

colorUI使用 一、colorUI简介 官网地址—还在完善 colorUI特点: 组件精美,色彩十分鲜艳封装特特特特别的好,每一个样式都是单独的class选择器,都是封装极好的css小组件兼容性,扩展性极好。亲测在vant weep组件上可…

使用ColorUI组件

项目初始化请参照:使用ColorUI构建小程序项目_LookOutThe的博客-CSDN博客 目录 一、开发准备工作 1.在前端开发工具中打开下载好的demo目录 2.浏览器打开demo 3.微信开发工具打开上一篇构建好的项目 二、开始使用 1.找需要使用的组件 2.复制我们要使用的那段…

jQuery弹窗组件 colorbox

公司开发微信端的wap网站,因为微信浏览器的限制,对很多jquery组件支持的都不是很好,弹窗总是有这样那样的问题,试验了好几个之后,最后使用了colorbox这款jquery插件 github:https://github.com/jackmoore/…

postgresql服务启动报错CST FATAL: could not create lock file “/tmp/.s.PGSQL.5432.lock“: 权限不够

背景:服务器断电后服务无法启动 问题描述:重启postgresql数据库服务报错 CST FATAL: could not create lock file “/tmp/.s.PGSQL.5432.lock”: 权限不够 故障原因: 权限不足 解决方案: 修改tmp权限 chown -R postgres:postgr…

psql: error: connection to server on socket “/tmp/.s.PGSQL.5432“ failed: No such file or directory

当我许久不用postgresl之后,突然有一天需要连接使用这个数据库 当我连接时,出现了以下的错误 因为我是mac os系统,我用brew进行postgresql的卸载安装和更新均不起作用, 后续又查看是否环境变量配置或者是端口占用等问题也是没有成…

CM 登入出現Unable to acquire JDBC Connection 打開hue 出現 TCP/IP connections on port 5432

场景: CM 登入出現Unable to acquire JDBC Connection。 打開hue 出現 TCP/IP connections on port 5432 conn _connect(dsn, connection_factoryconnection_factory, **kwasync) OperationalError: could not connect to server: Connection refusedIs the ser…

【故障排查】harbor核心服务不可用(pgsql.conf permission denied)(failed to connect to tcp://postgresql:5432)

问题描述: harbor核心服务不可用 jobserver报错 docker ps (发现core和jobserver 重启了) core报错: 访问pg.conf permission denied 处理: [rootnode4 ~]# cd /data/database/ [rootnode4 database]# ls pg13…

postgres connection to server at “localhost“ (::1), port 5432 failed: Connection refused

Problem 今天打开pgAdmin4的时候发现总是报错,显示如下: 即便输入密码,还是不停弹出这个界面 Solution 在电脑开始键旁边的搜索框搜索"service"或者“服务”,并找到"postgresql-x64" 双击显示如下界面 …

postgresql 14 服务器打不开的问题(5432服务器端口失败)

问题描述:(*注 服务器端口默认5432 ,我的端口号是安装时自己改的) *注意 : 安装到语言选项时 选择 C 而不是默认 安装时的警告:postsql安装后步骤出现问题,安装可能未正确完成,启动数据库服务失败 解决…

PostgreSQL Unable to connect to server: XXX port 5432 failed: Connection refused

一、问题说明 使用 pgAdmin 连接本地的 PostgreSQL 时,报错“Unable to connect to server: XXX port 5432 failed: Connection refused”。 截图如下: 二、问题原因 通过报错提示信息可以看出:客户端工具访问被拒绝的原因是&#xff0…

postgresql .s.PGSQL.5432 文件报错

执行命令 创建软连接 sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432

因为计算机中丢失ssce5432.dll,ssce5432.dll 64位

ssce5432.dll 64位是sentry spelling-checker engine软件运行必不可少的文件,缺少这款文件将导致软件的部分功能无法实现,系统提示“ssce5432.dll找不到”或者“ssce5432.dll错误”等信息,本站提供ssce5432.dll 64位下载,支持win7…

关于谷歌浏览器不能打开页面问题的解决

关于谷歌浏览器不能打开页面问题的解决 谷歌访问任何地址出现以下错误 打不开页面 解决方法如下 1、点击浏览器右上角的三个点选择设置 拉到最下面找到高级 选择系统 点击 打开代理设置 2、选局域网设置 3、把框中的√取消 然后点击确定即可 重新刷新页面问题解决

解决google浏览器不能访问本地磁盘图片资源问题 or 配置Tomcat访问本地磁盘图片资源

解决google浏览器不能访问本地磁盘图片资源问题: Not allowed to load local resource 1.修改Tomcat中的conf/server.xml文件 其中在 添加 说明 path:为自己图片所在文件夹; docBase:为自己图片所在本地磁盘路径; 2.数据库中路径设置问题…

谷歌地图打不开怎么办?

谷歌地图打不开怎么办? 方法一:使用在线版google地图,点击下方链接进入 天天看地图 https://www.tiantianditu.com/3d.html 方法二,使用奥维地图,打开google图层 打开google图层需要3个步骤,1,下载奥…

127.0.0.1可以访问,localhost不能访问的问题

二者概念 localhost:本地服务器127.0.0.1:本机地址(本机服务器) 二者区别 localhot:是不经网卡传输的,它不受网络防火墙和网卡相关的的限制。127.0.0.1:是通过网卡传输的,它依赖网卡,并受到网…

Google浏览器跨域不能设置cookie问题

在前后端分离的项目中Google浏览器中不能设置cookie,因为在Google浏览器80版本后增加了SameSite的cookie限制,默认为Lax模式不携带cookie和session。 解决这一问题的方法就是在正确配置springboot和vue的跨域配置的前提下做本地的域名映射,将…