Using textstyle4ht package

Michal Hoftich

August 2, 2015

Contents

1 Introduction
2 Basic configuration
3 Styling

1 Introduction

textstyle4ht is part of helpers4ht, helper packages for tex4ht bundle. It’s main function is to make easier custom configurations, in particular inputing HTML tags and automatic generating corresponding CSS for basic font attributes such as style, weight and color.

As it is best to learn with examples, here is a LaTeX package mystyles.sty

  \ProvidesPackage{mystyles}
  \RequirePackage{xcolor}
  
  \newcommand\myhighlight[1]{\textcolor{red}{\textbf{#1}}}
  \newenvironment{myquote}{\small\itshape\color{green}}{}
  \endinput

We can use these macros like that:

  Here is some text \myhighlight{with highlights}, but \textit{also} \textbf{some
    other} formatting.
  
  \begin{myquote}
  Some not so long quote
  \end{myquote}

2 Basic configuration

In order to make our macros configurable, we may use special file where configurable hooks are inserted. This file is named mystyles.4ht:

  \NewConfigure{myhighlight}{2}
  \pend:defI\myhighlight{\a:myhighlight}
  \append:defI\myhighlight{\b:myhighlight}

What it does is that it declares two new hooks with \NewConfigure and then patches \myhighlight macro. \pend:defI will insert content at the beginning of the macro \mystyles, \append:defI at the end. Note that : character can be contained in macro names in .4ht files. Also note that several versions of \[ap]pend:def<x> exists, depending on the number of parameters of patched macro.

Macros \a:myhighlight and \b:myhighlight were declared with \NewConfigure{myhighlight}{2}. Their content can be declared with

  \Configure{myhighlight}{content for \a:myhighlight}{and content for \b:myhighlight}

Now we can show usage of textstyle4ht package. We can modify the mystyles.4ht file slightly:

  \RequirePackage{textstyle4ht}
  \NewConfigure{myhighlight}{2}
  \pend:defI\myhighlight{\a:myhighlight}
  \append:defI\myhighlight{\b:myhighlight}
  \Configure{myhighlight}{\InlineTag{myhighlight}{}}{\EndTag}
  \ConfigureEnv{myquote}{\BlockTag{myquote}{}}{\EndTag}{}{}

There are some new commands introduced. Normally, tags are inserted with \HCode{<tag>} command. textstyle4ht uses instead special commands for inserting inline tags, block tags and their automatic closing. This meand that instead of

  \Configure{\HCode{<div class="hello">}}{\HCode{</div>}}

you can use

  \Configure{\BlockTag{hello}{}}{\EndTag}

For block tags, div is the default element, so you don’t have to specify it, first parameter is class of the element, second parameter are any HTML arguments. You can leave it empty in most cases.

If you want use different element than div, you can specify it as optional parameter:

  \BlockTag[article]{hello}{}

What is the difference between \InlineTag and \BlockTag? Default element for inline tag is span and it should be used for shorter text inside paragraph. Block tag should be used typically for longer texts, with possibility of nested paragraphs. Typical use are some environments. It takes care of correct paragraph handling see Control the <p> tags inserted by tex4ht for some details.

3 Styling

textstyle4ht also provides macros for saving basic font information such as font size, weight, style or color in the CSS file. It is little bit complicated because of technical limitations. The font information must be grabbed at place where it is in effect, which unfortunately isn’t inside configurable hooks, as these are inserted before and after macro code, and font changes happen inside.

Two commands are provided: \SaveStyle and \GrabStyle. First one is simpler, it saves font information for the current element. The second is useful for saving information from commands with such as \myhighlight

We can add to mystyles.4ht these lines

  \append:def\myquote{\SaveStyle}
  \AtBeginDocument{\GrabStyle{\InlineTag{myhighlight}{}}{\myhighlight}}

They may look mysterious at first, but it is not so hard. We use \append:def to insert \SaveStyle after beginning of mystyle environment. It is available as \mystyle macro. At this moment, <div class="myqoute"> is selected as current element and we can use just \SaveStyle to save the CSS.

The second line is even more mysterious. \GrabStyle macro takes two parameters, first produces element which should be configured CSS for, the second is macro which is grabbed for style. It must take exactly one parameter. The macro is printed inside a box, which is then discarded, so nothing shows in the document. But it must be called in the moment when text can be printed, so we must call it in \AtBeginDocument, because .4ht files are processed when LaTeX is still inside document preamble.

And this is how our example with all configurations ends:

Here is some text with highlights, but also some other formatting.

Some not so long quote