[Tex/LaTex] How to suitably configure the TEXINPUTS variable in latexmkrc for detecting the sty and cls files present in a custom folder in the project directory

latexmkpackagespaths

I am working on a project where in the .cls and .sty files installed by the TeX distributions are too old, but they have been patched by other users on github. I wish to use these updated versions of these .cls and .sty files for my project. For various personal reasons (easy portability across computers), I do not wish to use texmf-local tree and wish to put them in a folder titled custom_cls_sty_files in the root of my project directory.

I am using latexmk as my build tool. Following the solution proposed here, I tried configuring my latexmkrc as

$ENV{'TEXINPUTS'}='./custom_cls_sty_files//:' . $ENV{'TEXINPUTS'};

However, this broke everything. latexmk is not only unable to find the .sty and .cls files, it could not find even main.tex!

Here is an mwe of main.tex

\documentclass{article}
\usepackage{setspace}
\usepackage{blindtext}

\begin{document}
    \doublespacing
    \blinddocument
\end{document}

where a local copy of setspace.sty is located in the folder custom_cls_sty_files.

Presently, I am encountering this issue on a Windows 10 machine on TL 2018. But I have a mac laptop at home and a linux machine at work where I work on this project through git/github combo. Therefore, I would like a single cross-platform latexmkrc file that can solve this issue (i.e. assuming that this issue arose in the first place due to OS differences)

Error message when trying the suggested solution

Use of uninitialized value in concatenation (.) or string at (eval 17) line 59, <GEN0> chunk 1.
Latexmk: This is Latexmk, John Collins, 25 October 2018, version: 4.61.
Latexmk: applying rule 'lualatex'...
Rule 'lualatex': Rules & subrules not known to be previously run:
   lualatex
Rule 'lualatex': The following rules & subrules became out-of-date:
      'lualatex'
------------
Run number 1 of rule 'lualatex'
------------
------------
Running 'lualatex  -recorder  "main.tex"'
------------
This is LuaTeX, Version 1.07.0 (TeX Live 2018/W32TeX)
 restricted system commands enabled.
! I can't find file `main.tex'.
<*> main.tex

(Press Enter to retry, or Control-Z to exit)
Please type another input file name:

Best Answer

Latexmk actually has a subroutine for adding items to search paths. You can write

ensure_path( 'TEXINPUTS', './custom_cls_sty_files//' );

This takes care of using the correct operating-system-dependent separator between items in the variable. It also gracefully handles the case where the search-path variable doesn't already exist.

This subroutine is not yet documented, but it will be in the next release.

Documentation of ensure_path

ensure_path( var, values ...)

The first parameter is the name of one of the system's environment variables for search paths. The remaining parameters are values that should be in the variable. For each of the value parameters, if it isn't already in the variable, then it is prepended to the variable; in that case the environment variable is created if it doesn't already exist. For separating values, the character appropriate the the operating system is used -- see the configuration variable $search_path_separator.

Example:

ensure_path( 'TEXINPUTS', './custom_cls_sty_files//' );

(In this example, the trailing // is documented by TeX systems to mean that latex, pdflatex, etc search for files in the specified directory and in all subdirectories.)

Technically ensure_path works by setting Perl's variable $ENV{var}, where var is the name of the target variable. The changed value is then passed as an environment variable to any invoked programs.