1

I have a LaTeX-Project that is split into multiple files using the package subfiles. The project has the following structure:

main.tex
.latexmkrc
lib/custom-class.cls
lib/custom-package.sty
foo/bar.tex
foo/image.png
baz/...

I use latexmkrc to include the class- and package-files in lib in TEXINPUTS:

ensure_path( 'TEXINPUTS', 'lib//' )

The project builds correctly when I build main.tex, but when I build one of the subfiles (e.g. for/bar.tex) I get the error

File `custom-class.cls` not found.

I realise this is because .latexmkrc is not used when building in a subdirectory. I found a discussion on the GitHub-Page of the VSCode-extension LaTeX-Workshop (which I am using) and the best solution there seems to be to just copy and paste .latexmkrc into the subdirectories – the issue is that I have many (20 or so) subdirectories and I want to avoid having to change all these files whenever I change the main .latexmkrc, especially since I am not the only one working on this project. Is there any other way, or can I, perhaps, create a .latexmkrc in each of the subdirectories that just imports the main .latexmkrc?

Thanks for any pointers.

8
  • (a) I don't think copying and pasting would work, since the relative path is wrong (from the base directory it is lib/ but from foo/ it should be ../lib/). This can be solved if you use absolute paths instead. (b) To have the same file show up in multiple folders is a textbook case of what symbolic links are good for. Commented Jul 27, 2023 at 13:30
  • Oh right, I didn't think about the problem with the path being relative... using absolute paths would break when building on a different system – and would symbolic links work with git? As in, does git recognise that they are symbolic, or would git recognise 20 different files in the different subdirectories?
    – GeF
    Commented Jul 27, 2023 at 13:48
  • For the path problem, if you don't worry about possible injection issues, you can add both ./lib/ and ../lib/ to the search path. The question about git and symlink is more complicated, but it should work if you configure things right (and create the symlinks using relative paths and not absolute paths). Commented Jul 27, 2023 at 13:54
  • Another option, since you are working with git, is that you can implement a post-commit hook that copies (perhaps even with appropriate seding to replace the paths) the .latexmkrc to the appropriate directories. You just need to make sure that all edits are made to the main .latexmkrc and the generated ones are not tracked by git (which you can help by putting them all in .gitignore.) Commented Jul 27, 2023 at 13:59
  • 1
    Thanks for the help! Your last comment got me thinking and I just added .latexmkrc files to all subdirectories containing only the line do '../.latexmkrc'; This works perfectly since I do not worry about injection issues.
    – GeF
    Commented Jul 28, 2023 at 15:34

1 Answer 1

1

Try adding the following lines at the very beginning of the main file:

\makeatletter
\def\input@path{{lib/}{../lib/}}
\makeatother

This sets the input path to the directories where to search for additional files, from both perspectives (main directory and subfolder foo).

I tested this approach with the following sample project:

% main.tex
\makeatletter
\def\input@path{{lib/}{../lib/}}
\makeatother
\documentclass{custom-class}
\usepackage{custom-package}
\usepackage{graphicx}
\usepackage{subfiles}
\begin{document}
\subfile{foo/bar.tex}
\end{document}

% foo/bar.tex
\documentclass[../main]{subfiles}
\begin{document}
\includegraphics[width=2cm]{image.png}
\end{document}

% cp `kpsewhich example-image.png foo/image.png`

% lib/custom-class.cls
\LoadClass{article}

% lib/custom-package.sty
\endinput
1
  • Thanks! This looks like a great solution since I only use latexmk to set TEXINPUTS and quick preliminary testing suggests it works well. I found another solution more akin to what Willie Wong suggested (see comments on the question), but I will accept your answer. If someone needs this with more functionality of latexmk the solution in the comments should work.
    – GeF
    Commented Jul 28, 2023 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .