Blog

LaTeX Conditional Content: One Source for Print and E-book Publishing | LODE Publishing

Wed Feb 05 2025 00:00:00 GMT+0000 (Coordinated Universal Time)

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\ifebookthat 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 inmain.tex, but you create a smallebook.texthat sets the flag. Rather than using\inputto includemain.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 (forprint)\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 (fordigital)\documentclass[bibliography=totoc,open=right]{scrbook}\newif\ifebook\ebooktrue %Setto 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\ebooktruevs\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\ifebookcondition 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:\ifebookClick here to jump to the next chapter.\elseSee page \pageref{next-chapter}forthe 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\ifebookClick the chapter titleinthe tableofcontents\elseFind chapters listed on page \pageref{toc}\fi

3. Format-Specific Layout\ifebook\setlength{\parskip}{1em} % More spacingfore-readers\else\setlength{\parskip}{0.5em} % Tighter spacingforprint\fi

Building Different Versions

  1. For PDF (print version):
  • Overleaf automatically compilesmain.tex
  • Uses default\ebookfalse
  1. For EPUB (e-book version):
  • Triggered through latexmkrc
  • Sets\ebooktrueduring 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.

Get in Touch

We speak English (native), German (native), and French. Let's discuss your project!