1

I'm evaluating the possibility to convert my latex book to epub3. This is the MWE:

\documentclass[a4paper,11pt]{book}
\usepackage[top=1in, bottom=1in, left=1.1in, right=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage[dvipsnames, table]{xcolor}

\usepackage{minted}
\definecolor{lightgray}{cmyk}{0.06, 0.05, 0.05, 0}
\setminted[python]{bgcolor=lightgray, breaklines, baselinestretch=0.8, fontsize=\small}
\setmintedinline[python]{bgcolor=lightgray, breaklines, fontsize=\small}
\setminted[text]{breaklines, baselinestretch=0.8, fontsize=\small}

\begin{document}

Lorem ipsum dolor sit amet, consectetur \mintinline{text}{asd_asd} adipiscing elit. Proin varius molestie faucibus. Suspendisse \mintinline{python}{asdasdasd} elementum \mintinline{python}{asd} velit sit amet urna convallis, et mattis nibh mollis. Proin quam felis, tristique nec hendrerit non, lobortis at lorem. Cras ac vehicula mauris. Curabitur ac nibh mollis, sodales ex sed, lacinia neque. \mintinline{text}{asdsad} Morbi fringilla libero ac sem eleifend gravida. Morbi aliquet ligula pharetra euismod lobortis. Fusce tristique id erat vitae venenatis. Ut \mintinline{python}{asdasadasdasasdasd}, \mintinline{python}{asd}, \mintinline{python}{asdasd} quis mi tempor, laoreet metus in, efficitur felis. Cras tristique viverra leo, elementum sollicitudin augue accumsan ac.

\begin{minted}{python}
def my_func(p1, p2, p3="asd"):
    # some line of wonderful code
    print("hello")
\end{minted}
\begin{equation}
y = m x^{2} + 2 x + 1
\end{equation}

Fusce \mintinline{python}{asd} congue neque volutpat massa posuere iaculis. Nulla commodo ipsum sed orci tincidunt faucibus. Mauris volutpat massa nisl, non lobortis lacus pulvinar nec. Nam elementum tortor nisi, ut tincidunt mi blandit vel. \mintinline{text}{asdas_dasd{asd}} Phasellus hendrerit dictum augue. Nullam ac sodales massa. Aliquam in accumsan odio, eu imperdiet libero. Sed aliquam ut orci et malesuada. \mintinline{python}{asd} Donec consequat vel mi id egestas. Sed posuere venenatis mi, quis ultrices leo congue a. Fusce et mi \mintinline{python}{asd} quis purus scelerisque elementum. Maecenas massa lectus, fringilla vel volutpat non, efficitur eget purus. Phasellus quis varius felis.

\end{document}

The lorem ipsum paragraphs have a few mintedinline commands, which represent code. Also, there is minted codeblock and an equation.

First, I tried to run the basic command: tex4ebook -s -f epub3 test2.tex . The result is shown in the attached picture: on the left we can see the pdf, on the right the epub. mintedinline creates new div elements, hence the new rows, whereas minted code block is not respecting new lines and indentations. Also, the equation font size is really big.

enter image description here

Ideally, I would like to create a configuration file targeting mintedinline{text}, mintedinline{python} to create a span element with a custom CSS identifier. As for minted codeblock, I have no idea.

What can I try?

3

It seems that some Minted options don't play nicely with the current TeX4ht support for this package. The following code causes compilation error and it also collapses all lines into one:

 \setminted[python]{bgcolor=lightgray, breaklines, baselinestretch=0.8, fontsize=\small}

The breaklines and bgcolor options seems to cause the problems. To fix that, save the following code as minted.4ht:

% minted.4ht (2019-11-07-14:08), generated from tex4ht-4ht.tex
% Copyright 2019 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this license is in
%   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 2019-11-07-14:08}

\NewConfigure{InlineCode}{2}

\def\:tempa#1{%
  \endgroup%
  \begingroup%
  \Configure{BVerbatimInput}{\HCode{}}{}{}{}{}\a:InlineCode% The \HCode is necessary for correct paragraph handling
  \begingroup%
  \o:minted@inline@iii:{#1}%
  \b:InlineCode%
  \endgroup%
}

