主要难点是涉及整个tikz
库多文件之间的相互关联。
提取一个「相对比较简单的」MWE如下,其中:
\input{pgfmanual-en-main-preamble.tex}
需要的代码来自./base/doc/pgfmanual-en-main-preamble.tex
- 需要使用
ltxdoc
文档类,其中定义了一些关键的命令和环境
截自v3.10(2025-06-22),该文件可在这里获得:pgfmanual-en-main-preamble.tex
\documentclass{ltxdoc}
\usepackage{tikz}
\usepackage{pgf}
\input{pgfmanual-en-main-preamble.tex}
\begin{document}
\begin{codeexample}[]
\begin{tikzpicture}
\foreach \x in {1,2,...,5,7,8,...,12}
\foreach \y in {1,...,5}
{
\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y) node{\x,\y};
}
\end{tikzpicture}
\end{codeexample}
\end{document}
当然,\input{pgfmanual-en-main-preamble.tex}
内部多达300行,还是不够简的。
纵览整个pgfmanual-en-main-preamble.tex
文件,其核心在于:
% Line 187
\input{pgfmanual-en-macros}
根据TSE上搜索的结果和kpathsea
的索引逻辑,该文件的位置位于./base/tex/latex/doc/pgfmanual-en-macros.tex
上面的文件调用了pgfmanual-en-macros.tex
,我们同时还发现:
% pgfmanual-en-macros.tex
% Line 1858
\usepackage{pgfmanual}
这将会调用./base/tex/latex/doc/pgfmanual.sty
文件:
% pgfmanual.sty
\ProvidesPackage{pgfmanual}[2009/10/15]
\input pgfmanual.code.tex
其中的\input pgfmanual.code.tex
会调用上面截图路径中的pgfmanual.code.tex
:
% pgfmanual.code.tex
\input pgfmanual.prettyprinter.code.tex
\input pgfmanual.pdflinks.code.tex
接下来只要用二分删除的方式,理论上可以只提取出「最轻量」的「仅仅用于codeexample
环境实现」的部分「最短代码」。
首先尝试二分删除简化「pgfmanual-en-main-preamble.tex
」,似乎仅有这两行是必不可少的:
\usepackage{calc,listings}
% \usepackage[version=latest]{pgf}
\input{pgfmanual-en-macros}
这里[version=latest]
会报错...懒得管,先注释这个选项即可...
非常初步简化的MWE如下:
\documentclass{ltxdoc}
\usepackage{tikz,pgf,calc,listings}
\input{pgfmanual-en-macros.tex}
\begin{document}
\begin{codeexample}[]
\begin{tikzpicture}
\foreach \x in {1,2,...,5,7,8,...,12}
\foreach \y in {1,...,5}
{
\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y) node{\x,\y};
}
\end{tikzpicture}
\end{codeexample}
\end{document}
换言之,下面的工作只要专心把pgfmanual-en-macros.tex
进一步做简化即可。
TBC.
问 如何实现 pgfmanual(pgf-tikz 官方手册)中代码与结果共存的效果('side-by-side' code examples)