2

I'm using Texmaker from the MikTex distribution.

What I would like to do, is to

  • create Latex code
  • run Texmaker to do all the substituitons, e.g. from \newcommand
  • build it as pure ASCII-code rather than as pdf

Question: How to do it, how to configure Texmaker, provided it's possible?

Proposals from your comments: In chronological order:

  • use or combine with pdftotext

  • use tex4ebook with DOM-filters

  • use the lwarp package

  • use pandoc

  • use markup

My preliminary evaluation of these proposals:

  1. pdftotext works, of course, and might be useful as a fallback solution if I'd need to redo the epub file 100 % (or in parts) manually with Sigil, see flow below. Excluded lwarp, pandoc and markup from this evaluation.

  2. I'm confident to achieve my objective by a) running tex4ebook with a config-file as proposed by michal.h21, b) use Scrivener to introduce some substituions beforehand, e.g. to preseve work done on \index{}, c) let Sigil do its magic (re-formatting, TOC, metadata, etc.). // Yes, it will remain a semi-automatic process.

  3. Using 2a) alone the created epub-file seems to behave fine with Calibre's eBook reader (software), but gives strange behavior on my iPad (hardware). Haven't dug into it, but probably the <guide> section inside content.opf misses some information for some reason. Sth. like that. // Just another reason to follow a minimal-coding strategy, i.e. avoiding as many fancy stuff in the output as possible.

  4. Using make4ht with the same config file and processing that HTML-file with Sigil on a new epub seems to work fine, even on my iPad.

Process in mind: From your comments please find the basic process I have in mind below. At the moment it's not clear, whether or not I can realize it, and how reliable it will be when repeated. The pdf-part is reliable, while epub-creation can lead to fragile epub code (works on some reader, but not on others). // Approach: single source, once frozen, pdf AND epub output. // The example is simplified, of course. // epub can't be any valid epub-content, to avoid problems on any eBook-reader. // "Minimal epub" means: don't include fancy things in the output file. // An example can be HTML-comments, which are allowed, but, with bad luck, irritates some eBook reader (takes forever to just load it). // Decoration with <p> </p> - tags is done by Sigil, if I remember correctly. So is partioning, TOC-creation, stylesheeting etc. I.e. many things pdflatex would provide are kind of redundant.

Single frozen source, pdf AND epub (running on any eBook reader) derived from it.

