25

How to do I build my LaTeX files with TeX Live using the continuous integration server Travis CI to which I push with git, especially considered that I do not want to download everything of TeX Live every time?

I tried putting this into my .travis.yml

before_install:
- sudo apt-get update
- sudo apt-get install texlive-latex-extra
# Initialize some directories before installing packages
- sudo mkdir /home/travis/texmf
- sudo tlmgr init-usertree
- sudo tlmgr install stmaryrd # Test installing a package not included by default
script:
- mkdir _build
# Work with a timout of 3 minutes
- travis_wait 3 pdflatex -output-directory _build ./src/nameofmytexfile.tex

But it gave me

Unknown directive ...containerchecksum (...) , please fix it! at /usr/share/texlive/tlpkg/TeXLive/TLPOBJ.pm line 210, <$retfh> line 5761.

I would prefer to use pdflatex since I also use that on my local machine.

Related

My script was a bit inspired by How do I install an individual package on a Linux system?

where I found how to install packages as hinted at in the Travis documentation and from tlmgr cannot setup TLPDB

A simpler script without installing extra packages is in Continuous integration with travis -- package.sty not found and the answer contains a link to the latex3 scripts, but they work with LuaTeX and not with pdflatex. Something similar is found through this answer: https://tex.stackexchange.com/a/197587/98850

Questions for other continuous integration services

For a similar question for GitLab CI (thanks to @JBantje for creating it), see Compiling (La)TeX files automatically with GitLab CI

  • I once used a similar setup, but switched to using GitLab’s integrated Continuous Integration feature (with my home server as the “runner”). The CI jobs are handled a lot faster than with Travis+Docker and I have full control of the TeXlive installation. If anyone is interested, I’m happy explain more :) – JBantje Nov 9 '18 at 21:12
  • 1
    @JBantje Yes, please add an answer! Also, it looks like GitLab also offers free CI builds on their servers? So even though I don't have a server available at the moment to install GitLab on, maybe it can still be made to work on GitLab's servers. – PHPirate Nov 10 '18 at 10:56
  • 1
    Posted this as a Q&A here: tex.stackexchange.com/questions/459484/… (since it would be an off-topic answer here) – JBantje Nov 11 '18 at 16:35
  • @JBantje Thanks a lot! Agree that it's much better to keep these separate. – PHPirate Nov 11 '18 at 17:09
  • 1
    @bluprince13 Thanks for writing that out! Although this question is about Travis CI, but maybe you could leave a comment/answer at the question about GitLab here: tex.stackexchange.com/questions/459484/… . Also thanks for mentioning the other blog post, I hadn't seen it before. – PHPirate Jan 18 at 6:32
22

This answer contains a summary of two answers: one using Tectonic and Docker, and one using TeX Live and pdflatex. But there are much more options, for the complete guide including advantages/disadvantages see this repository: github.com/PHPirates/travis-ci-latex-pdf.

1. Steps for building LaTeX using Docker and Tectonic

Tectonic is a LaTeX enginge (compiler) which downloads the needed packages automatically, as well as compiling as much times as needed taking BibTeX into account. Can also work with biber.

Docker provides the ability to quickly download a pre-installed Tectonic to the Travis server.

  1. Enable your repo for Travis: install the Travis GitHub App to your GitHub account by going to the Marketplace, scroll down, select Open Source (also when you want to use private repos) and select 'Install it for free', then 'Complete order and begin installation'.
  2. Now you should be in Personal settings | Applications | Travis CI | Configure and you can allow access to repositories, either select repos or all repos.
  3. Copy config file below to your repo, specify the tex file you want to compile, possibly change the directory your tex file is in after $TRAVIS_BUILD_DIR if it is not src/.
  4. commit and push, view your builds at travis-ci.com.

file .travis.yml:

sudo: required
language: generic
services: docker

script:
  # We use the docker image from https://hub.docker.com/r/dxjoke/tectonic-docker/
  - docker pull dxjoke/tectonic-docker
  # Compiling only main.tex:
  - docker run --mount src=$TRAVIS_BUILD_DIR/src,target=/usr/src/tex,type=bind dxjoke/tectonic-docker /bin/sh -c "tectonic main.tex"
  # Compiling multiple files as well as using biber:
#  - docker run --mount src=$TRAVIS_BUILD_DIR/src,target=/usr/src/tex,type=bind dxjoke/tectonic-docker /bin/sh -c "tectonic --keep-intermediates --reruns 0 biber-mwe.tex; biber biber-mwe; tectonic biber-mwe.tex; tectonic main.tex"

Tip: following this post Malcolm Ramsay has written up a pretty complete blog post for a similar use case.

