我在大学里用 LaTeX 写下讲座(这真的很方便),我希望
tex
文件在 pdf
中自动编译。
我的存储库中有几个
.tex
文件,如下所示:
.
├── .gitlab-ci.yml
└── lectures
├── math
| ├── differentiation
| | ├── lecture_math_diff.tex
| | ├── chapter_1.tex
| | └── chapter_2.tex
| └── integration
| ├── lecture_math_int.tex
| ├── chapter_1.tex
| └── chapter_2.tex
└── physics
└── mechanics
├── lecture_physics_mech.tex
├── chapter_1.tex
└── chapter_2.tex
So主文件,例如
lecture_math_diff.tex
使用
\include{chapter_1}
\include{chapter_2}
标签,组成整个讲座。
结果,我想像这样在 pdf 中构建我的工件:
├── math
| ├── lecture_math_diff.pdf
| └── lecture_math_int.pdf
└── physics
└── lecture_physics_mech.pdf
这里可以做什么?我是否必须编写任何
sh
脚本来收集所有tex
文件或使用gitlab runners?
一种方法是使用一个简短的脚本(例如 python 或 bash)并运行
latexmk
来生成 PDF 文件。
latexmk
是一个 perl 脚本,可以自动编译 latex 文件。可以在这里找到简短的介绍
使用 python3,脚本可能如下所示:
# filename: make_lectures.py
import os
from subprocess import call
# configuration:
keyword_for_main_tex = "lecture"
if __name__ == "__main__":
tex_root_directory = os.getcwd()
for root, _, files in os.walk("."):
for file_name in files:
# check, if file name ends with `tex` and starts with the keyword
if file_name.endswith("tex") and file_name.startswith(keyword_for_main_tex):
os.chdir(root) # go in the directory
call(['latexmk', '-lualatex', file_name]) # run latexmk on the main file
os.chdir(tex_root_directory) # go back to root directory in case of relative paths
此脚本假定,只有要编译为 PDF 的文件以关键字
lecture
(如问题中所示)开头。但是 if
语句,它检查要构建的文件,也可以扩展为更详细的比较作为匹配正则表达式。
latexmk
在此处使用命令行标志 -lualatex
调用,以演示如何全局配置构建过程。 .latexmkrc
文件提供了本地配置可能性(对于每个单个项目),这些文件由 latexmk
. 读取和处理
如果我们调用
latexmk
作为 shell 命令,我们必须确保它安装在我们的 gitlab runner 上(以及 texlive
)。如果 Docker
container runners 已注册(参见 here 如何完成),那么您只需要指定来自 DockerHub 的图像的名称,这将导致下面的示例 gitlab-ci.yml
文件:
compile_latex_to_pdf:
image: philipptempel/docker-ubuntu-tug-texlive:latest
script: python3 make_lectures.py
artifacts:
paths:
- ./*.pdf
expire_in: 1 week
随意,将图像更改为您喜欢的任何其他图像(例如
blang/latex:latest
)。请注意,工件提取假定存储库中没有其他 PDF 文件。
最后的评论:我没有尝试过,但也应该可以直接在 gitlab runner 上安装
texlive
和 latexmk
(如果你可以访问它的话)。
你可以看看https://github.com/reallyinsane/mathan-latex-maven-plugin。使用 maven 或 gradle 插件,您还可以为您的项目使用“依赖项”。