# Can LaTeX be used to create flexible ebook typesize and pagesize, like html?

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?

• that is essentially the same as generating html and tex4ht, latexml etc have customizations to generate the metadata needed to package up an html based ebook. – David Carlisle Jan 26 '16 at 21:11
• You can do it but I’m not sure if it makes a lot of sense: much of LaTeX’ strength is as a typesetting engine and you lose all that when producing an ebook: engines such as pdfLaTeX are comparable to the ebook reader and produce the display rather than the epub/mobi: they interpret the (ebook) input and render it. And they make fundamentally different trade-offs: an ebook reader engine needs to render text very quickly with limited resources, but produces sub-optimal (let’s not mince words here: terrible) layout. pdfLaTeX produces great layout but uses time-consuming algorithms for this. – Konrad Rudolph Jan 27 '16 at 12:58
• What If a new .eBk format using a new standardized set of LaTeX packages, a finalized LaTeX3, and a new eBk TeX distribution were created just for ebook reader devices, so it could compile every .eBk file to it's screen dimension and the reader's choice of font and sizes: maybe 8pt, 9pt, 10pt, 11pt or 12pt, and the reader's choice for Roman, San-Serif and Typewriter fonts. And, the device could adjust (recompile) changes in preferences if necessary. Imagine what that would be like. I wish users would develop such a system. That seems easy, and eReader devices are getting powerful. – user12711 Jan 28 '16 at 15:15

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}
\Preamble{xhtml}
\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}
\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

• Excellent example. I didn't realize all that was possible with html (or epub). – user12711 Feb 14 '16 at 3:34