返回首页

使用 LaTeX、CPP 和 Make 排版教科书

本文描述了使用 LaTeX、CPP、M4 和 GNU Make 排版教科书的流水线。提供了图像、列表、表格宏示例。分析项目结构和 UTF-8 预处理器键。

使用 CPP M4 预处理器在 LaTeX 中排版教科书
Advertisement 728x90

使用 CPP 和 M4 预处理器结合 LaTeX 自动化教科书布局

为了自动化教科书或技术文档的布局,使用了 LaTeX、CPP 预处理器、GNU Make 以及 M4 等工具的组合。这种方法将内容与格式分离,支持宏定义,并通过 Makefile 管理构建过程。主要工具包括:MiKTeX 中的 pdflatex、用于 POSIX 工具的 Cygwin、以 Eclipse 作为编辑器,以及 git 用于版本控制。

所需软件集包括:

  • cpp — 用于条件编译和包含的 C 预处理器。
  • pdflatex — LaTeX 编译器(路径:C:\Program Files\MiKTeX\miktex\bin\x64\pdflatex.exe)。
  • make — 构建协调器。
  • m4 — 带脚本功能的替代预处理器。
  • pandoc — 转换为 DOCX 的工具。
  • 另外:realpath、sort、tree、grep、MiKTeX。

检查 pdflatex 的可用性对于正确处理西里尔字母至关重要。

Google AdInline article slot

文件处理管道

构建过程设计为一个管道:.texi(源文件)→ 预处理器(cpp/m4)→ .tex → pdflatex → PDF。源文件(.texi、.mk、pix/)使用 git 进行版本控制。每个章节是一个单独的文件夹,包含 .texi、.mk 和图像。

章节示例 .texi 文件:

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 管理依赖关系,使用 cpp 或 m4 从 .texi 生成 .tex。

Google AdInline article slot

技术文本的基本 LaTeX 元素

LaTeX 自动处理编号、缩进和公式。最小构造集:

  • 图像。
  • 引文。
  • 代码列表。
  • 表格。
  • 枚举列表。
  • 超链接。
  • 公式。
  • 目录。
  • 参考文献。

插入图像

标准代码块:

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

CPP 宏:

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}

在 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
                          )     )

引文和代码列表

引文:

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

代码列表(前言设置 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}

在 C 代码列表中,需要转义 # 以避免与预处理器冲突。

表格和列表

缩写表:

\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}

编号列表:

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

页眉、引用和前言

页眉:\usepackage{fancyhdr}, \pagestyle{fancy}。

引用:\usepackage[hidelinks]{hyperref}, \usepackage{cleveref}。标签 \label{} 和 \Cref{}。

示例:

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

将 CPP 配置为通用预处理器

LaTeX 的关键选项:

| Option | Description |

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

| -E | UTF-8 输出 |

| -P | 无行标记 |

| -C | 保留注释 |

| -traditional-cpp | 保留空白 |

| -nostdinc | 无标准包含 |

| -fexec-charset=UTF-8 | UTF-8 编码 |

| -DHAS_XXX | 宏定义 |

| -undef | 无 GCC 宏 |

CPP 也适用于 GraphViz、ASM 和 LD 脚本。

项目结构

仓库:按章节分文件夹(chapter.mk、chapter.texi、pix/),公共部分:preamble.texi、library.mk。

示例树结构:

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

根 .tex 通过 include 收集章节。

关键点

  • 关注点分离:作者专注于内容,LaTeX 负责布局。
  • 预处理:CPP/M4 用于宏、包含和条件编译。
  • 自动化:GNU Make 作为章节和图像的依赖管理器。
  • 跨平台:Windows 下的 Cygwin + MiKTeX。
  • 可扩展性:易于添加代码列表、公式和参考文献。

— Editorial Team

Advertisement 728x90

继续阅读