在此 MWE 中
\documentclass{article}
\usepackage{xcolor}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\begin{document}
Why this is red \textcolor{red}{\cmark}
But not this one? {\color{red}\cmark}
\end{document}
在使用编译时,PDF 中两者都\cmark
显示为红色,lualatex
但在 HTML 中,只有第一个显示为红色,第二个仍为默认的黑色文本。使用编译为 HTML make4ht foo.tex
在 Linux 上使用 TL 2018
答案1
编辑:我已将color
选项添加到 TeX4ht 源。TeX Live 更新后,应该可以使用以下方法获取颜色:
$ make4ht filename.tex "color"
原始答案中包含的警告仍然相关 - 这可能会导致一些问题,因此仅在必要时使用它。
原始答案:
您可以尝试以下版本color.4ht
:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% color.4ht 2009-05-21-09:32 %
% Copyright (C) 1997--2009 Eitan M. Gurari %
% %
% This work may be distributed and/or modified under the %
% conditions of the LaTeX Project Public License, either %
% version 1.3c of this license or (at your option) any %
% later version. The latest version of this license is %
% in %
% http://www.latex-project.org/lppl.txt %
% and version 1.3c or later is part of all distributions %
% of LaTeX version 2005/12/01 or later. %
% %
% This work has the LPPL maintenance status "maintained".%
% %
% This Current Maintainer of this work %
% is Eitan M. Gurari. %
% %
% If you modify this program your changing its signature %
% with a directive of the following form will be %
% appreciated. %
% \message{signature} %
% %
% [email protected] %
% http://www.cse.ohio-state.edu/~gurari %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\immediate\write-1{version 2009-05-21-09:32}
\expandafter\ifx \csname color:def\endcsname\relax
\let\color:def\def
\else \expandafter\endinput\fi
\let\:temp\begingroup
\HLet\color@setgroup\:temp
\def\HColor{\:warning{\string\Hcode{...}{...} is deprecated; Use
\string\Configure{HColor}{...}{...}}\Configure{HColor}}
\NewConfigure{HColor}[2]{\if !#1!\if!#2!\let\HColor\relax \else \edef\HColor{#2}\fi
\else
\expandafter\edef\csname CLR:#1\endcsname{#2}\fi}
\def\:temp#1#2#3{%
\@ifundefined{color@#2}%
{\c@lor@error{model `#2'}}%
{\@ifundefined{\string\color @#1}{}%
{\PackageInfo{color}{Redefining color #1}}%
\csname color@#2\expandafter\endcsname
\csname\string\color @#1\endcsname{#3}}}
\ifx \definecolor\:temp
\pend:defIII\definecolor{%
\expandafter\ifx\csname HColor:##2\endcsname\relax\else
\csname HColor:##2\endcsname ##3 //%
\edef\:temp{{HColor}{##1}{\HColor}}%
\expandafter\Configure\:temp
\fi}
\fi
\NewConfigure{color}{1}
\def\:tempc#1#2#3{\protect\leavevmode{\protect\a:textcolor
\color#1{#2}#3\protect\b:textcolor}}
% \HLet\@textcolor=\:tempc
\NewConfigure{textcolor}{2}
\def\pagecolor{%
\begingroup \a:pagecolor
\let\ignorespaces\endgroup
\let\set@color\set@page@color
\color}
\NewConfigure{pagecolor}{1}
\def\@undeclaredcolor[#1]#2{\a:color{#1 #2}%
\@ifundefined{color@#1}%
{\@latex@error{Undefined color model `#1'}\@ehd}%
{\csname color@#1\endcsname\current@color{#2}%
\set@color}%
\ignorespaces}
\pend:defI\@declaredcolor{\a:color{##1}}
\pend:defI\@declaredcolor{\ifdefined\end:def:color\end:def:color\else\aftergroup\b:textcolor\fi\a:textcolor\def\end:def:color{\b:textcolor}}
\NewConfigure{SetHColor}[2]{{%
\expandafter\let\expandafter\:temp \csname CLR:#2\endcsname
\ifx \:temp\relax
\edef\:temp{#2 //}\expandafter\get:HColor\:temp
\ifx \HColor\relax
\expandafter\ifx \csname get!HColor\endcsname\relax \else
\edef\:temp{#2!//}\expandafter\csname get!HColor\expandafter\endcsname\:temp
\fi
%
\fi
\else \let\HColor=\:temp \fi
\ifx \HColor\relax
\:warning{missing \string\Configure{HColor}{#2}{...}
(in LaTeX: \csname\string\color @#2\endcsname)}%
\expandafter\global\expandafter\let\csname CLR:#2\endcsname\empty
\else #1\fi }}
\def\get:HColor#1 #2//{%
\expandafter\ifx \csname HColor:#1\endcsname\relax
\let\HColor=\relax
\expandafter\ifx \csname\string\color @#1\endcsname \relax\else
\expandafter\ifx \csname colortyp:\endcsname\relax \else
\csname colortyp:\expandafter\expandafter\expandafter\endcsname
\csname\string\color @#1\expandafter\endcsname
\space . //%
\fi \fi
%
\else
\csname HColor:#1\endcsname #2//%
\fi
}
\def\c:HColor:gray:{\def\HColor:gray##1 ##2//}
\Configure{HColor:gray}{\Configure{HColor}{}{}}
\def\c:HColor:rgb:{\def\HColor:rgb##1,##2,##3 ##4//}
\Configure{HColor:rgb}{\Configure{HColor}{}{}}
\def\c:HColor:cmyk:{\def\HColor:cmyk##1,##2,##3,##4 ##5//}
\Configure{HColor:cmyk}{\Configure{HColor}{}{}}
\long\def\:temp#1#2#3{%
\ifcolors@
\def\:temp{#1}\def\:tempa{\relax}\ifx\:temp\:tempa
\a:colorbox \else \a:fcolorbox \fi
\fi
{#1{\leavevmode #2{\set@color#3}}}%
\ifcolors@
\def\:temp{#1}\def\:tempa{\relax}\ifx\:temp\:tempa
\b:colorbox \else \b:fcolorbox \fi
\fi
}
\HLet\color@b@x\:temp
\NewConfigure{colorbox}{2}
\NewConfigure{fcolorbox}{2}
\Hinput{color}
\endinput
我添加了以下声明:
\pend:defI\@declaredcolor{\ifdefined\end:def:color\end:def:color\else\aftergroup\b:textcolor\fi\a:textcolor\def\end:def:color{\b:textcolor}}
它会在命令前添加一个代码\@declaredcolor
,供 内部使用\color
。我认为它的功能有点脆弱,这就是为什么默认情况下不使用它的原因。\end:def:color
如果在一个 TeX 组中使用了多个命令,则会执行该命令。它会为前一个命令\color
插入结束标记。会插入开始标记并将当前颜色保存到 CSS 文件中。最后一个命令必须在组关闭后使用 命令完成。\color
\:textcolor
\a:textcolor
\color
\aftergroup
请看示例:
Why this is red \textcolor{red}{\cmark}
But not this one? {\color{red}\cmark\color{green}\xmark}
生成的 HTML:
<!--l. 10--><p class="noindent" >Why this is red <span id="textcolor1"><span
class="pzdr-">✓</span></span>
</p><!--l. 12--><p class="indent" > But not this one? <span id="textcolor2"><span
class="pzdr-">✓</span></span><span id="textcolor3"><span
class="pzdr-">✗</span></span>
</p>
和 CSS:
span#textcolor1{color:#FF0000}
span#textcolor2{color:#FF0000}
span#textcolor3{color:#00FF00}
还有一张图片:
此解决方案有一个大问题,这也是为什么它不是默认使用的。以下消息来自tex4ht
讨论此事的消息来源:
The \Verb!\color{red}! instruction might be problematic since it does not on its own determine the extent of the text to be colored. As
因此,其效果可以跨越逻辑界限。这违背了标记语言的哲学,尤其是 XML 的哲学。
It is possible to implement the \Verb!\color{...}! feature but I'm not sure it is desirable to do so. I think it is preferable to expect
用户使用 \Verb!\textcolor{red}{...}! 形式的命令来获取代码片段。
看看这个例子:
\color{brown} Fist paragraph.
Second paragraph.
您可能希望两个段落都是棕色的,但只有第一个段落可以正常工作。问题是颜色更改标签将插入第一个段落内,但它将在段落末尾隐式关闭。结束标签根本不会插入,因为没有要关闭的组。