[Tex/LaTex] Can LaTeX be used to create flexible ebook typesize and pagesize, like html

ebooktex4ebook

I've overheard mention of LaTeX being used for the production of eBooks. I'm thinking that it's in reference to producing small pages of a fixed size, say 7", fixed type size, say 8pt, for reading pdf files on the ever more popular Tablet Computers, but not particularly the eInk eReaders that have even smaller screens than that and primarily use ePub, Mobi or Kindle formats.

On second thought, I figured I could be wrong about that. It seems like a programmer could more easily create a LaTeX compiler (a hypothetical alternative to pdfLaTeX) that instead generates epub, mobi or something else I don't know about, which flows to the size of the eReader screen, and resizes to the preferred font size its reader likes.

Has that already been done for those who'd love to use LaTeX for reflowable eBooks, but NOT pdf eBooks?

Best Answer

tex4ebook can be used for LaTeX to ebook formats conversion. It supports epub, epub3 and mobi formats.

I've had some presentations about it last year, slides in Czech are online, as well as source code of examples. Examples include sample code and results, so they are interesting even when you don't understand Czech.

Default look is rather plain and basic, you need to use custom CSS to add some custom design. Maybe use some CSS framework which supports responsive design, it is important in order to support devices with various screen sizes. The CSS can be added in the .cfg file, such as this one:

\RequirePackage{include4ht}
\RequirePackage{addfont4ht}
\Preamble{xhtml}
\CoverMetadata{cover.png}
\AddCss{scale.css}
\Configure{textbf}{\NoFonts\HCode{<strong>}}{\HCode{</strong>}\EndNoFonts}
\Configure{textit}{\NoFonts\HCode{<em>}}{\HCode{</em>}\EndNoFonts}
\begin{document}
\NormalFont{EBGaramond}{EBGaramond12-Regular.woff}
\BoldFont{EBGaramond}{EBGaramond12-Italic.woff}
\ItalicFont{EBGaramond}{EBGaramond12-Italic.woff}
\Configure{@HEAD}{
\HCode{<style type='text/css' >\Hnewline
body{font-family:rmfamily, "EBGaramond", sans-serif;
font-size:1.3em;
}\Hnewline
p img{display:inline;}
</style>}}
\EndPreamble

packages include4ht and addfont4ht are part of helpers4ht bundle, which isn't on CTAN yet. They provide commands \AddCss for adding custom CSS (scale.css in this case), and \NormalFont, \BoldFont and \ItalicFont, which add custom fonts in .woff format. All needed files can be found in previously linked repository with examples for my presentation.

\Configure{@HEAD}{
\HCode{<style type='text/css' >\Hnewline
body{font-family:rmfamily, "EBGaramond", sans-serif;
font-size:1.3em;
}\Hnewline
p img{display:inline;}
</style>}}

this code declares included EBGaramond font to be used in the document, it needs to be included after all other CSS code, so we must use this special construct to insert something into HTML header.

Unfortunately all these features (custom fonts, responsive CSS etc.) supports only epub3 format. You can compile your TeX file using command

tex4ebook -c configfile.cfg -f epub3 texfile.tex

You can see example of TeX file converted using this converter and styled with scale.css here