\HLet\minted@inline@iii\:tempa


\NewConfigure{MintedColorbg}{2}
\renewenvironment{minted@colorbg}[1]{%
  \colorlet{shadecolor}{#1}%
  \extractcolorspec{shadecolor}{\:tempa}%
  \expandafter\convertcolorspec\:tempa{HTML}\minted:bgcolor%
  \a:MintedColorbg%
  }
  {\b:MintedColorbg\medskip\noindent}

\Hinput{minted}
\endinput

The important code is this:

\NewConfigure{MintedColorbg}{2}
\renewenvironment{minted@colorbg}[1]{%
  \colorlet{shadecolor}{#1}%
  \extractcolorspec{shadecolor}{\:tempa}%
  \expandafter\convertcolorspec\:tempa{HTML}\minted:bgcolor%
  \a:MintedColorbg%
  }
  {\b:MintedColorbg\medskip\noindent}

It redefines internal Minted environment that is used for listings with colored background. It saves the background color using xcolor's commands \extractcolorspec and \convertcolorspec into \minted:bgcolor command. Note that commands used by TeX4ht can contain : character as part of the filename. The commands \a:MintedColorbg and \b:MintedColorbg are used to insert HTML code using \Configure command.

This configuration is inserted from a custom configuration file. We will also handle the line collapsing problem in the config file. Here it is, myconfig.cfg:

\Preamble{xhtml}
\catcode`\:=11
\Configure{fancyvrb}
   {\IgnorePar \EndP \gHAdvance\fancyvrbNo by 1
    \gHAssign\fancyvrb:cnt0
    \HCode{<pre class="fancyvrb" id="fancyvrb\fancyvrbNo">}}
   {\EndP\HCode{</pre>}}
   {\gHAdvance\fancyvrb:cnt by 1
    \ifnum \fancyvrb:cnt>1 
    \HCode{\Hnewline}\fi} {}
   {}{}

\Configure{MintedColorbg}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="minted-color-box" style="background-color:\#\minted:bgcolor">}}
   {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}
\Configure{Picture}{.svg}
\catcode`\:=12
\begin{document}
\EndPreamble

I will describe some parts of the code:

\Configure{fancyvrb}
   {\IgnorePar \EndP \gHAdvance\fancyvrbNo by 1
    \gHAssign\fancyvrb:cnt0
    \HCode{<pre class="fancyvrb" id="fancyvrb\fancyvrbNo">}}
   {\EndP\HCode{</pre>}}
   {\gHAdvance\fancyvrb:cnt by 1
    \ifnum \fancyvrb:cnt>1 
    \HCode{\Hnewline}\fi} {}
   {}{}

This is modified configuration from TeX4ht core. It used <pre> tag instead of <div> for fancy verbatim listings (Minted uses fancy verbatim in the background). This fixed the line collapsing problem.

\Configure{MintedColorbg}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="minted-color-box" style="background-color:\#\minted:bgcolor">}}
   {\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}

This enables background color for the code listing. \ifvmode\IgnorePar\fi\EndP is used to disable paragraphs around the box. We need this code to produce well-formed HTML. The background color is inserted using: style="background-color:\#\minted:bgcolor", which uses the \minted:bgcolor defined in minted.4ht.

Note that inline minted code should be already colored, it is strange that it doesn't work for you.

\Configure{Picture}{.svg}

This requires math to be converted as SVG images. This should result in better looking results than default bitmaps. Note that the size of math depends on the font size of your original document. As you use 11pt base font size, it can be slightly larger. You may try to change the base font size if you want smaller math.

You can compile your document using:

tex4ebook -c myconfig.cfg -sf epub3 test2.tex

Another option for better looking math is to use MathML. The problem is that while MathML is a part of Epub 3 specification, many reading systems don't support it. You can require MathML using:

 tex4ebook -c myconfig.cfg -sf epub3 test2.tex "mathml"

This is the result with SVG image:

enter image description here

And this with MathML:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.