Back to Home

LaTeX with CPP and Make for typesetting textbooks

The article describes a pipeline for typesetting textbooks with LaTeX, CPP, M4 and GNU Make. Examples of macros for images, listings, tables are provided. Analysis of project structure and preprocessor keys for UTF-8.

Typesetting textbooks in LaTeX with CPP M4 preprocessors
Advertisement 728x90

# Automating Textbook Layout: LaTeX with CPP and M4 Preprocessors

To automate the layout of textbooks or technical documentation, a combination of LaTeX, the CPP preprocessor, GNU Make, and utilities like M4 is used. This separates content from formatting, enables macros, and manages builds through Makefile. Key tools: pdflatex from MiKTeX, Cygwin for POSIX utilities, Eclipse as the editor, git for version control.

Required software set includes:

  • cpp — C preprocessor for conditional compilation and includes.
  • pdflatex — LaTeX compiler (path: C:\Program Files\MiKTeX\miktex\bin\x64\pdflatex.exe).
  • make — build orchestrator.
  • m4 — alternative preprocessor with scripting.
  • pandoc — converter to DOCX.
  • Additionally: realpath, sort, tree, grep, MiKTeX.

Checking pdflatex availability is critical for correct Cyrillic handling.

Google AdInline article slot

File Processing Pipeline

The build is structured as a pipeline: .texi (sources) → preprocessor (cpp/m4) → .tex → pdflatex → PDF. Sources (.texi, .mk, pix/) are versioned in git. Each chapter is a separate folder with .texi, .mk, and images.

Example .texi for a chapter:

sinclude(`latex_m4_preproc_utils.texi')

\graphicspath{ {PATH_CHAPTER_X_DIR/pix} }

\chapter{Name Glavy}

\section{Prolog}
khkhkhkhkhkhkhkhkhkhkhkhkh

\section{Paragraf_1}
khkhkhkhkhkhkhkhkhkh

\section{Itog}
khkhkhkhkhkhkhkhkhkhkhkh

\section{Giperssylki}

\begin{enumerate}
  \item \href{wwwwwwwwwwwwwwwww}{zzzzzzzzzzzz}
\end{enumerate}

GNU Make manages dependencies, generating .tex from .texi using cpp or m4.

Google AdInline article slot

Basic LaTeX Elements for Technical Texts

LaTeX automates numbering, indents, and formulas. Minimal set of constructs:

  • Images.
  • Quotes.
  • Code listings.
  • Tables.
  • Enumerations.
  • Hyperlinks.
  • Equations.
  • Table of contents.
  • Bibliography.

Inserting Images

Standard block:

\begin{figure}[h]
    \centering
    \includegraphics[width=0.99\textwidth]{arch}
    \caption{Harvard architecture }
    \label{fig:mesh1}
\end{figure}

CPP macro:

Google AdInline article slot
#define INSERT_PIX(FILE_NAME,HINT )  \
  \begin{figure}[h]  \
    \centering   \
    \includegraphics[width=0.99\textwidth]{FILE_NAME}   \
    \caption{HINT }  \
    \label{fig:FILE_NAME}  \
  \end{figure}

On M4:

define(INSERT_PIX2, format(
                           \begin{figure}[h!]  
                               \centering   
                               \includegraphics[width=0.75 \textwidth]{%s}   
                               \caption{%s }  
                               \label{fig:%s }  
                           \end{figure}
                           , $1, $2, $1
                          )     )

Quotes and Listings

Quote:

\begin{quote}
    "In any dele important define prioritety. 
    Inache vtorostepennoe, khotya and nuzhnoe, otnimet all sily 
    and not dasetcoyti to glavnogo."
\end{quote}

Listing (preamble setup lstset: breaklines=true, frame=single):

\begin{lstlisting}[label=some-code,caption=Ruleilnyy if]  
    int ret = NVRAM_Get(ID_IPv4_ROLE, tmp, 1, &tmp_len);
    if (MM_RET_CODE_OK != ret) {
        return ERROR_CODE_HARDWARE_FAULT;
    }
\end{lstlisting}

In C-code listings, escape # to avoid conflicts with the preprocessor.

Tables and Lists

Acronym table:

\begin{tabular} {ll}
    Akronim & Rasshifrovka \\
    \hline
    TUI & Text-based user interface \\
    CLI & command-line interface \\
    UART & universal asynchronous receiver / transmitter \\
    JTAG & Joint Test Action Group \\
    LED & light-emitting diode \\
    CIC & Cascaded integrator–comb \\
\end{tabular}

Numbered list:

\begin{enumerate}
    \item 
    \item 
    \item 
\end{enumerate}

Headers, References, and Preamble

Headers: \usepackage{fancyhdr}, \pagestyle{fancy}.

References: \usepackage[hidelinks]{hyperref}, \usepackage{cleveref}. Labels \label{} and \Cref{}.

Example:

\subsubsection{Upravlenie diskretnymi outputami}
\label{DOUTctrl}
...
\item Provide the ability to read discrete inputs; \Cref{DinRead}

Configuring CPP as a Universal Preprocessor

Keys for LaTeX:

| Option | Description |

|--------|-------------|

| -E | UTF-8 output |

| -P | No linemarkers |

| -C | Preserve comments |

| -traditional-cpp | Preserve whitespace |

| -nostdinc | No standard includes |

| -fexec-charset=UTF-8 | UTF-8 encoding |

| -DHAS_XXX | Macros |

| -undef | No GCC macros |

CPP is applicable for GraphViz, ASM, LD scripts.

Project Structure

Repository: folders by chapters (chapter.mk, chapter.texi, pix/), common: preamble.texi, library.mk.

Example tree:

.
├── about_author
│   ├── about_author.mk
│   ├── about_author.texi
│   └── pix
... (12 directories, 33 files)

Root .tex collects chapters via include.

Key Points

  • Separation of concerns: Author focuses on content, LaTeX on layout.
  • Preprocessing: CPP/M4 for macros, includes, conditional compilation.
  • Automation: GNU Make as dependency manager for chapters and images.
  • Cross-platform: Cygwin + MiKTeX for Windows.
  • Extensibility: Easy to add listings, formulas, bibliography.

— Editorial Team

Advertisement 728x90

Read Next