TeX4HT allows to reset LaTeX macros to insert HTML code in order to produce clean HTML documents. This is great, but it often gets in a mess of <p> tags, such as:

<p class="noindent" ><hr />Some text</p>

after I set a command to insert <hr />. Here I'd like <hr /> to be outside any <p> tag.

There's worse. Sometimes it just generates code which is plain wrong, like:

<p class="noindent"><span class="small-caps"></p><p class="noindent">Some text</p></span>

after I set a command to set small caps.

Is there a way to control these <p> tags to make them behave properly?

  • Do you use custom commands and configurations or just standard LaTeX? Can you post mwe? – michal.h21 Aug 7 '12 at 7:02
  • It's not standard, there's quite a few personal commands. The code is on github.com/cc-translators/sagesse. It is built by typing make sagesse_lulu.html. – ℝaphink Aug 7 '12 at 7:37

Paragraph handling is little bit tricky with tex4ht. Some information can be found in the article TeX4ht configure: Part 1.

There are two issues, if command with configuration hooks is at the beginning of the paragraph.

If some hook is inserted at the beginning of the paragraph, it must place the

tag manually, with some of commands described in the article. The code usually looks like:

\ifvmode \IgnorePar\fi \EndP\Tg<h2 class="dvmonth">}{\Tg</h2>\IgnoreIndent}

This will include block level element with no paragraphs inside, \IgnoreIndent will cause next paragraph to have class="noindent".

For inline elements, it can look:

\Configure{sample}{\ifvmode \ShowPar\fi \HCode{<i>}}{\Tg</i>}

There is probably bug with \Tg<> command, which causes that its content gets before

tag. Solution is to use \HCode instead in the hooks which are inserted at beginning of the paragraphs.

%Wrong, gives: <i><p>
\Configure{sample}{\ifvmode \ShowPar\fi \Tg<i>}{\Tg</i>}
\Configure{sample}{\ifvmode \ShowPar\fi \HCode{<i>}}{\Tg</i>}

Edit: As Ulrike Fisher pointed out in comments, there are some details about configuring.

If you have some custom commands and you want to provide configurations for them, it is best to collect them in some package like mycommands.sty. Then you can provide file mycommands.4ht, which is called automatically by tex4ht when you include mycommands.sty.

For every configuration, you need to provide three things:

  1. Create configuration hooks. This is done with


    This will create configuration hooks \a:sample and \b:sample.

  2. Redefine command to include configuration hooks:


  3. Provide default configuration for hooks

    \Configure{sample}{\Tg<tag name>}{\tg</tag name>}

This configuration will be then used as default.

You can redefine default configuration in custom config file, which is specified at command line when running tex4ht.


\Preamble{xhtml, charset=utf-8}
\Configure{sample}{\Tg<another tag>}{\Tg</another tag>}

To be compiled with

htlatex myfile "mysample, other options" " -cunihtf -utf8"

Last possibility to put configuration somewhere in your tex file in block that is called only when running with tex4ht

\Configure{sample}{\HCode{<bla bla>}}{\HCode{</bbla bla>}

note that there are some commands disabled, like \Css and this method is best be used for testing purposes only

| improve this answer | |
  • 1
    Now my documents are valid XHTML 1.0 documents! – ℝaphink Aug 7 '12 at 11:22
  • Someone should write a book about TeX4HT, it's a great tool once you know how to use it! – ℝaphink Aug 7 '12 at 11:50
  • 1
    Maybe we could start with community wiki here, I would add some details about creating configurations etc., but my knowledge in other areas like font handling is really limited. – michal.h21 Aug 7 '12 at 12:03
  • @michal.h21: It would be great if you can add infomations about configuring. Beside the actual code: could you also add some ideas how to organize the code. E.g. where to put new/changed configuration code if it should affect only one document or if it should be used for all documents or if you simply want to do a fast test. – Ulrike Fischer Aug 8 '12 at 7:42
  • @UlrikeFischer I added some details – michal.h21 Aug 8 '12 at 12:30

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.