I a nutshell I need to get rid of less useful bytes, and have more control to insert classes, div-tags etc. Trust me: this can be done partially with ease using Scrivener, if needed. (If you don't know this program, think of a tool to create, organize, modify and collect a huge set of notes of various length.)

The problem is that programs/tools tend to put too much into an epub file ... which is a really weak format (may work fast and fine on one reader, but causes problems on an other one).

enter image description here

Example (almost obsolete now): Unfortunately I left room for some confusion about what my "ASCII"-requirement may or may not mean. Hoping, readers no longer trigger on 'ascii' or 'pdf', and starting with this simple Latex document ...

\documentclass[10pt]{book}

\begin{document}

\chapter{Lorem ipsum}

  Dolor sit amet consectetuer eros sit quis mauris pretium. Phasellus penatibus interdum dolor Ut nisl.

  \section{Nam amet}%<<<

  Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum amet.%<<<

  Facilisi Nulla ultrices malesuada orci nibh eget ac Aliquam eros ut. 

  \section{Lorem gravida}

  Oorci sociis Nunc id hendrerit at ac amet Pellentesque. Eleifend risus orci sem Sed ac.

  A nec pellentesque Pellentesque Morbi fringilla accumsan et metus at enim. 

  Eu felis Curabitur quis nibh tellus.

\end{document}

... it would be OK if the marked part turns into ...

  <h3 class='myOne'>1.1 Nam amet</h3>
<p>Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum amet.
</p>

... but certainly not into ...

  <h3 class='sectionHead'><span class='titlemark'>1.1   </span> <a id='x2-20001.1'></a>Nam amet</h3>
<!-- l. 12 --><p class='noindent'>Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum amet.
</p>

Anything else you might see when displaying a pdf file in an ASCII editor is not wanted here.

Background 1 (almost obsolete now): This is an alternative attempt to create HTML which is as pure, i.e. minimal, as possible. I tried tex4ebook, which is a great tool, but unfortunately it puts all kind of extra-information and styles, mimicking Latex-appearance, which I do not want, even with the tidy-option. (Perhaps I'm missing an option to get rid of it?)

I think of a two-step process:

  • ASCII-creation as given above
  • run some Perl-script to resolve remaining issues

The expansion feature from Latex/Texmaker would be nice, e.g. to expand abbreviations (via \newcommand), and references from using \ref or \vref the way I need as HTML. I can do this to some degree from creating a pdf AND copying relevant text from it (i.e. typset text "spoiling" with HTML-tags) - but this is not a nice solution.

There will be remaining issues like extracting and transforming e.g. list-environments. But this should be doable with Perl, which was made for this purpose.

Background 2 (almost obsolete now): The objective is to create just one big HTML-file, which I can break down as needed by Sigil, which takes care of all epub-stuff.

Background 3 (almost obsolete now): I create my Latex document using Scrivener, a writing tool, by inserting only relevant Latex-code AND compiling as plain text into Texmaker. This gives me full and easy control what to include or exclude or modify things.

Screenshot, showing a page opened in Sigil, demonstrating extra-information, which is not needed, and missing tags, which need to be inserted, e.g. via my Perl-script. Top-right: tex4ebook processing. // This is a short example where too much output is created for the epub file. Less is more, more or less.

enter image description here

1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Joseph Wright
    Commented May 16, 2022 at 16:19

1 Answer 1

4

Honestly, I don't think that what you want to achieve is too useful. The extra HTML tags and attributes carry useful semantic information that can be then used for CSS styling, etc.

For example this code:

<h3 class='sectionHead'><span class='titlemark'>1.1   </span> <a id='x2-20001.1'></a>Nam amet</h3>
<!-- l. 12 --><p class='noindent'>Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum amet.
</p>

<h3 class='sectionHead'> means that this title was produced by the \section command, <span class='titlemark'> can be used for special formatting of the section number. <a id='x2-20001.1'></a> is a destination for links from \ref commands pointing to this section, and also from TOC. If you remove this tag, cross-references will stop working. <!-- l. 12 --> is line number of the original TeX file, this can be useful for debugging, but I agree that it is not as useful as the other tags. <p class='noindent'> means that this paragraph was not intended in the original document. As HTML files are meant for consumption by machines, which don't mind extra information, you don't gain anything by removing the tags, but lose quite a lot.

With this being said, if you really want to remove all this information, you can. There are two possible ways. One is to use TeX4th configuration file to change generated tags, the other is to use LuaXML DOM filters to remove tags programmatically. You can also mix these approaches, to use the configuration file for the easier stuff, and the build file to remove remaining elements that are hard to remove from the TeX side.

Your particular example can be solved using just the configuration file. Save the following code as mycfg.cfg:

\Preamble{xhtml}
\def\blocktag#1{\ifvmode\IgnorePar\fi\EndP\HCode{#1}}
\Configure{chapter}{}{}{\blocktag{<h2>}\chaptername\ \TitleMark\HCode{<br />\Hnewline}}{\blocktag{</h2>}}
\Configure{section}{}{}{\blocktag{<h3>}\TitleMark}{\blocktag{</h3>}}
\Configure{subsection}{}{}{\blocktag{<h4>}\TitleMark}{\blocktag{</h4>}}
\Configure{subsubsection}{}{}{\blocktag{<h5>}\TitleMark}{\blocktag{</h5>}}
\ConfigureMark{chapter}{\thechapter}
\ConfigureMark{section}{\thesection\ }
\ConfigureMark{subsection}{\thesubsection\ }
% subsubsection doesn't need mark configuration, as it doesn't produce a number
% handle paragraphs
\Configure{HtmlPar}{\EndP\HCode{<p>}}{\EndP\HCode{<p>}}{\HCode{</p>}}{\HCode{</p>}}
\Configure{textbf}{\HCode{<b>}\NoFonts}{\EndNoFonts\HCode{</b>}}
\Configure{textit}{\HCode{<i>}\NoFonts}{\EndNoFonts\HCode{</i>}}
\Configure{emph}{\HCode{<em>}\NoFonts}{\EndNoFonts\HCode{</em>}}
% handle the <a> tag inside sections

\catcode`\:=11

\def\Title:Link#1#2{}
\def\EndTitle:Link#1{}
% uncomment the following lines to get correct cross-references
%\LinkCommand\SectionLink{span,\noexpand\:gobble,id}
%\def\Title:Link{\SectionLink}
%\def\EndTitle:Link#1{\EndSectionLink}
\catcode`\:=12


\begin{document}
\EndPreamble

To handle section titles, we need to provide two configuration commands for each sectioning type:

 \Configure{sectionname}{at start of section}{at end of section}{section title}{end section title}
 \ConfigureMark{sectionname}{code that prints section number}

So to configure section, we need to use:

\Configure{section}{}{}{\blocktag{<h3>}\TitleMark}{\blocktag{</h3>}}
\ConfigureMark{section}{\thesection\ }

This removes all unnecessary formatting produced by TeX4ht.

Then we can fix paragraphs:

\Configure{HtmlPar}{\EndP\HCode{<p>}}{\EndP\HCode{<p>}}{\HCode{</p>}}{\HCode{</p>}}

This removes the comment with line numbers and information about indentation. The \EndP command inserts the closing tag for the previous paragraph.

I also provided some nicer formatting for \textbf and similar commands using:

\Configure{textbf}{\HCode{<b>}\NoFonts}{\EndNoFonts\HCode{</b>}}

The \NoFonts command will prevent insertion of <span class="cmbex"> etc. These tags are inserted every time you use non-default font. \NoFonts will prevent that. You need to use \EndNoFonts to turn it on again. If you don't want to use font information at all, you can disable it by adding NoFonts option to the \Preamble command, like:

 \Preamble{xhtml,NoFonts}

The last bit is the most controversial. The <a> element in section titles is inserted using the \Title:Link command. You can redefine it to discard the link. Because it uses the : in it's name it is also necessary to change \catcode of this character:

\catcode`\:=11
\def\Title:Link#1#2{}
\def\EndTitle:Link#1{}
\catcode`\:=12

With this configuration, you will get the following result with

tex4ebook -c mycfg.cfg sample.tex
 <h2>Chapter 1<br /> 
Lorem ipsum</h2>
<p>   Dolor sit amet consectetuer eros sit quis mauris pretium. Phasellus penatibus
interdum dolor Ut nisl.
   </p>
   <h3>1.1 Nam amet</h3>
<p>   Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum
amet.
</p><p>   Facilisi Nulla ultrices malesuada orci nibh eget ac Aliquam eros ut.
</p><p>
   </p>
   <h3>1.2 Lorem gravida</h3>
<p>   Oorci sociis Nunc id hendrerit at ac amet Pellentesque. Eleifend risus orci sem
Sed ac.
</p><p>   A nec pellentesque Pellentesque Morbi fringilla accumsan et metus at
enim.
</p><p>   Eu felis Curabitur quis nibh tellus.
   </p> 

If you want cross-references and TOC to work correctly, I would suggest to use the following configuration for `\Title:Link:

\LinkCommand\SectionLink{span,\noexpand\:gobble,id}
\def\Title:Link{\SectionLink}
\def\EndTitle:Link#1{\EndSectionLink}

The \LinkCommand defines new command that uses TeX4ht cross-referencing mechanism to produce links. Instead of the <a> element, this version produces <span>, \noexpand\:gobble removes the possible out link, and id holds destination for links that point to the section.

With this change, you will get the following result:

  <h2 id='lorem-ipsum'>Chapter 1<br /> 
<span id='x2-10001'>Lorem ipsum</span></h2>
<p>   Dolor sit amet consectetuer eros sit quis mauris pretium. Phasellus penatibus
interdum dolor Ut nisl.
   </p>
   <h3 id='nam-amet'>1.1 <span id='x2-20001.1'>Nam amet</span></h3>
<p>   Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum
amet.
</p><p>   Facilisi Nulla ultrices malesuada orci nibh eget ac Aliquam eros ut.
</p><p>
   </p>
   <h3 id='lorem-gravida'>1.2 <span id='x2-30001.2'>Lorem gravida</span></h3>
<p>   Oorci sociis Nunc id hendrerit at ac amet Pellentesque. Eleifend risus orci sem
Sed ac.
</p><p>   A nec pellentesque Pellentesque Morbi fringilla accumsan et metus at
enim.
</p><p>   Eu felis Curabitur quis nibh tellus.
   </p>   

Note that section now look like this:

  <h3 id='nam-amet'>1.1 <span id='x2-20001.1'>Nam amet</span></h3>

The <span id='x2-20001.1'>Nam amet</span> was added by the changed confituration, and id='nam-amet' was added by tex4ebook, to provide stable link destination based on the section title, instead of section position, which is more likely to change.

There is also some extra whitespace i paragraphs, which is generated from the whitespace in the DVI file. To get rid of this, I would use the DOM filters.

Simple DOM filter for this task could look like this:

local domfilter = require "make4ht-domfilter"

local function remove_space(node, regex)
  -- remove whitespace only from the text nodes
  if node and node:is_text() then
    node._text = node._text:gsub(regex, "")
  end
end

local filter = domfilter {
  function(dom)
    -- loop over <p> elements
    for _, p in ipairs(dom:query_selector("p")) do
      -- remove <p> elements without text
      local children = p:get_children()
      if #children < 2 and p:get_text():match("^%s*$") then
        p:remove_node()
      else
        local first = children[1]
        local last  = children[#children]
        remove_space(first, "^%s+") -- remove whitespace at the beginning
        remove_space(last, "%s+$") -- remove whitespace at the end of paragraph
      end
    end
    return dom
  end
}

Make:match("html$", filter)

You can require is using the -e option:

$ tex4ebook -c mycfg.cfg -e build.lua sample.tex

This is the result:

   <h2 id='lorem-ipsum'>Chapter 1<br /> 
<span id='x2-10001'>Lorem ipsum</span></h2>
<p>Dolor sit amet consectetuer eros sit quis mauris pretium. Phasellus penatibus
interdum dolor Ut nisl.</p>
   <h3 id='nam-amet'>1.1 <span id='x2-20001.1'>Nam amet</span></h3>
<p>Adipiscing est leo convallis nunc interdum Lorem hendrerit Vestibulum
amet.</p><p>Facilisi Nulla ultrices malesuada orci nibh eget ac Aliquam eros ut.</p>
   <h3 id='lorem-gravida'>1.2 <span id='x2-30001.2'>Lorem gravida</span></h3>
<p>Oorci sociis Nunc id hendrerit at ac amet Pellentesque. Eleifend risus orci sem
Sed ac.</p><p>A nec pellentesque Pellentesque Morbi fringilla accumsan et metus at
enim.</p><p>Eu felis Curabitur quis nibh tellus.</p>  
3
  • Thank you: I think I will need some time to digest your prosals ... which I certainly couldn't have worked out myself.
    – MS-SPO
    Commented May 16, 2022 at 16:16
  • @MS-SPO I hope I didn't overwhelm you :) You can find more information about configurations here: kodymirus.cz/tex4ht-doc/Configurations.html
    – michal.h21
    Commented May 17, 2022 at 9:48
  • It's fine with me. Having a choice I tend to be overwhelmed rather than underwhelmed :)
    – MS-SPO
    Commented May 17, 2022 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .