# Making \marginpar work with tex4ht

I tried using \marginpar using tex4ht and pdflatex.

Here is the PDF output:

Here is the HTML output:

Is it possible to configure tex4ht to produce HTML output with good looking marginal notes?

-
How is the HTML output supposed to look like? –  krlmlr Oct 18 '12 at 23:00
Like the PDF output in the first image? –  M-V Oct 19 '12 at 3:03
@M-V, what is the status of this question? Are you still looking for solutions or have you found something suitable? –  Paul Gessler Aug 19 '14 at 2:15
Actually I gave up and moved on. I am no longer using tex4ht. Thanks. –  M-V Aug 19 '14 at 3:59

This is really just a css issue. Stylesheets produced by tex4ht are pretty plain and basic, which is a good thing, because you can easily modify the appearance of things to your needs.

By default, content generated by tex4ht is sized to fill whole page width. This is on the one hand wrong from typographical and usability points of view, as long lines are hard to read. Second problem in this particular problem is that there is no space in the right column where margin par could float.

So we need to set text width to some size, to ease the read and to take some space in the margins. To make things little bit harder, we have to take account the fact, that these days, many people browse web with their smartphones or tablets, so we have to deal with situation that these devices have width smaller than ideal page width and there is no space left for the margins.

This is possible to solve using css technique called media queries and responsive web. I like the article about web typography, which provides basic template for responsive typography.

I made small package for ease working with css in tex4ht. It is not released yet, but it is useful for including external css files. This package is called cssframework.sty:

\ProvidesPackage{cssframework}
\edef\css@curroutpufile{}

% open css file
\newcommand\cssstartoutput[1]{%
\xdef\css@curroutpufile{#1}%
\special{t4ht>\css@curroutpufile}%
}

% close css file
\newcommand\cssstopoutput{%
\special{t4ht<\css@curroutpufile}%
}

}
% #1 template name
% #2 css output file
\newcommand\processcss[2]{%
\AtBeginDocument{%
\cssstartoutput{#2}%
\NoFonts%
\input{#1}%
\EndNoFonts%
\cssstopoutput%
}
}

\newcommand\includecss[1]{%
\special{t4ht+@File: #1}
}

\edef\css@selectrpath{}
\def\hnl{\HCode{\Hnewline}}
%\def\finselectr{}
\newcommand\Selector[2]{%
\bgroup%
\edef\css@selectrpath{\css@selectrpath{} #1}
\hnl\unskip\css@selectrpath\{\hnl
#2
\hnl\}\hnl
\egroup%
}

% support for media queries
% default medium is all, #2 is rule, #3 is
\newcommand\MediaQuery[3][all]{%
@media #1 and (#2)\{\hnl
#3\hnl%
\}\hnl%
}

% Various helper functions
% default font size
\newcommand\emwidth{16}
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}
% support for browsers without rem
\newcommand\RemProperty[2]{%
#1:\the\dimexpr #2\relax;#1:\CalcRem{#2}rem;
}


and I modified the code provided by Viljamis little bit, incorporating some adjustments for tex4ht, you can download it from gist: scale.css.

Now you need to combine these pieces in custom config file:

\usepackage{cssframework}
\Preamble{xhtml}
\includecss{scale.css}
\begin{document}
\Css{.marginpar {width:5em; float:right; text-align:left; margin-left:auto; font-size:85\%; text-decoration:none;color:\#aaa;}
}

\Css{@media screen and (min-width: 46.3em){
.marginpar{margin-right:-6em;}
}}
\EndPreamble


we loaded cssframework package, included file scale.css and provided basic styling for marginpar. Notice that we used \% and \# to escape special characters. Interesting part is

\Css{@media screen and (min-width: 46.3em){
.marginpar{margin-right:-6em;}
}}


from inspection of scale.css, I found that text width, when there is enough space is set to 34.3em. Marginpar has width of 5em, plus 1em is left as space between text and the note. Because the text is centered, we need to show marginpar in margin when there is enough space on both sides, so we need to calculate text width + 2 * (margin par width + space) = 34.3em + 2 * (5em + 1em) = 46.3em. So enough space for showing the margin par is 46.3em.

The result when there is enough room:

And when the width is too small:

-