Note that it is also possible to for example automatically deploy a pdf to your GitHub releases page when you tag a git commit, instructions in the overview repository github.com/PHPirates/travis-ci-latex-pdf.

2. Steps for building LaTeX using pdflatex and TeX Live

I adapted the LaTeX3 build file for use with pdflatex. A disadvantage of pdflatex is that a bigger TeX Live scheme (basic instead of infraonly) needs to be installed, because that includes pdflatex. On the other hand, Travis allows caching so you don't download it every time. Note that you could also use TeX Live and pdflatex from a Docker image, see the answer from Strauman.

If you use more packages, just add them in the file below. Also holds for .cls files.

texlive_install.sh:

#!/usr/bin/env sh

# Originally from https://github.com/latex3/latex3

# This script is used for testing using Travis
# It is intended to work on their VM set up: Ubuntu 12.04 LTS
# A minimal current TL is installed adding only the packages that are
# required

# See if there is a cached version of TL available
export PATH=/tmp/texlive/bin/x86_64-linux:$PATH
if ! command -v texlua > /dev/null; then
  # Obtain TeX Live
  wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
  tar -xzf install-tl-unx.tar.gz
  cd install-tl-20*

  # Install a minimal system
  ./install-tl --profile=../texlive/texlive.profile

  cd ..
fi

# Just including texlua so the cache check above works
# Needed for any use of texlua even if not testing LuaTeX
tlmgr install luatex

# Other contrib packages: done as a block to avoid multiple calls to tlmgr
# texlive-latex-base is needed to run pdflatex
tlmgr install   \
  exam          \
  amsfonts      \
  stmaryrd      \
  amsmath

# Keep no backups (not required, simply makes cache bigger)
tlmgr option -- autobackup 0

# Update the TL install but add nothing new
tlmgr update --self --all --no-auto-install

texlive/texlive.profile:

selected_scheme scheme-basic
TEXDIR /tmp/texlive
TEXMFCONFIG ~/.texlive/texmf-config
TEXMFHOME ~/texmf
TEXMFLOCAL /tmp/texlive/texmf-local
TEXMFSYSCONFIG /tmp/texlive/texmf-config
TEXMFSYSVAR /tmp/texlive/texmf-var
TEXMFVAR ~/.texlive/texmf-var
option_doc 0
option_src 0

.travis.yml:

install:
 - source ./texlive_install.sh
cache:
  directories:
    - /tmp/texlive
    - $HOME/.texlive
script:
- mkdir _build
# Prefix command with travis_wait x so it times out after 3 mins
- travis_wait 3 pdflatex -output-directory _build ./src/nameofmytexfile.tex

Note that it is also possible to for example automatically deploy a pdf to your GitHub releases page when you tag a git commit, instructions in the overview repository github.com/PHPirates/travis-ci-latex-pdf.

Changelog

Update October 2018 @WtfJoke amended @rekka's Docker image to work with biber, updated incstructions

Update July 2018 @rekka provides a Docker image with Tectonic which makes the build faster and the build file much shorter, instructions added.

Update May 2018 Travis is migrating open-source from travis-ci.org to travis-ci.com, and they have introduced a GitHub App. Instructions are updated.

Update Jan 2018 I discovered the tectonic engine, added instructions.

  • 1
    Note that Tectonic is a fork of the XeTeX engine (and incidentally one that strangely starts from the unreadable converted C source rather than xetex.web), so it doesn't satisfy the “prefer pdflatex” condition. Nevertheless, if it works for you (as it evidently does) then great. It's unfortunate that there isn't a cleaner alternative within the mainstream TeX ecosystem. – ShreevatsaR Feb 8 '18 at 17:10
  • @ShreevatsaR Thanks for the info, I didn't know that! Yup, so in some cases I need to revert to the pdflatex answer. That's why I didn't remove that part :) – PHPirate Feb 8 '18 at 17:10
  • 1
    @AndreasStorvikStrauman That would be great! I think it would be cool if you would want to take time to write up an answer yourself. It makes more sense to me that different methods are different answers, so people can vote up the best one. I didn't split my answer up into different answers as I'm not sure if everyone would agree with that... they might think I would just be doing it to get extra reputation. But this is just a summary and partly duplicate of what's in my overview repo anyway (code duplication...) – PHPirate Sep 10 '18 at 12:39
  • 1
    @Joker The image just runs tectonic, which should run bibtex as much as needed automatically. It does not support biber yet, but you have at least these options: 1. You could use tectonic with miniconda which does work with biber, see here 2. You could derive your own docker image from the one by rekka which works with biber, using the code from the Miniconda method. – PHPirate Sep 19 '18 at 11:03
  • 1
    @Joker It's slower but not much, reference build time for my test file was under a minute for the docker image and 1-2 minutes for the Miniconda method. Information like this is in the readme I linked to, with advantages and disadvantages and stuff. – PHPirate Sep 19 '18 at 11:25
