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:
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>