How to Build PDF and EPUB Versions of the AI Agent Book

You can compile the AI Agent Book locally into PDF using XeLaTeX via book/build_pdf.sh or into EPUB using Pandoc via build_epub.sh, with both formats generated from the same Markdown source files in language-specific directories like book/ or book-en/.

The bojieli/ai-agent-book repository hosts the complete source for Bojie Li's "AI Agents in Depth." The project includes automated build pipelines that transform Markdown chapters into publication-ready PDF and EPUB artifacts, supporting over a dozen languages including English, Chinese, and Arabic.

Building the PDF Version

The PDF generation relies on the ElegantBook LaTeX class and XeLaTeX to produce a professionally typeset document with proper CJK font support.

Required Tools and Dependencies

Before running the build script, install the following dependencies:

  • Pandoc for Markdown-to-LaTeX conversion
  • XeLaTeX and the ElegantBook LaTeX class
  • librsvg (provides rsvg-convert for SVG handling)
  • CJK-compatible fonts including Songti SC and Heiti SC

Running the PDF Build Script

Navigate to the book directory and execute the shell script:

cd book
bash build_pdf.sh

The script defined in book/build_pdf.sh assembles the chapter sequence—including introduction.md, chapter1.md through chapter10.md, and afterword.md—into a single LaTeX document. It applies the custom preamble from book/preamble.tex and optionally inserts a cover page from book/cover.tex.

PDF Output Verification

Upon completion, the script generates 深入理解‑AI‑Agent‑李博杰‑v2.0.pdf in the same directory and reports the final file size and page count for verification.

Building the EPUB Version

The EPUB pipeline uses Pandoc to convert Markdown into EPUB3 format with MathML support and automated table-of-contents generation.

Required Tools for EPUB

Install these prerequisites:

  • Pandoc
  • pdftoppm (from the poppler utilities)
  • Python 3
  • epubcheck (optional, for validation)

Language Selection and Build Commands

The master script build_epub.sh supports both single-language and bulk builds. For a specific language, pass the language code as an argument:

./build_epub.sh en

To build all supported languages (Chinese, English, Spanish, Indonesian, Russian, Tamil, Vietnamese, Turkish, Korean, Hungarian, and Hebrew):

./build_epub.sh

Note that Japanese and Arabic are supported but excluded from the default "all" loop and must be built individually by passing ja or ar explicitly.

EPUB Generation Process

As implemented in build_epub.sh, the build process executes the following steps:

  1. Validation: Checks that the language-specific directory (e.g., book-en/) contains the required Markdown files and corresponding PDF cover.
  2. Cover Generation: Rasterizes the first page of the PDF into a JPEG cover image using pdftoppm.
  3. Conversion: Invokes pandoc with the epub_external_links.lua filter to rewrite external links for EPUB compliance, enabling MathML equations and syntax highlighting.
  4. Post-processing: For right-to-left languages (Arabic, Hebrew), the Python helper flatten_epub_toc.py flattens the table of contents structure.
  5. Validation: If available, epubcheck validates the final EPUB file.

The resulting files follow the naming convention AI-Agents-in-Depth-Bojie-Li-v2.0-{lang}.epub.

Key Build Files Reference

Understanding these specific files helps customize the build process:

  • book/build_pdf.sh: Main compilation script for Chinese PDF generation.
  • build_epub.sh: Master orchestration script for all language EPUBs.
  • book/preamble.tex: LaTeX configuration defining ElegantBook settings and font packages.
  • epub_external_links.lua: Pandoc Lua filter ensuring URL compatibility in electronic formats.
  • flatten_epub_toc.py: Post-processing script handling RTL layout requirements.

Summary

  • PDF builds use XeLaTeX with the ElegantBook class via book/build_pdf.sh, requiring CJK fonts and librsvg tools.
  • EPUB builds use Pandoc via build_epub.sh, supporting 12+ languages with automatic cover generation and MathML rendering.
  • Both formats source content from Markdown files in book/ or book-<lang>/ directories.
  • The build system includes specialized handling for right-to-left languages and optional EPUB validation.

Frequently Asked Questions

What prerequisites are required to build the AI Agent Book locally?

You need Pandoc, XeLaTeX, the ElegantBook LaTeX class, librsvg, and CJK fonts (Songti SC, Heiti SC) for PDF generation. For EPUB builds, you additionally need pdftoppm from poppler and Python 3. Optional validation requires epubcheck.

Can I generate the book in languages other than English and Chinese?

Yes. The build_epub.sh script supports automated builds for Spanish, Indonesian, Russian, Tamil, Vietnamese, Turkish, Korean, Hungarian, and Hebrew. Japanese and Arabic are supported but must be built individually by passing ja or ar as explicit arguments.

How does the EPUB build handle right-to-left languages like Arabic and Hebrew?

The build script detects RTL languages and invokes flatten_epub_toc.py to post-process the EPUB table of contents. This Python utility flattens the hierarchical navigation structure to ensure proper rendering in e-book readers that support bidirectional text.

Where are the source Markdown files located?

The primary Chinese source resides in the book/ directory, containing files like introduction.md, chapter1.md through chapter10.md, and afterword.md. Translated versions live in language-specific directories such as book-en/, book-es/, and book-ja/, each containing the equivalent Markdown structure for that locale.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →