Rotating text with tex4ht

i want to rotate text within a table, which works fine when generating a pdf document, but not when using tex4ht. Is there a way to fix this?

MWE:

\documentclass{book}
\usepackage{mwe}

\begin{document}

\rotatebox{90}{text}

\end{document}

• it is actually quite hard to get correct rotation for inline text, it flows all over the page in my tests. – michal.h21 Oct 1 '18 at 13:55

We can use the CSS transform property together with rotate() function to rotate boxes in HTML. You can try the following configuration file graphicx.4ht:

% graphicx.4ht (2017-10-21-16:16), generated from tex4ht-4ht.tex
% Copyright 2003-2009 Eitan M. Gurari
% Copyright 2009-2017 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2017-10-21-16:16}

\let\Gin:esetsize\Gin@esetsize
\def\Gin@esetsize{%
\ifx \Gin@ewidth\Gin@exclamation
\let\Gin:ewidth\Gin@ewidth
\else
\setlength\tmp:dim\Gin@ewidth
\edef\Gin:ewidth{\the\tmp:dim}%
\fi
\ifx \Gin@eheight\Gin@exclamation
\let\Gin:eheight\Gin@eheight
\else
\setlength\tmp:dim\Gin@eheight
\edef\Gin:eheight{\the\tmp:dim}%
\fi
\Gin:esetsize
}
\DeclareGraphicsRule{.png}{bmp}{.xbb}{}
\DeclareGraphicsRule{.jpg}{bmp}{.xbb}{}
\DeclareGraphicsRule{.gif}{bmp}{.xbb}{}
\DeclareGraphicsRule{.pdf}{bmp}{.xbb}{}
\DeclareGraphicsRule{.svg}{bmp}{.xbb}{}

\NewConfigure{rotatebox}{2}
\pend:def\Grot@box{\a:rotatebox}
\append:def\Grot@box{\b:rotatebox}

\Configure{rotatebox}{\HCode{<span class="rotatebox" style="transform: rotate(-\Grot@angle deg);">}}{\HCode{</span>}}
\Css{.rotatebox{
display: inline-block;}}

\Hinput{graphicx}
\endinput


The important code is the following:

\NewConfigure{rotatebox}{2}
\pend:def\Grot@box{\a:rotatebox}
\append:def\Grot@box{\b:rotatebox}

\Configure{rotatebox}{\HCode{<span class="rotatebox" style="transform: rotate(-\Grot@angle deg);">}}{\HCode{</span>}}
\Css{.rotatebox{
display: inline-block;}}


A new tex4ht configuration, rotatebox has been declared and the configuration hooks have been inserted to the \Grot@box command, which is used to draw the rotated box. The hooks are then configured to insert the <span class="rotatebox"> element with style="transform: rotate(-\Grot@angle deg);" CSS style. This style enables the rotation. The \Grot@angle command contains the rotation angle, which must be negative for some reason in the CSS.

The following TeX code:

\documentclass{book}
\usepackage{mwe}

\begin{document}

normal text
\rotatebox{90}{text}
\rotatebox{190}{some longer text}
normal text continues

\end{document}


will be rendered in this way in HTML:

In PDF, it looks a bit different:

The problem is that things started to go wrong when I tried to use a different point of rotation than the center, so I think this is the best we can get.

Edit:

If your Epub viewer doesn't support rotations (just a proof of the poor support ebooks still receive), you can convert the rotated text to an image. Try the following config file:

\Preamble{xhtml,svg}
\Configure{rotatebox}{\Picture+{}}{\EndPicture}
\begin{document}
\EndPreamble


The result:

• Sorry, but i forgot to mention that i'm working on an ebook. The text in the html file gets rotated now, but unfortunately not in the epub file. Which changes are necessary for it to work in an epub file? – Jonas Oct 2 '18 at 10:48
• @Jonas if your epub viewer doesnt support css rotation, then only solution is to convert rotatebox to a picture. – michal.h21 Oct 2 '18 at 13:53
• @Jonas I've added configuration for creation of images from the rotated text. – michal.h21 Oct 2 '18 at 16:56
• Now with the new config file, neither the html file nor the epub file gets rotated, but the generated png files aren't rotated in the first place, so the rotatebox command seems to get ignored completely. Do you have any idea why? – Jonas Oct 5 '18 at 9:18
• @Jonas try the updated config file. it uses dvisvgm for the image creation, hopefully your epub viewer supports svg – michal.h21 Oct 5 '18 at 9:30