I am using the tex4ht package to compile EPUB documents with LaTeX. However, I am also using the same base .tex files to compile PDF documents with pdflatex. Since there are some significant differences between how EPUB and PDF files work and compile, I've found myself wishing for functions that allow me to switch certain text fragments on and off based on some global variables (i.e. whether I compile it as an EPUB or a PDF).

Use Case 1:

I use imakeidx for generating indices. When I compile an index with PdfLaTeX, it "merges" several instances of \index that appear on the same page. Thus, an index entry could appear as:

walrider, 49-61, 142

I thus have been fairly "generous" with my \index placements, putting them in every new paragraph where a certain word appears.

However, when I compile an index with tex4ebook (using tex4ht package) it doesn't have distinct pages (since, of course, the output is a reflowing text format). Instead, every single instance shows up in the index:

walrider, 693, 695, 701, 708, 719, 734, 736, 742, 743, 747, 752, 763, 777, 792, 794, 796, 798, 802, 804, 819, 828, 837, 840, 848, 855, 867, 877, 880, 915, 920, 935, 946, 956, 960, 961, 983, 1003, 1006, 1009, 1012, 1017, 1021, 1029, 1030, 2683

Which is making the index excessively long. Thus, it would be useful if I could make some of these \index instances disappear - but only when I compile it with tex4ebook (or set some global variable).

Use case 2:

While both the PDFs and the EPUB files I am compiling at the moment are hyperlinked, eventually I want to prepare a PDF version for Print On Demand - which of course will not have any hyperlinks. Thus, I need to insert page references at strategic locations. Now, LaTeX can easily do page references:

(see p. \ref{walrider})

But I only need these page references when compiling the PDF version for Print On Demand, so again some kind of function would be useful which allowed me to set a global variable that decides if these page references are actually compiled or not.

Since I don't have much experience with writing new LaTeX, I was wondering what the most elegant way of doing this is.

2 Answers 2


To solve your first problem, I would define two commands - one for important terms, which should be used when you want the index entry even in the Epub file, the second for all instances you want in the PDF version.

The second issue can be solved using multiple TeX files - separate file for the print on demand, another for other uses. Document text can be in another file that would be included by the driver files.

Here is an example:

Main driver for electronic formats:


Print on demand driver (see the redefined \printref command):

\renewcommand\printref[1]{\ (see p.~\pageref{#1})}

Both drivers use custom package myindex.sty:


For the Epub version, we need to redefine \secondaryindex to print nothing. Save the following as myindex.4ht:


Sample text.tex:

Helo world \primaryindex{Hello}\secondaryindex{Hello}\secondaryindex{Hello}

More text\printref{sec:hello}.


This is the print on demand version:

enter image description here

And this HTML:

enter image description here


Not a precise answer, but too long for a comment:

Without getting involved in your specific problems, my approach will be indeed use rmarkdown or bookdown (rmarkdown for books), because:

  1. Rmarkdown syntax is easier and it can be converted, among others formats, to epub, LaTeX or PDF via LaTeX.

  2. It can include raw html chunks that are ignored when converted to PDF (via LaTeX)

  3. It can include raw LaTeX chunks that are ignored when exported to HTML (or EPUB).


  bookdown::epub_book: null
  html_document: default
  pdf_document: default

*test1* <i>test2</i> \emph{test3}

The first and second outputs should produce:

test1 test2

Whereas the PDF output should be:

test1 test3

You must log in to answer this question.

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