Create eBooks from Latex tex files – E-Book aus Latex tex Dateien erstellen

VGO_6993_1000

English below. Click on „weiterlesen“.

Dieser Artikel hat nicht direkt etwas mit Fotografie zu tun, aber indirekt. Als Fotograf möchte man ja seine Bilder auch veröffentlichen und das kann man heute ja auch als Buch oder E-Book ganz preiswert. Bücher habe ich bisher mit dem Text-System Latex erstellt. Dabei nutze ich die Software TexLive und den Editor WinEdt.

Ich habe in den letzten Wochen experimentiert und getestet, wie man am besten aus einer tex Datei ein ebook machen kann. Zuerst hatte ich Softcover probiert, das aber nur für einfache Sachen geeignet war. Abbildungen schlecht und Bibliografie funktioniert gar nicht.

Dann bin ich auf tex4ebook gestoßen, das es hier gibt: https://github.com/michal-h21/tex4ebook. Nach einer Woche Test und Konfiguration, (zuerst mit Linux und dann Windows) bei der der Entwickler Michal sehr hilfreich war, bin ich mit dem Ergebnis sehr zufrieden. Es geht mit einem Klick aus dem Texteditor (ich nutze WinEdt) eine PDF Datei zu erstellen und mit der selben Datei mit dem nächsten klick ein E-Book. So hatte ich mir das vorgestellt. Als Latex Distribution nutze ich TexLive. Mit MikTex wollte es nicht funktionieren. Es geht das Normale, graphicx, fontspec, bibliography, Fußnoten, Formeln.

Für mich ist das die beste Lösung, die ich bisher gesehen habe.

Hier sind die PDF Datei und das E-Book zum anschauen und vergleichen. Beide sind nur als Beispiel gedacht. (Als E-Book Reader für Andoid habe ich Gitden getestet, der sehr gut funktioniert. Für Windows ist Calibre geeignet.) Hier der Link zur Quelldatei.

Weiter unten gibt es eine Anleitung dazu in Englisch, für die, die vor der gleichen Herausforderung stehen und noch nicht wissen, wie es geht. Englisch, um eine breitere Leserschaft zu erreichen.

Create e-Books from Latex tex files

This article has nothing directly to do with photography, but indirectly. As a photographer, one would like to publish his pictures. You can indeed do this today quite inexpensive as a book or e-book. Books I’ve created with the Texsystem Latex. Here I use the software TexLive and the editor WinEdt.

In recent weeks I have experimented and tested on how to best create an ebook from a tex file. Initiallyt I had tried Softcover, but that was only suitable for simple books, pictures where positioned poorly and bibliography does not work.

Then I came across tex4ebook, which is available here: https://github.com/michal-h21/tex4ebook. After one week of testing and configuration (first with Linux and then Windows) at which the developer Michal was very helpful, I am very satisfied with the result. With one click from the text editor (I use WinEdt) you are able to create a PDF file and the next click creates an e-Book from the same source. Just as I had imagined. I use the Latex TexLive distribution. Initially it did not work with MikTex, later I managed this also. All the normal commands plus graphicx, fontspec, bibliography, footnotes, formulas work very well.

For me this is the best solution I‚ve seen so far.

Here are small samples: a PDF file and the same as eBook for viewing, reading and comparison. (As an eBook Reader for Andoid I’ve tested Gitden, which works well. On  Windows I use Calibre.) Here is the Link to the source file.

A sample of a larger project (and files) can be viewed here:

  • eBook (a complete 277 pages book about hoof care, 48 MB )
  • PDF file (the same book from the identical source, 15 MB)

Please respect the copyrights for this book. In case you want this book in a printed version, it’s available from lulu.com.

Content

  • Instructions
  • Installation
    • Install TexLive first
    • Alternatively install MiKTeX
    • Install tex4ebook
    • Creating an eBook with TexLive and TexWorks
    • Creating an eBook with MiKTeXand WinEdt
  • Important settings for e-books
    • Preamble
    • Cover Images
    • Images
    • Ligatures
  • Additional remarks for designing the book
    • Language
    • Special characters
    • Too long lines
    • Images
    • EBook Readers
    • Creating epub3 files
    • Using Tidy to clean-up html files
    • Creating mobi files for Amazon Readers
    • Mathematical Formulas
    • Advanced use of the preamble
    • Using the HTML-Files produced and styling for html and epub
    • Creating good looking tables

Important

Please read the full article to the end before you start experimenting. Then read it again and follow the instructions step by step. I’ve received feedback that combining the TexLive and Miktex instructions into one document makes it difficult to read and follow. I plan to change this soon.

Instructions

last updated 2.3.2015

  • Added links to new sample files

Update 23.2.2015

  • Added instructions on how to create good looking tables

Update 4.2.2015

  • Added instruction for MiKTeX and some minor changes according to changes in tex4ebook.

To create ebooks from new or existing Latex (.tex) files on a Windows PC you need

  • A Linux-Distribution. I have tested it with TexLive, it might work with MikTex also.
    (Download TexLive from https://www.tug.org/texlive/acquire-netinstall.html. Save the file where you find it again for later use.
    Alternatively you can use MiKTeX. My first tests with MiKTeX failed, but with some help from Michal Hoftich and Ulrike Fischer I managed to make it work. See the details below.
    Warning: With both packages – MikTex and TexLive – installed at the same computer at the same time you will run into problems. Unless you want to spend time to find out how to resolve the issues, work with one package only. I picked TexLive as my favourite.
  • The Software Tex4ebook from Michal Hoftich. (Download the zip file from https://github.com/michal-h21/tex4ebook and then click on the icon “Download ZIP” or directly from here https://github.com/michal-h21/tex4ebook/archive/master.zip. Save the file where you find it again for later use.
  • The Software Make4ht from Michal Hoftich
    (Download the zip file from https://github.com/michal-h21/make4ht and then click on the icon “Download ZIP” or directly from here https://github.com/michal-h21/make4ht/archive/master.zip. Save the file where you find it again for later use.

Installation

Install TexLive first

Open the file “install-tl-windows.exe” from you PC. Follow the instructions given. The installation may take 60 minutes or more depending on your internet connection. Also allow to install the Editor that comes with the package. Compiling the tex-files is easier with a good editor.

After a successful installation you should see this:

tex4ebook_1

Alternatively Install MiKTeX

Download the software from here and run the installer. I used the complete installation. The best editor for MiKTeX is WinEdt. It’s not free, but worth every cent. It plays well together with TexLive also. Download WinEdt from here.

Install tex4ebook

This is more to “copy” files to the correct location than “installing” anything. Extract the two downloaded zip files where you stored your download.
This will create two directories

make4htmaster

tex4ebook_2

and tex4ebook-master

tex4ebook_3

After TexLive is installed go to the Command Line interface (cmd) and enter

kpsewhich -var-value TEXMFHOME.

Depending on your Windows Installation and language you will see different output from this command.

tex4ebook_4

In my case it was

C:/Users/Volker/texmf

You may need to create this directory if it does not exist. Also you need to create the subdirectory

  • scripts/lua below texmf and
  • tex/latex/ below texmf

Copy the files extracted from the two links above into this directory. In case you decide to copy the directory as from your download storage area you need to rename the directories from

  • make4ht-master to make4ht and
  • tex4ebook-master to tex4ebook

Then

  • Copy the directory make4ht with it’s content to texmf/scripts/lua
  • Copy the directory tex4ebook with it’s content to texmf/tex/latex/

Finally it should look like this on your PC
Below the directory C:/Benutzer/Volker (resp. your personal settings)

tex4ebook_5

You should see the texmf directory like this

tex4ebook_6

Creating an eBook with TexLive and TexWorks

Open Texworks and create a little testfile like this one

\documentclass{article}
 \begin{document}
 hello world
 \end{document}

Create a PDF file form this with Xelatex for example and see if your Latex installation works.

tex4ebook_7

As a result a PDF file should open in a separate window. Now you can proceed to create an e-book from this file.

Note: With more complicated files like for the examples below and the source file you find on this page I have recognized that you best use LuaLatex to compile your tex file and create the PDF.

To make this easy without using the command line and bat files open “bearbeiten” and click on “Einstellungen”

tex4ebook_8

This window will open

tex4ebook_9

Click on the plus sign to create a new entry

tex4ebook_10

Enter a Name like “E-Book” (or whatever you like) and enter the command “texlua” in the field below.

Next click on the + sign for adding arguments to this command. Enter

C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua

As first argument (This is exactly the location of the tex4ebbook.lua file on your PC. Your location might be different according to you Windows installation). As second argument enter

$fullname

tex4ebook_11

Finally click on OK for each open window (2) to close and save your changes.
From the menu select E-Book as the “Compiler” and click on the green “run” button.

tex4ebook_12

In later tests I have recognized that TexWorks has problems in working with additional arguments, at least I did not manage to make it work and did not want to spend more time in finding it out. I strongly recommend to use WinEdt. It’s a far more advanced Editor and it works. So, if you want to use the advantages of advanced styling of your ebook chnage from here on to WinEdt.

Other Tex-Editors may have other ways of adding these execution commands. I have managed this in WinEdt also. If you use WinEdt, you may know how to do this, if not you find some hints in the help file.

I have used the Luatex entry of the execution modes and mis-used it for creating ebooks
That’s how it looks in WinEdt 8.

tex4ebook_13

At the end of the document is an example of a full featured setting for the parameters.

The creation of the e-book should run without error messages and if so, you will find the ebook in an subdirectory of the original tex-file location with the name [filename]-epub and inside you have your e-book named [filename].epub

tex4ebook_14

If you have Calibre installed, you can open the book and check the content.

tex4ebook_15

From here you can experiment with different tex-files that you already have and/or add commands to your current file to learn more how the eBook compares with PDF files for printing.

I have tested

  • The normal tex stuff
  • Including a cover page
  • Title
  • Index
  • Using fonts with fontspec
  • Bibligraphy
  • Footnotes
  • Ligatures (resp. not using them in html/epub)
  • Math formulas
  • Special charactes like those üäöÜÄÖßũĩõã  and the inverted ǝ

All that works!

Reminder: You should use LuaLatex to compile your tex file and create the PDF from this same file. With XeLatex it did not work for me.

Creating an eBook with MikTex and WinEdt

Creating the eBook with MiKTeX has some hurdles due to – as it seems – a software bug in MiKTeX. After the successful installation of MiKTeX you should perform a quick test, if creating PDF files works. If this is OK you need to install tex4ebook. Different to TexLive MiKTex does not give you a texmf path with the command

kpsewhich -var-value TEXMFHOME.

The result is like this and not very helpful

2015-02-04_09-55-08

So you have to do it manually. Create a directory texmf in C:/Users/[user name]/texmf. In my case I created:

C:/Users/vg/texmf

You now need to tell MiKTeX to be able to find this directory to execute tex4ebook. WinEdt has a great tool to do this. In case you know more about Latex then I do, you may find different ways of achieving this. I used WinEdt. So open WinEdt and select Tex/MiKTeX/MiKTeX Options from the menu.

2015-02-04_18-28-34

The below windows will open. Click on „Add..-“ and add the directory c:\Users\vg\texmf resp. your exact directory to the list.

2015-02-04_09-54-12

Next change the settings in Options/Execution Modes exactly as described above for TexLive.

2015-02-04_18-43-43

The Text in the „Parameters“ field is like this

"C:\Users\vg\texmf\tex\latex\tex4ebook\tex4ebook.lua" "%N%T"

Next is a correction of a software bug in MiKTex. If you would create an eBook now, you will get an error message like this one, unless MiKTeX has updated the software by the time you read this. (Thanks to Ulrike Fischer for finding this out).

(C:/Program Files/MiKTeX
 2.9/tex4ht/ht-fonts/unicode/lm/lm-rep-cmrm.htf)
--- error --- Illegal storage address
Make4ht: Fatal error. Command tex4ht returned exit code 1

This is a problem of tex4ht in MiKTeX: it doesn’t find the unicode.4hf file. The workaround is to move unicode.4hf „one folder down“, so that miktex can find it. Locate the file here

D:/MiKTeX2.9/tex4ht/ht-fonts/unicode/unicode.4hf

Create a directory below ../unicode/charset and call it ../unicode/charset/uni and copy the file unicode.4hf to this directory.

2015-02-04_19-08-35

Now you can start compiling an eBook by selecting „LuaTeX“ from the drop down menu. If you are a fan of command lines you know how to do this anyhow.

2015-02-04_18-49-06

You now should have your test epub file in the same folder as your tex file. There are two errors to correct still. One is to install tidy. This is explained below and not different to TexLive.

The second issue is that MiKTeX complains not beeing able to find „zip“. This is due to a different naming of the file compared to TexLive. MiKTeX calls the file miktex-zip.exe, TexLive calls it zip.exe. So just locate the file in the directory

D:/MiKTeX2.9/miktex/bin

Copy the file miktex-zip.exe, paste it in the same place and call it zip.exe. Done.

The following instructions are valid for both TexLive and MiKTeX.

Important settings for e-books

Preamble

In case you want to use special fonts with the fontspec package of Latex you need to have the following lines of code in your preamble of the tex file after declaring the document class

\ifdefined\HCode
 \usepackage[ngerman]{babel}
 \usepackage[T1]{fontenc}
 \usepackage[utf8]{inputenc}
 \else
 \usepackage{polyglossia} %includes fontspec package
 \setmainfont[Ligatures=TeX]{DejaVu Serif Condensed} 
%or any font you like to use
 \setsansfont[Ligatures=TeX]{DejaVu Sans}
 \fi

The first part is used for the ebook, the second part for your PDF file. Below these lines you may add commands and include packages that can/must be used for both output formats.

Some packages don’t work well with tex4ht (used to create the html file and then the epub file with tx4ht) and at the same time they don’t have much sense to be used in html. Some examples of such packages are fancyhdr or pdfpages. You should put them into \else branch of the preamble

Cover Image

In case you want to include a cover image for your e-Book you have to add the command

\usepackage{tex4ebook}

to the preamble of your tex-file. Additionally as the first line after \begin{document} add

\coverimage[width=1\textwidth]{filelocation/filename}

By that the cover image will only be added to the E-Book and the PDF file. This is helpful, if you want to distribute the PDF file with a cover. For printed books you mostly need a separate cover file and would not need this.

Note: If you use the document class

\documentclass[12pt,twoside]{scrbook}

for your PDF file and try to create an eBook in epub3 format with it, the cover page will most probably move to the end of the book, which is not desired. To prevent that from happening you could use different document classes for books and eBooks like this:

\ifdefined\HCode
    \documentclass[12pt,oneside]{book}
       .... more definitions ...
\else
     \documentclass[12pt,twoside]{scrbook
     .... more definitions ...
\fi

Below you find an other way of achieving this. Read „Advanced use of the preamble“.

Images

Images – different to only producing PDF files – must be stored in the same directory as the tex file or better in a directory below e.g. \images.

If this is not modified in existing tex files, the image will not be displayed in the ebook or the html file. You then access the images with this command:

\includegraphics[width=1\textwidth]{images/image.jpg}

Or any other image placing command you might be using.

Ligatures

Tex supports Ligatures when the fonts used have them as glyphs. Most standard fonts used in html and e-books do not have ligatures. In consequence you will see a blank space instead of an “ff” or “fl” or similar combinations that normally are converted to ligatures.
To prevent this from happening in e-Books you need to create file with following content:

local filter = require "make4ht-filter"
 local process = filter{ "fixligatures", "hruletohr"}
 Make:htlatex()
 Make:htlatex()
 Make:htlatex()
 Make:match("html$",process)

Save it in the same directory as your tex-file with the name of your tex-file and the extension .mk4 (eg.: mytexfile.mk4).

This file will be used by tex4ebook automatically when creating the e-Book and removes the ligatures resp. replaces them with standard characters.

Additional remarks for designing the book

Language

To use the right language for words like „Chapter“ and the corrrect hyphenation you need to declare the language to be used.

\usepackage{polyglossia} %lädt auch fontspec
 %\usepackage{fontspec}
 \setdefaultlanguage [spelling=new]{german}

Special charters

To use special characters that are supported by fontspec for creating PDF files but not by tex4ht you need to include some special commands in the preamble after

\ifdefined\HCode
\usepackage{newunicodechar}
        \newunicodechar{ũ}{\~{u}}
        \newunicodechar{­}{--}
        \newunicodechar{ĩ}{\~{i}}
        \newunicodechar{–}{--}
        \newcommand\utfentity[2]{\newunicodechar{#1}
 {\HCode{\string&\#x#2;}}}
        \utfentity{ǝ}{01DD}

and before

 \else

By this you can print the above defined characters in html and in the ebook as long as they are available in the font you are using. To test it: Arial has them all, otherwise it’s a boring font. You may need more and different ones depending on you language and needs. There is more information one the new unicode character package here. Michal has been a great help in this area.

Too long lines

If you lines are getting too long because hyphenation does not work, use this

\sloppy

This is applied to the complete book. If you need this for a paragraph only do this:

\begin{sloppypar}
 Text
 \end{sloppypar}

Images

General remark: To print a good looking image on the maximum width of an A4 page (21cm) with 300 dpi you need an image width of appr. 2.500 pixels. In consequence your images don’t have to be larger than this or even smaller if your book is smaller and you do not use the full size of the page. Fitting the image size to the maximum need reduces the time for compilation for each image and the size of you PDF file.

To get to the maximum size needed do this calculation: Convert the image width to inches, by dividing the cm with 2.54. Multiply this with 300, e.g. (16 cm / 2.54) x 300 dpi = 1.889 (pixels for the image). This also means: don’t use smaller images. They will not look good in your book.

Images for the full width of the print area look good with these commands:

\begin{figure}
 \includegraphics[width=1\textwidth]{images/VGO_0250.jpg}
 \caption{irgendwas}
 \label{irgendwas}

Images with a caption left or right look good like this. You may want to modify the width of the image by changing the number 0.5 to a higher or lower value depending on what you want to show.

\begin{figure} [t]
 \begin{captionbeside}{Peter und Luise}[i]
 \includegraphics [width=0.5\textwidth] {images/ewald-u1_009.jpg}
 \end{captionbeside}
 \label{Peter_und_Luise}
 \end{figure}

Images in block of four (2 x 2) look good like this:

\begin{figure}
 \subfigure[Bildtext 1] {\includegraphics[width=0.5\textwidth]
{images/bell1.jpg}}
 \subfigure[Bildtext 2] {\includegraphics[width=0.5\textwidth]
{images/bell1.jpg}}
 \subfigure[Bildtext 3] {\includegraphics[width=0.5\textwidth]
{images/bell1.jpg}}
 \subfigure[Bildtext 4] {\includegraphics[width=0.5\textwidth]
{images/bell1.jpg}}
 \caption{Vier Bilder im Block}
 \label{Bell}
 \end{figure}

Images in a block of two next to each other look good like this:

\begin{figure}
 \subfigure[Bildtext 1] {\includegraphics[width=0.5\textwidth] 
{images/ewald-u1_009.jpg}}
 \subfigure[Bildtext 2] {\includegraphics[width=0.5\textwidth] 
{images/ewald-u1_009.jpg}}
 \caption{Zwei Bilder nebeneinander}
 \label{Bell}
 \end{figure}

All these images are converted to eBooks very well.

E-Book reader

To test your e-Book on Windows you may use Calibre.
To test it on an Android Smartphone or Tablet best use the free Gitden Reader available in Googles PlayStore.

Creating epub3 files

The format epub3 is more advanced and creates better looking eBooks. The above reader can handle this file format, some older reader will not open epub3 files. In case you decide to create epub 3 files you need to add a parameter to the compilation command:

-f epub3

The full command may look like this (in on eline of course):

"C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua" 
-c config.cfg  -i -f epub3 "%N%T"

Using Tidy to clean-up html files

To create „clean“ html files that create „clean“ eBooks you may include the software tidy in the command for compiling the book. You do this by adding the parameter t like this

-t

The complete command looks like this

"C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua" 
-c config.cfg  -i -f epub3 -t "%N%T"

To make it work you need to download and install tidy first. Download the file from here. Unzip the file to get tidy.exe. Create a directory „tidy“ below C:/Programme(x86). Copy the file tidy.exe to this directory.

Using the tool Path Editor, that you can download from here, add the path „\tidy“ to the system settings so that the script is able to find the file.

Creating mobi files for Amazon Readers

Tex4ebook allows to directly create mobi files by adding the parameter

-mobi

to the command. The full command in thsi case looks like this

"C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua" 
-c config.cfg  -i -f mobi -t "%N%T"

To make this work you need to download and install „kindlegen“. Download kindlegen from here. Next extract the files to a known directory, then copy them to a directory called „kindlegen“ below C:/Programme(x86). The important file is kindlegen.exe.

Using the tool Path Editor, that you can download from here, add the path „\kindlegen“ to the system settings so that the script is able to find the file.

Note on the mobi format: Mobi is not as advanced as epub3 and not really good for mathematical formulas. It currently does not support mathml. So if you need this, stay away from mobi. It’s good for „normal“ books.

Mathematical Formulas

To make use of the advanced presentation of mathematical formulas with epub3 in your ebook you finally cann add

mathml

as a parameter to you compilation command:

"C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua" 
-c config.cfg  -i -f epub3 "%N%T" mathml

Here is an example screenshot from an Android smartphone using the Gitden reader.

screen_20150122_1818

Advanced use of the preamble

You may have recognized that the preamble has some large modifications. You might find it easier to seperate the files into one preamble for the PDF-Book and one for the eBook and from there use the same file for the main content of the book. Like this:

for PDF

\documentclass{scrbook}
\usepackage{fontspec}
...and more ...
\begin{document}
\input{text}
\end{document}

for eBook/epub

documentclass{book}
\usepackage{...}
\begin{document}
\input{text}
\end{document}

The common file text.tex will look like this

\maketitle
\tableofcontents
\chapter{...}

Using the HTML-Files produced and styling for html and epub

The html file created as a side product of the conversion process from tex to eBook can be used to create a website of your book. If you know a little about modifying the look of a html file with css style sheets it is easy to achieve.

The software creates – depending on the number of chapters in your book – various html files all starting with the name of your original tex-file. You can combine them in one file or leave them as single chapter files and put them on a webserver. By opening the single files you’ll quickly identify it’s content and put them in the correct order.

To make cusstomized css work you need to create a file config.cfg stored in the same directory as your tex file with this content:

\Preamble{xhtml}
\begin{document}
\CssFile[custom.css] 
/* css.sty */
\EndCssFile
\Configure{CoverMimeType}{image/jpg}
\EndPreamble

The custom css will be used by adding a parameter to the compiling command like this:

"C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua"
 -c config.cfg "%N%T"

Next you create a style sheet and call it custom.css. Inside this file you can add the style definitions like this one

div.figure img {
text-align:center; 
width: 100%
}

This is for displaying all images as wide as the page is like on the book page

body {
font-family: "Georgia", Times, serif; 
font-size: 1em; 
line-height: 1.35em; 
width: 19cm; 
padding-left: 2.5cm; 
padding-right: 2cm; 
padding-top: 2cm;
}

This is for selecting a different font, if you like and adjusting the line space for better legibility. It is also for limiting the width of the page and good margins. By this your web-page looks more like a book page.

If you want the text in your eBook with full justification (Blocksatz in German) but not the heading you can add „text-align: justify“ to the body and „text-align: left“ to the headings like this in custom.css

body
    {
    font-family: "Georgia", serif; 
    font-size: 1em; 
    line-height: 1.35em;
    hyphens: auto; 
    text-align: justify;
    }

h2.titleHead
    {
    line-height: 1.5em;
    }

h1,
h2,
h3,
h4
    {
    text-align: left;
    }

Note: The page-width and padding commands do not work well with eBook files. So if your priority is on eBook, do not use the width and padding styles. If you want to use it for html website, have a second run after your eBook is finished with the width and padding set to the correct values.

One other thing that might not look in your eBook like in the PDF file is how lists are displayed. This can also be modified in the custom css file.

Also instead of putting all single html files into one large file you can create a file named index.asp (or similar if you use php) to combine the single html files into one document. Here is an asp example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> test ebook </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <link rel="stylesheet" type="text/css" href="test-fontspec.css" /> 
 </head>
 <body>
  <div class="page-wrap">
<!--#include file="test-fontspec.html"-->
<!--#include file="test-fontspecli1.html"-->
<!--#include file="test-fontspecch1.html"-->
<!--#include file="test-fontspecli2.html"-->
<!--#include file="test-fontspec4.html"-->
<!--#include file="test-fontspec5.html"-->
<!--#include file="test-fontspec6.html"-->
</div>
 </body>
</html>

Creating good looking tables

Tables are difficult in any word processing software. And Latex is very special. Here is a way how I think you could create good looking tables with Latex for your PDF file and your eBook.

I decided to use the package tabularx for this. Consequently you need to add a few more lines to you preamble before \begin{document}. For detailed information and complete documention of the package look here.

\usepackage{booktabs} %Professionelle Tabellen
\usepackage[para]{threeparttable} %Tabellen mit Legenden
\usepackage{tabularx} %automatische Zeilenumbruch ermöglicht
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{caption}
\captionsetup{labelsep=colon,justification=raggedright,
singlelinecheck=off}

After you have done this you can include a table like this:

\chapter{A Table for eBooks}
   {\renewcommand{\arraystretch}{2}
   \begin{table}[th]
      \caption {\textbf{This could be a nice looking table}}
      \label{uebersicht}
      \newcolumntype{Y}{>{\raggedright\arraybackslash\hsize=.25\hsize}X} % 25% line width
      \newcolumntype{Z}{>{\raggedright\arraybackslash\hsize=.50\hsize}X} % 50% line width
      \newcolumntype{Q}{>{\raggedright\arraybackslash}X} % line width default
      \begin{tabularx}{\textwidth}{ZYY}
      \rowcolor{gray!30} \textbf{Lorem} &  \textbf{Ipsum}  &  \textbf{Dolor}   \\ \midrule %table header
         Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.  & Lorem ipsum  &    Aenean massa.  \\ \midrule
         Lorem ipsum dolor sit amet  & Consectetuer adipiscing elit. &  Nascetur ridiculus mus.  \\ \midrule
         4711 & 0815 & 200.000  \\
         \bottomrule
      \end{tabularx}
   \end{table}

The first command

{\renewcommand{\arraystretch}{2}

takes care of increasing the white space between tables rows. The number is a factor to increase it. You may want to play with it to fit your taste.

These three lines define new column types. You may add more definitions according to your needs and the requirements of your table.

\newcolumntype{Y}{>{\raggedright\arraybackslash\hsize=.25\hsize}X} 
\newcolumntype{Z}{>{\raggedright\arraybackslash\hsize=.50\hsize}X} 
\newcolumntype{Q}{>{\raggedright\arraybackslash}X}
  • The first line defines a column of 25% width of the table width and is called Y.
  • The second line defines a column of 50% width of the table width and is called Z.
  • The third line defines a default column width and is called Q.

This helps to control the look of your table according to it’s content. By changing \raggedright to \raggedleft or \justify you can also adopt the justification of text in the columns.

This defines the start of the table:

\begin{tabularx}{\textwidth}{ZYY}

This line defines the table header. In this example with a gray background and bold text:

\rowcolor{gray!30} \textbf{Lorem} &  \textbf{Ipsum}  &  \textbf{Dolor}

Following these commands you add the content itself with \midrule and close the table with a \bottomrule.

The final table in PDF looks like this:

table_test_pdf

And the table in the eBook opened with Calubre looks like this:

table_test_ebook.jpg

To create this look of the table you need to add a few commands in the file custom.css that we have used earlier already.

#TBL-5-1-1, /* table head col 1 */
#TBL-5-1-2, /* table head col 2 */
#TBL-5-1-3 /* table head col 3  add more as required */
    {
    background-color: #E6E6E6;
    line-height: 2em;
    }

#TBL-5-1g /* table background excl. table head*/
    {
    background-color: #F2FAFC;
    }    

table
    {
    width: 100%
    margin-top: 1em;
    margin-bottom: 1em;
    /*font-family: "Arial Narrow";  use a narrow font, if space is an issue*/
    }

div.caption
    {
    margin-bottom: 1em;
    font-style: italic;
    }

The first digit in the ID #TBL-5-1-1 may be different in your files. Check this in your html file which is created during the process of creating the eBook.

Find a working tex file and sample pdf, eBook and custom.css here in one zip file.

Finally

In case you find any errors, typos or something you do not understand in this instruction let me know and send me a note. I also appreciate any remarks if and how you managed to create eBooks.

My current parameters for compiling the eBook are these (in one line):

"C:\Users\Volker\texmf\tex\latex\tex4ebook\tex4ebook.lua" 
-c config.cfg  -f epub3 -t "%N%T" mathml

They work well in WinEdt.

Have fun testing!

Advertisements

4 Gedanken zu „Create eBooks from Latex tex files – E-Book aus Latex tex Dateien erstellen

  1. Pingback: Install make4ht with MiKTeX | DL-UAT

  2. Thank you very much for all this info. I was wondering how you can create the index and the hyperlinks, as in the e-book on hoof care. Best wishes

  3. I wonder if the content of the distribution has changed: the file „tex4ebook.lua“ is not included, which is why I get stuck relatively early in the process. Am I doing anything wrong?

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s