![在 LaTeX 文档中为所有元音添加不同的颜色](https://linux22.com/image/277017/%E5%9C%A8%20LaTeX%20%E6%96%87%E6%A1%A3%E4%B8%AD%E4%B8%BA%E6%89%80%E6%9C%89%E5%85%83%E9%9F%B3%E6%B7%BB%E5%8A%A0%E4%B8%8D%E5%90%8C%E7%9A%84%E9%A2%9C%E8%89%B2.png)
是否可以为一段文本中的字母 a、e、i、o、u、ya 赋予不同的颜色?例如,将所有字母 a 染成红色,字母 e 染成蓝色,字母 i 染成绿色,等等。
编辑: 当然无需手动操作。
答案1
需要 xelatex:
\documentclass{article}
\usepackage{color}
\XeTeXinterchartokenstate = 1
\newXeTeXintercharclass \vowelsclass
\XeTeXcharclass `\a \vowelsclass
\XeTeXcharclass `\e \vowelsclass
\XeTeXcharclass `\i \vowelsclass
\XeTeXcharclass `\o \vowelsclass
\XeTeXcharclass `\u \vowelsclass
\XeTeXcharclass `\A \vowelsclass
\XeTeXcharclass `\E \vowelsclass
\XeTeXcharclass `\I \vowelsclass
\XeTeXcharclass `\O \vowelsclass
\XeTeXcharclass `\U \vowelsclass
\XeTeXinterchartoks 0 \vowelsclass = {\bgroup\color{blue}}
\XeTeXinterchartoks 1 \vowelsclass = {\bgroup\color{blue}}
\XeTeXinterchartoks 2 \vowelsclass = {\bgroup\color{blue}}
\XeTeXinterchartoks 3 \vowelsclass = {\bgroup\color{blue}}
\XeTeXinterchartoks 4095 \vowelsclass = {\bgroup\color{blue}}
\XeTeXinterchartoks \vowelsclass 0 = {\egroup}
\XeTeXinterchartoks \vowelsclass 1 = {\egroup}
\XeTeXinterchartoks \vowelsclass 2 = {\egroup}
\XeTeXinterchartoks \vowelsclass 4095 = {\egroup}
\begin{document}
One two three four five six seven eight.
\end{document}
请注意,单词边界类目前的数量为 4095(自 2016 年起),最初为 255
答案2
这只是为了练习使用 LaTeX3 代码。
\documentclass{article}
\usepackage{xparse,xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\colorize}{mm}
{
\cs_set:cpn { maryjane_color_#1: } { \textcolor{#2}{#1} }
}
\tl_new:N \l_maryjane_text_tl
\NewDocumentCommand{\changecolors}{ O{aeiou} m }
{
\tl_set:Nn \l_maryjane_text_tl { #2 }
\tl_map_inline:nn { #1 }
{
\tl_replace_all:Nnn \l_maryjane_text_tl { ##1 } { \use:c { maryjane_color_##1: } }
}
\l_maryjane_text_tl
}
\ExplSyntaxOff
\colorize{a}{red}
\colorize{e}{blue}
\colorize{i}{green}
\colorize{o}{yellow}
\colorize{u}{blue!30}
\begin{document}
\changecolors{The quick brown fox jumped over the lazy dog}
\changecolors[a]{The quick brown fox jumped over the lazy dog}
\end{document}
在可选参数中,您可以指定已声明字母的子集。如果您指定未声明的字母(带有\colorize
),它将被吞噬。
答案3
luatex
问题的解决方案:
\RequirePackage{luatex85}
\documentclass[varwidth]{standalone}
\usepackage{luacode}
\begin{luacode}
vowels = {'a', 'e', 'i', 'o', 'u', 'y',
'A', 'E', 'I', 'O', 'U', 'Y'}
vowels_char = {}
for _, value in pairs(vowels) do
vowels_char[#vowels_char + 1] = string.byte(value)
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function color_vowels(head)
for n in node.traverse_id(node.id("glyph"), head) do
if table.contains(vowels_char, n.char) then
local n1, n2
n1 = node.new(node.id("whatsit"), node.subtype("pdf_literal"))
n2 = node.new(node.id("whatsit"), node.subtype("pdf_literal"))
n1.data = "q 1 0 0 rg"
n1.mode = 1
node.insert_before(head, n, n1)
n2.data = " Q"
n2.mode = 1
node.insert_after(head, n, n2)
end
end
return head
end
luatexbase.add_to_callback("pre_linebreak_filter", color_vowels, "Color vowels")
luatexbase.add_to_callback("hpack_filter", color_vowels, "Color vowels")
\end{luacode}
\begin{document}
The quick brown fox jumped Over the lazy dog
\hbox{The quick brown fox jumped Over the lazy dog}
$a^2$
\end{document}
关于代码的一些注释。string.byte
返回其参数的 ASCII 值。颜色变化是在pdf
“whatits”pdf_literal
节点的级别上完成的(q
是一种开始组和Q
一种结束组pdf
,rg
是非rgb
描边操作的模型)。
最后说明一下:我的方法不适用于内联数学。