Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile multiple LaTeX files w/ GitLab CI

Problem

I write down lectures at university in LaTeX (which is really convenient for this purpose), and i want tex files to automatically compile in pdf.

I have couple of .tex files in my repository like this:

.
├── .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 main file, for example, lecture_math_diff.tex using

\include{chapter_1}
\include{chapter_2}

tags, to form whole lecture.

And as result, i want to have my build artifacts in pdf like this:

├── math
|   ├── lecture_math_diff.pdf
|   └── lecture_math_int.pdf
└── physics
    └── lecture_physics_mech.pdf

What can be done here? Do i have to write any sh script to collect all tex files or use gitlab runners?

like image 868
murych Avatar asked Oct 19 '22 02:10

murych


2 Answers

One approach would be to use a short script (e.g python or bash) and to run latexmk to generate the PDF files.

latexmk is a perl script, which compiles latex files automatically. A short introduction can be found here

With python3 the script could look like the following one:

# 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[-3:] == "tex" and file_name[0:len(keyword_for_main_tex)] == keyword_for_main_tex:
                os.chdir(root) # go in the direcotry
                os.system("latexmk -lualatex "+ file_name) # run latexmk on the mainfile
                os.chdir(tex_root_directory) # go back to root directory in case of relative pathes

This script assumes, that only files to be compiled to PDF start with the keyword lecture (as in the question). But the if statement, which checks for files to build, could also be extended to more elaborate comparison as matching regular expressions. latexmk is called with the command line flag -lualatex here to demonstrate how to configure the build process gloally. A local configuration possibility (for each single project) is given with .latexmkrc files, which are read and processed by latexmk.

If we call latexmk as shell command, we have to make sure, that it is installed on our gitlab runner (and also texlive). If Dockercontainer runners are registered (see here how it is done), then you just need to specify the name of an image from DockerHub, which leads to the example gitlab-ci.yml file below:

compile_latex_to_pdf:
  image: philipptempel/docker-ubuntu-tug-texlive:latest
  script: python3 make_lectures.py
  artifacts:
    paths:
    - ./*.pdf
    expire_in: 1 week

Fell free, to change the image to any other image you like (e.g. blang/latex:latest). Note, that the artifacts extraction assumes, that no other PDF files are in the repository.

A final remark: I did not try it, but it should also be possible to install texlive and latexmk directly on the gitlab runner (if you have access to it).

like image 109
Chris H. Avatar answered Oct 20 '22 22:10

Chris H.


You can have a look at https://github.com/reallyinsane/mathan-latex-maven-plugin. With the maven or gradle plugin you can also use "dependencies" for your projects.

like image 41
reallyinsane Avatar answered Oct 20 '22 23:10

reallyinsane