3

I have a class I originally used for PDF output, but now want to also have an option for HTML. I have a very basic environment where the block of text is a different color. For example:

\documentclass{article}
\usepackage{xcolor}

\newenvironment{specialtext}
{\begin{color}{blue}}
{\end{color}}

\begin{document}
This is normal text.
\begin{specialtext}
This is a first paragraph of special text.

This is a second paragraph of special text.
\end{specialtext}

It works fine for pdflatex, but if I process with htlatex, there is no color. I have spent hours searching forums, and have found that \color and \begin{color} do not work with htlatex (for reasons that are over my head). Unfortunately, \textcolor{blue}{TEXT} is not an option, because the input spans multiple lines.

My knowledge of htlatex and CSS are pretty limited. I tried just defining the environment in my .cfg file, and that didn't work. I also tried making a .4ht file with the same name as the class that has the environment definition (I just cribbed this from a forum; don't actually know what I'm doing):

\ConfigureEnv{specialtext}{\NoFonts}{\EndNoFonts}{}{}
\Css{.specialtext{font-color: blue;}}

\endinput 

And this line then showed up in my .css file:

./specialtext{font-color: blue;}

But the paragraphs that are supposed to be "specialtext" aren't tagged with it.

Is there something really obvious I'm missing? This seems like it should be easy.

TIA!

UPDATE: My tex4ht.cfg file is minimal. I just use it to set the margins so the text is more readable.

\Preamble{html}  
\begin{document}  
\Css{body { max-width : 600px; }}  
\Css{body { text-align : justify; }}
\Css{body { margin : auto; }}
\Css{img {max-width : 500px; height : auto;}}
\EndPreamble

"myclass.4ht" consists entirely of what I typed above. I created it solely to try to deal with this issue. (Like I said, I have pretty minimal knowledge, but everything except this has more or less worked out of the box).

  • it would be helpful if you posted your cfg file as well but I think that you need something like\ConfigureEnv{specialtext}{\HCode{<p class="specialtext">}}{\HCode{</p>}}{}{} – Andrew Mar 20 '18 at 23:18
  • Thank you! I added the content of my .cfg file (such as it is) above responsive to your comment. I will add your suggestiong (I assume to the .4ht file) and see if that works. – Sean Mar 21 '18 at 5:51
2

I moved your environment to a custom package, in order to make it possible to configure it using a .4ht file:

\ProvidesPackage{specialtext}
\RequirePackage{xcolor}
\definecolor{specialtextcolor}{rgb}{0,0,1}

\newenvironment{specialtext}
{\par\begin{color}{specialtextcolor}}
{\end{color}}

\endinput

I made two changes: I defined specialtextcolor color. We will use it in the .4ht file to pass information about color to the CSS file. The second change is explicit \par command. I suppose that you want it and it is necessary in order to avoid HTML tag mismatch that would be produced otherwise.

The configuration file specialtext.4ht may look like this:

\def\get:xcolorcss#1#2{%   
  \expandafter\extractcolorspec\expandafter{#1}{\tsf:color}%
  \expandafter\convertcolorspec\tsf:color{HTML}\tsf:color%
  \edef#2{\#\tsf:color}%                                                                                                                             
}
\ConfigureEnv{specialtext}{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="specialtext">}}{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}
\AtBeginDocument{%
  \get:xcolorcss{specialtextcolor}{\color:specialtext}
  \Css{.specialtext{color:\color:specialtext;}}
}

The \ifvmode\IgnorePar\fi\EndP commands are necessary for correct paragraph handling. The <div class="specialtext"> is used for styling it's contents.

The code in \AtBeginDocument command is more interesting:

\AtBeginDocument{%
  \get:xcolorcss{specialtextcolor}{\color:specialtext}
  \Css{.specialtext{color:\color:specialtext;}}
}

The \get:xcolorcss command extracts color name or specification in the CSS form and saves it in a command for a later use. We can use this command in the \Css command directly. It produces following code:

 .specialtext{color:#0000FF;}

It is necessary to use it inside \AtBeginDocument, because it is defined in xcolor.4ht file, which is loaded after specialtext.4ht (the .4ht files are loaded in order they were used in the document).

This is the resulting HTML:

enter image description here

  • Thank you so much! I tried this. Just to be sure I'm not dealing with collisions, I put the above text verbatim into specialtext.sty and specialtext.4ht in my local texmf path. Then I made a test.tex with just the three paragraphs. On my output, I get !Undefined control sequence. \@begindocumenthook ...t\endcsname \get:xcolorcss{specialtextcolor}{\color:...} Any thoughts on this? – Sean Mar 21 '18 at 14:53
  • @Sean which TeX distribution do you use? The \get:xcolorcss is quite recent addition to tex4ht, so you may need to update it – michal.h21 Mar 21 '18 at 14:57
  • My html output looks just like yours, except the first line is specialtextcolor And the two lines of specialtext are indented but not colored. – Sean Mar 21 '18 at 14:57
  • This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode LaTeX2e <2016/02/01> Babel <3.9q> and hyphenation patterns for 81 language(s) loaded. I'll see if updating to a newer version helps. Thanks again! – Sean Mar 21 '18 at 15:06
  • @sean texlive 2015 is really old, you should try to update. I've updated my answer with definition for \get:xcolorcss so it should work anyway. – michal.h21 Mar 21 '18 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.