# Writing own LaTeX templates with relative paths, difference between include, input, class

I'm working on a Windows environment with Miktex. Most my documents are created with XeLaTeX but I think my problem is not compiler-specific.

I would like to create a template with all my package includes, header setup and graphics path e.g. to include header logos from. In all my documents using the template, I would like to only have document specific setup and document contents. My problem is, that the documents might be in a folder different from the template, but I still would like to include pictures from the template folder. My setup looks like this:

.
├── templates/
│   ├── my_template.tex
│   └── pictures/
└── documents/
├── my_document.tex
└── pictures/
└── some_picture.png


Minimal examples could be:

my_template.tex:

\documentclass[
11pt,
a4paper,
]{scrarticle}

\usepackage{scrextend}
\usepackage{scrlayer-scrpage}

%%%%% Graphics %%%%%
\usepackage{graphicx}
\graphicspath{{./pictures/}}

%%%% Title Setup %%%%%
\cfoot{}


my_document.tex:

\input{../templates/my_template.tex}

\graphicspath{{./pictures/}}

\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}


My question is now, what is the standard way of achieving this working. I found this question very helpfull, but I need a little more detail on the different approaches to figure out which one is appropriate. I don't fully understand the difference between include and input in this context or if I should turn my template into a class instead.

As in https://tex.stackexchange.com/a/250/162943, \input will retype the contents of the file and includes add clearpages and cannot be nested. I would stick with \input.
The command \graphicspath receives as argument a list of folders: \graphcispath{{folder1}{folder2}}, so I propose to use the complete path for your template image folder (templates/pictures) and a relative path for the documents image folder (documents/pictures), which I suppose will always have the same name.

my_template.tex:

\documentclass[
11pt,
a4paper,
]{scrarticle}

\usepackage{scrextend}
\usepackage{scrlayer-scrpage}

%%%%% Graphics %%%%%
\usepackage{graphicx}
\graphicspath{{C:/Path/to/template/image/folder}{./pictures/}}

%%%% Title Setup %%%%%
\cfoot{}


my_document.tex:

\input{../templates/my_template.tex}

\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}


Another solution would be to use symbolic links. Create a symbolic link of the template image folder inside your project image folder and only use \graphicspath{{./pictures/}}. In this case you would need to remember to add the name of the folder in each includegraphics.

To avoid absolute paths, you could use the subfiles package (even though its main use case is a little bit different). The required changes are:

• Make the template a complete document, by adding \begin{document}\end{document}.

• At the end of the preamble of the template, load the subfiles package, and set the graphics path afterwards, but letting it only refer to the picture directories relevant for the template, using relative paths.

• Start your real document with the line

\documentclass[rel.path to template]{subfiles}

• In the preamble of the real document, add further local picture directories to the graphics path.

The advantage over other solutions is that the template only needs to know about the picture directories relevant to the template, and the documents may keep their specific images in directories that need not be the same. Moreover, there are no absolute paths required.

For your example, these changes look as follows:

% templates/my_template.tex
\documentclass[
11pt,
a4paper,
]{scrarticle}

\usepackage{scrextend}
\usepackage{scrlayer-scrpage}

%%%%% Graphics %%%%%
\usepackage{graphicx}
\usepackage{subfiles}
\graphicspath{{./pictures/}}% <<< PICTURES LOCAL TO template DIRECTORY

%%%% Title Setup %%%%%
\cfoot{}
\begin{document}% <<< NECESSARY FOR subfiles TO WORK
\end{document}% <<< NECESSARY FOR subfiles TO WORK

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% documents/my_document.tex
\documentclass[../templates/my_template]{subfiles}% <<< LOAD PREAMBLE FROM ../templates/my_template.tex

\makeatletter% <<< EXTEND THE GRAPHICS SEARCH PATH BY LOCAL DIRECTORIES
\edef\Ginput@path{\Ginput@path{./pictures/}}% <<<<<<<<<<<<<<<<<
\makeatother% <<<<<<<<<<<<<<<<<

\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}


As mentioned in one of the other answers, \input directly copy over the text from another file as if you had just manually combined them. This is generally the easiest thing to do and can be nested for large documents. \include will always create a new page to put the content on and can not be nested.

The benefit that using \include gives you is that at the start of the document, you can use \includeonly with a list of filenames and then any includes that specify a different filename will be ignored. I've used this on very large projects where the main sections used \include so that I didn't have to wait for the other sections to compile when I was working on one of them. I then used \input for the subsections since you can't nest \include. You could also do it the other way by inputting the sections and including the subsections.

It is very common to put your preamble in another file. There are some ways to do this that are more or less correct than others. The most common way to do this is to put your preamble (other than \documentclass) into a separate file which you could call whatever, say mypreamble.sty, note that it must end in .sty, and then you can \usepackage{mypreamble} like any other package. You also have to add the line \ProvidesPackage{mypreabmle} to the top of the preamble file. It can also be done using \include but that is not recommended since \usepackage does some internal processing and allows optional parameters, etc.

One of the other packages that is useful for multiple files is called import which allows a command used like this: \import{path\to\secton}{sectionname.tex}. The nice thing about this is that if you want to import something within a file that you have already imported, you can call \subimport{relitive/path/}{plot1.tex} which uses a path relative to the imported file which is quite useful.

The answers of gernot and bryce brought me to actually trying to put my preamble in a .cls file. I had to rearrange some of the files, so that my file-tree now looks like:

.
├── templates/
│   └── tex/
│       └── latex/
│           └── my_template/
│               ├── my_template.cls
└── documents/
├── my_document.tex
└── pictures/
└── some_picture.png


my_template.cls now becomes

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{my_template}
11pt,
a4paper,
]{scrarticle}

\RequirePackage{scrextend}
\RequirePackage{scrlayer-scrpage}

%%%%% Graphics %%%%%
\RequirePackage{graphicx}
\graphicspath{{./pictures/}}

%%%% Title Setup %%%%%
\cfoot{}


and my_document.tex becomes

\documentclass{my_template}

\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}


The graphicspath inside my_template.cls is now relative to the file my_document.tex while the cls directly pulls in graphics from its own location. In order to work, the templates directory needs to be initialized as its own texmf root (I did that using Miktex console --> Settings --> Directories --> Add). The advantage of this setup is, that I can use the template from everywhere on my PC without the need to give relative or absolute paths to the directory it resides in. This helps me a lot in terms of GIT version control.

There are some more answers for this approach and details for e.g. transportable installations here:

Where do I place my own .sty or .cls files, to make them available to all my .tex files?

How to write a LaTeX package that bundles not only .sty and .cls files but also some logos in .pdf or .eps formats?

with especially this answer for default paths.