By
Clemens Lode
,
February 5, 2025
Electronic document is pushed into a tablet.

Using LaTeX Conditionals for Print and Digital Books: A Complete Guide

If you're publishing both print and e-book versions of your book, maintaining two separate manuscripts quickly becomes a nightmare. In this guide, I'll show you how to use LaTeX conditionals to maintain a single source document that automatically generates the right content for each format.

Understanding the Problem

Traditional publishing often requires different content for print and digital versions. For example:

  • Print books use "see page 123" while e-books need "click here"
  • Print books need high-resolution images while e-books need web-optimized ones
  • E-books might include interactive elements that aren't possible in print

Setting Up Your LaTeX Environment

First, we need to set up a conditional switch that tells LaTeX which format we're building. Add this near the top of your main document:

\newif\ifebook
\ebookfalse  % Default to print version

This creates a new conditional called \ifebook that defaults to the print version.

Creating Format-Specific Entry Points

Instead of modifying your main file each time you want to switch formats, create two entry points. Your main content stays in main.tex, but you create a small ebook.tex that sets the flag. Rather than using \input to include main.tex, you'll need two separate but nearly identical entry point files. While this means having two files, the actual content still lives in separate chapter files that both entry points include.

% main.tex (for print)
\documentclass[bibliography=totoc,open=right]{scrbook}
\newif\ifebook
\ebookfalse  % Default to print version

\begin{document}
\input{chapters/chapter1}
\input{chapters/chapter2}
% etc...
\end{document}

% ebook.tex (for digital)
\documentclass[bibliography=totoc,open=right]{scrbook}
\newif\ifebook
\ebooktrue  % Set to e-book version

\begin{document}
\input{chapters/chapter1}
\input{chapters/chapter2}
% etc...
\end{document}

The key points are:

  1. Both files are nearly identical
  2. The only difference is \ebooktrue vs \ebookfalse
  3. The actual content lives in separate chapter files
  4. Both main files include the same chapter files

While having two main files might seem like duplication, it's necessary because the \ifebook condition needs to be set before any content is processed. This ensures that all conditional content is correctly handled during compilation.This structure gives you:

  • Clean separation of content and format settings
  • Reliable conditional processing
  • Easy maintenance (just remember to keep both main files in sync for any preamble changes)

Using Conditionals in Your Content

Now comes the fun part. Whenever you need format-specific content, wrap it in conditional blocks:

\ifebook
    Click here to jump to the next chapter.
\else
    See page \pageref{next-chapter} for the next chapter.
\fi

Common Use Cases

1. Image Resolution

\ifebook
    \includegraphics[width=0.8\textwidth]{lowres-image}
\else
    \includegraphics[width=0.8\textwidth]{highres-image}
\fi

2. Navigation References

\ifebook
    Click the chapter title in the table of contents
\else
    Find chapters listed on page \pageref{toc}
\fi

3. Format-Specific Layout

\ifebook
    \setlength{\parskip}{1em}  % More spacing for e-readers
\else
    \setlength{\parskip}{0.5em}  % Tighter spacing for print
\fi

Building Different Versions

  1. For PDF (print version):
    • Overleaf automatically compiles main.tex
    • Uses default \ebookfalse
  2. For EPUB (e-book version):
    • Triggered through latexmkrc
    • Sets \ebooktrue during tex4ebook compilation
    • Creates EPUB with e-book-specific content

In the latexmkrc file of your Overleaf project, add the following line to compile your project to an EPUB file:

system("tex4ebook -j output -f epub3 ebook.tex");

Best Practices

  1. Keep Conditionals Focused: Don't wrap huge sections in conditionals. Instead, break them into smaller, logical chunks.
  2. Document Your Choices: Comment your conditional blocks to explain why different content is needed.
  3. Test Both Formats: Regularly compile both versions to catch formatting issues early.
  4. Use Version Control: Keep track of your conditional changes using git or another version control system.

Common Pitfalls to Avoid

  1. Nested Conditionals: Avoid deeply nested conditional statements as they become hard to maintain.
  2. Format-Specific Features: Don't rely too heavily on format-specific features that might break in future updates.
  3. Inconsistent Testing: Always test both formats when making changes.

ConclusionUsing LaTeX conditionals lets you maintain a single source document while producing optimized versions for both print and digital formats. The initial setup takes some time, but the long-term benefits in maintainability and consistency are worth it.Remember, the key is to think about your content's presentation in both formats from the start. Plan your conditional blocks carefully, and you'll create a flexible document that works beautifully in any format.

Summary

One of the biggest challenges in technical publishing is maintaining both print and digital versions of your book. This guide shows you how to use LaTeX conditionals to generate both formats from a single source, saving time and reducing errors.

What You'll Learn

  • How to set up LaTeX conditionals for print and e-book formats
  • Creating format-specific entry points in LaTeX
  • Automating the build process for both PDF and EPUB output
  • Best practices for maintaining conditional content

Who This Is For

Technical authors, academics, and publishers who use LaTeX and need to produce both print and digital versions of their books.

Related Books and Services

Recommended Further Reading

February 5, 2025

About the Author

Clemens Lode

Hello! My name is Clemens and I am based in Düsseldorf, Germany. I’m an author of books on philosophy, science, and project management, and coach people to publish their books and improve their approach to leadership.

I like visiting the gym, learning to sing, observing animals, and creating videos on science and philosophy. I enjoy learning from nature and love the idea of optimizing systems.

In my youth, I was an active chess player reaching the national championship in Germany, and an active pen&paper player leading groups of adventurers on mental journeys. These activities align with my calm approach to moderating meetings, leading meetups, and focusing on details. My personality type in socionics is IEE/ENFp.

Read more...
Clemens Lode

Related Blog Posts

Related Topics

Book Publishing

Book Publishing

Book publishing has evolved with POD (print-on-demand) services. While it's never too late to write the next great novel or an award-winning nonfiction book, many people see book publishing as a marketing tool. No matter what your approach to your book, the challenge is finding and writing for the right audience.

Read more...
LaTeX

LaTeX

LaTeX, a document processing system, creates a typeset finished product. The system works more like a compiler than a word processor. While initially complicated to learn, LaTeX allows better management of larger projects like theses or books by splitting the document into text, style, and references. Leslie Lamport created laTeX in the 1980s; his goal was to separate content from styling.

Read more...

Do you have a question about our services?

Reach out, we'd love to hear from you! Schedule a video chat or message us by e-mail or WhatsApp!
We speak English (native), German (native), and French, but our template supports all languages!

Send us an e-mail (mail@lode.de), we will reply as soon as possible!

Reach out to us via a chat on WhatsApp!

Let's talk! Set up a free call with Clemens to discuss our services. Use Calendly to schedule the call.

Or send us your question or comment here and we'll get back to you ASAP:

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Man signs up for the newsletter on his smartphone and laptop.

Sign Up for the Newsletter

Get notified about new book releases, major template updates, and new services (less than once per month).

We use Kit.com as our marketing platform. By clicking Submit, you acknowledge that your information will be transferred to Kit.com for processing. Learn more about Kit.com's privacy practices here.