4

LaTeX is not supported on Travis-CI but R is community-maintained. And since R uses LaTeX to run some vignettes and build manuals, you can access LaTeX through R and build it that way. With Yihui Xie's tinytex package, building a LaTeX document involves a pretty simple script.

For example, here's the .travis.yml file from a dummy repository which builds the first .tex file using pdflatex and then copies the PDF as a release:

language: r
sudo: true
latex: false
pandoc: false
warnings_are_errors: false

install: echo "Nothing occurs at installation, only script"
script:
- Rscript install_texlive.R
- Rscript -e 'tinytex::pdflatex(list.files(pattern = "\\.tex$")[1], bib_engine = "biber")'
branches:
  - master
  - travising
deploy:
  provider: releases
  skip_cleanup: true
  api_key:
    secure: <generated by `travis setup releases`>
  file: Report.pdf
  on:
    repo: HughParsonage/latex-travis

install_texlive.R

if (!requireNamespace("tinytex", quietly = TRUE)) {
    if (!requireNamespace("devtools", quietly = TRUE)) {
        install.packages("jsonlite", repos = "https://cran.rstudio.com/", quiet = TRUE)
        install.packages("httr", repos = "https://cran.rstudio.com/", quiet = TRUE)
        install.packages("memoise", repos = "https://cran.rstudio.com/", quiet = TRUE)
        install.packages("devtools", repos = "https://cran.rstudio.com/", quiet = TRUE)
        cat('devtools installed\n')
    }
  devtools::install_github(c('yihui/tinytex'), quiet = TRUE)
  tinytex::install_tinytex()
}
  • Interesting, didn't know that was included in R packages. So am I right in saying that the big pro of this is that tinytex handles automatic install of packages and still uses pdflatex? But the big con seems that build times are long, 5-8 minutes compared to 1-1.5 minute for my pdflatex build, not sure if caching would help? If the size of everything is not hundreds of mb's... – PHPirate Feb 11 '18 at 21:00
  • The pro is a simple .travis.yml file that should just work on Travis-CI whatever packages you need (though you do have to specify the bib engine). Caching helps a bit with the R side -- most of the time spent building is due to the installation of R and its packages, you could usecache: packages. But if you're after a speedy build and you're using the same class files and packages, you could probably just use the script linked in: tex.stackexchange.com/questions/328880/… – Hugh Feb 12 '18 at 1:47
  • Oh haha I only now see that that was your question, somehow I had seen that but had missed your GitHub repo. So all credits to Joseph :) I'll also refer to this variation in my readme if you don't mind, thanks again for letting me know. – PHPirate Feb 12 '18 at 9:10
  • I don't see the pdf in the link you provided, and the code doesn't produce a pdf for me either. Is the solution still valid? – Dambo Sep 4 '18 at 3:48
  • Which link? The solution should still be valid. – Hugh Sep 4 '18 at 3:51
3

November 2018:

Build LaTeX using TeX Live from a Docker image

Minimal example

Use the following .travis.yml:

sudo:       required
language:   generic
services:   docker

tex-config:
- build-pattern=src/main.tex,src/mwe.tex
- packages=cancel, exam

script:
- docker run --mount src=$TRAVIS_BUILD_DIR/,target=/repo,type=bind strauman/travis-latexbuild:small

Complete answer

I made a version, which is available through this git repo and accompanying this docker. This is the quickstart:

After following the setup below, then your repository has the following functionality

  1. You push to a branch
  2. travis-ci automatically installs the packages specified in the config file, and runs tests the pdflatex via latexmk on the TeX files specified in the config file.
    • If the directory containing the .tex file has a file named wants-fail, then travis only succeeds if the .tex-file build fails.

Note: If you want travis to push a branch containing the built pdfs, check out the instructions on the master branch

Setup:

In your repo, you need two files from this branch: the .travis.yml-file and the .travis/tex-config.ini.

NB: You also have to add travis-ci.org to your repo, and your repo to travis-ci.org.

  1. Copy the .travis.yml file from this repo to the root of your git repository
  2. Copy the .travis/tex-config.ini file from this repo to .travis/tex-config.ini in your repo. Make sure to add packages in this file (after the packages= line: comma sepearated)
  3. Profits

Configure the .travis/tex-config.ini to your needs. For more advanced options (e.g. push pdfs back to git repo after tests) see the instructions in the master branch. The master branch also has in depth configuration reference.

NB if you want to use more advanced features (push back to git or a different TeX scheme) you MUST use the .travis.yml from the master branch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.