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:
- Both files are nearly identical
- The only difference is
\ebooktrue
vs\ebookfalse
- The actual content lives in separate chapter files
- 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
- For PDF (print version):
- Overleaf automatically compiles
main.tex
- Uses default
\ebookfalse
- Overleaf automatically compiles
- 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
- Keep Conditionals Focused: Don't wrap huge sections in conditionals. Instead, break them into smaller, logical chunks.
- Document Your Choices: Comment your conditional blocks to explain why different content is needed.
- Test Both Formats: Regularly compile both versions to catch formatting issues early.
- Use Version Control: Keep track of your conditional changes using git or another version control system.
Common Pitfalls to Avoid
- Nested Conditionals: Avoid deeply nested conditional statements as they become hard to maintain.
- Format-Specific Features: Don't rely too heavily on format-specific features that might break in future updates.
- 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.