1

I need a colored text in an enumeration list. While the colors work fine outside enumerations, they disappear within enumerations (when compiling a pdf instead of an epub, the colors are there). Is there a way to get colored text in an enumeration list with tex4ebook?

Here is an MWE:

\documentclass[12pt]{book}

\usepackage{enumitem}
\usepackage{xcolor}

\begin{document}

\textcolor{blue}{
This text is blue.
\begin{enumerate}
    \item This text is black, although I would like to have it blue.
\end{enumerate}
}

\begin{enumerate}
\color{blue}
    \item This text is still black, although I would like to have it blue.
\end{enumerate}

\end{document}

And my config file:

\Preamble{xhtml}

\begin{document}
\EndPreamble

1 Answer 1

2

The problem here is due to how HTML works. As \textcolor can be used for shorter texts inside paragraphs, it must produce HTML tags that can be used inside paragraphs. As you use \textcolor for several paragraphs and even list environment, the produced markup is invalid. It works for the first line, as the start of color is marked up correctly, but the browser closes the color element as soon as the list starts.

To solve this, I would introduce a new environment, which can be configured to produce a correct HTML markup. I would modify your file in this way:

\documentclass[12pt]{book}

\usepackage{enumitem}
\usepackage{xcolor}

\newenvironment{colorenv}[1]{\color{#1}}{}
\begin{document}

\begin{colorenv}{blue}
This text is blue.
\begin{enumerate}
    \item This text is black, although I would like to have it blue.
\end{enumerate}
\end{colorenv}


\begin{enumerate}
    \item 
  \begin{colorenv}{blue}
      This text is still black, although I would like to have it blue.
    \end{colorenv}
\end{enumerate}

\end{document}

The colorenv can be then configured in the configuration file:

\Preamble{xhtml}
\ExplSyntaxOn
\renewenvironment{colorenv}[1]{
\get:xcolorcss{#1}\:colorenv
\ifvmode\IgnorePar\fi\EndP\HCode{<div~style="color:\:colorenv">}\par\ShowPar
}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
\ExplSyntaxOff
\begin{document}
\EndPreamble

The \get:xcolorcss command converts the color name, such as blue to hexadecimal RGB value, which can be used in CSS instructions. It is saved in the \:colorenv command, which we then use in <div~style="color:\:colorenv"> instruction.

This is the result:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .