1

I've created a simple macro in LaTeX for notes:

\documentclass{book}
\usepackage{xcolor}
\definecolor{notecolor}{rgb}{1,0.2,0.2}
\newcommand\note[1]{{\textcolor{notecolor}{\textbf{Note: }}}#1}

\begin{document}
\chapter{Introduction}
\note{This is a sample note}
\end{document}

I want to be able to style this with a border in HTML output via make4ht. The converted HTML looks like:

<p class="indent">   <span id="textcolor1"><span class="cmbx-10">Note: </span></span>This is a sample note
</p>

and I've created a tex4ht custom cfg file with:

\Configure{note}{\HCode{<p class="note">}}{\HCode{</p>}}
\Css{note{border-style: solid;}}

but the generated HTML remains the same after regenerating.

I've looked at various samples for customizing css with tex4ht but can't get it working. I've seen examples where this configuration seems to work, and others involving a .4ht file. I'm unclear on the use cases for different approaches.

Can anyone explain what I'm missing here and what the correct solution is?

2

The main issue why your code doesn't work is that you must declare note configuration first and then redefine the \note command to use this configuration.

I would extract your macro to a custom package, because the configuration file for that package can be called automatically by tex4ht. Let's say that the package is named mynote.sty:

\ProvidesPackage{mynote}
\RequirePackage{xcolor}
\definecolor{notecolor}{rgb}{1,0.2,0.2}
\newcommand\notetitle{Note:~}
\newcommand\note[1]{{\textcolor{notecolor}{\textbf{\notetitle}}}#1}
\endinput

I've moved the Note: text to a standalone macro, in order to make it a little bit more configurable. In this case, I would redefine the \note macro to insert three configurable hooks: first at the beginning, the second after the Note: text and the last one at the end. This will allow high level of flexibility in the configuration.

The configuration file, mynote.4ht:

\NewConfigure{note}{3}

\renewcommand\note[1]{\a:note\notetitle\b:note#1\c:note}

\Configure{note}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="note">}\par\HCode{<span class="title">}}
{\HCode{</span>}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}\par}

\AtBeginDocument{%
  \get:xcolorcss{notecolor}\note:color
  \Css{.note span.title{color:\note:color;font-weight:bold;}}
  \Css{div.note{border:1px solid \note:color;}}
}

As you can see, the original \note command is replaced by new definition, because we want to define the formatting purely with HTML and CSS, to avoid ugly code like

 <span id="textcolor1"><span class="cmbx-10">Note: </span></span>

The \a:note, \b:note and \c:note macros are created by \NewConfigure{note}{3} command.

The \Configure{note} command may look a little bit complex, so let's analyze it:

{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="note">}\par\HCode{<span class="title">}}

The \ifvmode\IgnorePar\fi\EndP code will end the current paragraph, it is necessary in order to avoid insertion of the closing </p> after opening <div> tag. \HCode{<div class="note">}\par inserts the <div> tag and opens a new paragraph. Because you can use multiple paragraphs inside the \note command, it is better to request it explicitly. The \HCode{<span class="title">} will be inserted before the \notetitle, this element is closed in the second configuration by \HCode{</span>}.

The last part closes the paragraph inside the note and closes the <div class="note"> element. It may be also necessary to use the \par element to fix issues with paragraph which follows the \note command. As you can see, the paragraphs can be a tricky business in tex4ht.

Interesting stuff is done in the styling part:

\AtBeginDocument{%
  \get:xcolorcss{notecolor}\note:color
  \Css{.note span.title{color:\note:color;font-weight:bold;}}
  \Css{div.note{border:1px solid \note:color;}}
}

\get:xcolorcss is a command provided by tex4ht configuration for the xcolor package. It extracts CSS definition of a color to a macro, which can be then used in the CSS definitions. This block must be enclosed in the \AtBeginDocumnt command, because it must be loaded after the xcolor configuration. The \Css commands just defines color for the title and border.

For a modified test file:

% https://tex.stackexchange.com/q/402271/2891
\documentclass{book}
\usepackage{mynote}

\begin{document}
\chapter{Introduction}
\note{This is a sample note}
Another text
\end{document}

You will get this result:

enter image description here

And the following HTML code:

   <h2 class="chapterHead"><span class="titlemark">Chapter 1</span><br /><a 
 id="x1-10001"></a>Introduction</h2>
   <div class="note">
<!--l. 8--><p class="indent" >   <span class="title">Note: </span>This is a sample note</p></div>
<!--l. 9--><p class="indent" >   Another text </p> 
  • Thank you so much for the excellent explanation. It is becoming clearer. The one issue I still have is how mynote.4ht is invoked. Should it be loaded from mynote.cfg or is it autoloaded somehow? Is the name important/arbitrary, etc. These are details I have not been able to find. – Ron Nov 20 '17 at 19:38
  • @Ron it is loaded automatically, tex4ht tries to load file with .4ht suffix whenever it loads a package with the same name. some information is here: github.com/michal-h21/helpers4ht/wiki/tex4ht-tutorial – michal.h21 Nov 20 '17 at 19:59
  • I'm still missing something. I copied your file samples above to a new document project and using 'htlatex file.tex', the note border is not picked up. The html still looks like "<p class="noindent"><span id="textcolor1"><span class="cmbx-10">Note:</span><span class="cmbx-10">&nbsp;</span></span>This is a sample note </p>". Seems that the .4ht file is not being found (in same dir as everything else). Am I missing something obvious like a command line argument. – Ron Nov 21 '17 at 8:41
  • @Ron the .4ht file must have the same basename as the .sty file, then it should be loaded automatically. – michal.h21 Nov 21 '17 at 8:53
  • Thank you again for all the help. That was indeed the problem. – Ron Nov 21 '17 at 8:58

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.