# \textcolor with tex4ht

This is a question about getting colors with tex4ht. The issue was discussed e.g. here, where a suggestion was made, which however I can't get to work. I asked a comment there and it was suggested to make a new question, so here it goes:

Can you give a working example of a config file with a working syntax for \Configure{HColor}{colorname}{csscode}, I can't get it to translate \textcolor{red}{blah} properly.

More precisely, if I have:

\documentclass[12pt]{article}

\usepackage[latin9]{inputenc}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[unicode=true]
{hyperref}
\usepackage{breakurl}

\usepackage{xcolor}

\usepackage[english]{babel}

\begin{document}

\paragraph{\textcolor{red}{WARNING}}

\end{document}


and I compile with tex4ht I get

    <?xml version="1.0" encoding="iso-8859-1" ?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <!--http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd-->   <html xmlns="http://www.w3.org/1999/xhtml"   >  <head>    <title></title>  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  <meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" />  <meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" />  <!-- html,xhtml,-css,NoFonts -->  <meta name="src" content="post2.tex" />  <meta name="date" content="2017-09-07 08:56:00" />  </head>
<body  > <p style="text-align:justify"><span class="paragraphHead"><a   id="x1-1000"></a><span id="textcolor1">WARNING</span></span>    </p>    </body></html>


which does not look red on a browser. Note the command

<span id="textcolor1">WARNING</span></span>


My htlatex options are xhtml,-css,NoFonts

• You disable generation of the css file with the -css option. Sep 7 '17 at 13:08
• How can I have colors with the -css option?
– Manu
Sep 7 '17 at 13:48
• Why don't you want to use the CSS file? It wouldn't be possible in an straight-forward way. Sep 7 '17 at 14:33
• Because free wordpress does not support it. Is it really so hard to tell tex4ht how to interpret the textcolor command?
– Manu
Sep 7 '17 at 15:07
• I found a way hot to do it, see the updated answer Sep 7 '17 at 20:05

If you use xcolor package, you shouldn't need to configure anything, colors should work out of the box:

\documentclass{article}
\usepackage{xcolor}
\definecolor{mycolor}{rgb}{1,0.2,0.2}
\begin{document}

Helo world \textcolor{red}{blah}. And I can use \textcolor{mycolor}{my color}.
\end{document}


If you really want to redefine it in the .cfg file, you need to use valid CSS color declaration, like #0012FF. You must escape the # character:

\Preamble{xhtml}
\Configure{HColor}{mycolor}{\#0012FF}
\begin{document}
\EndPreamble


Edit:

tex4ht saves the colors in the CSS file. Because you don't want to use the CSS file, we must use a trick to save the colors directly in the style attribute of element around the colored text. Try this .cfg file:

\Preamble{xhtml}
\makeatletter
\Configure{textcolor}{\HCode{<span style="color:\#\tsf@color;">}}{\HCode{</span>}}
\begin{document}
\EndPreamble

% redefine \textcolor command
\let\oldtextcolor\textcolor

\renewcommand\textcolor[2]{%
% we need to convert the current color to hexadecimal form usable in the style attribute
% the \tsf@color macro is used in the \Configure{textcolor} configuration to insert the
% CSS color
\extractcolorspec{#1}{\tsf@color}%
\expandafter\convertcolorspec\tsf@color{HTML}\tsf@color%
% call the original command
\oldtextcolor{#1}{#2}%
}%
\makeatother


It produces the following HTML:

<p class="noindent" >Helo world <span style="color:FF0000;">blah</span>. And I can use <span style="color:FF3333;">my color</span>. </p>

• It still doesn't work for me. I added a MWE that shows my output. I tried both with xcolor and without. Maybe there's something in my options that's wrong?
– Manu
Sep 7 '17 at 13:01
• Beatiful, thanks, it works! For some reason it doesn't work if I apply it to section headers, but I haven't debugged.
– Manu
Sep 7 '17 at 21:02