0

Problem: This is something that that bothered me for quite a time. If you have multiple files, then you have to make all input paths relative to the main tex file (or actually the directory from where you compile the main file) and not relative to the file that calls the input.

Appearances: There are also others that expected that the paths are meant to be relative to the current path here, here, here, here or here

Packages: I am aware of the import package. However, I think it is strange that there different commands for different levels. Also the subfiles can be used for relative paths.

Conclusion: Nevertheless, I thought there should be an elementary solution. Hence, I finally came up with a really short solution.

  • @DavidCarlisle This is what I wanted to say with "(or actually the directory from where you compile the main file)", i.e., this directory does not need to be the directory where the main file is located. However, in most cases this will be the directory of the main file. – Nathanael Skrepek Feb 11 '24 at 21:06

1 Answers1

1

Solution

One possible solution that uses the LaTeX3 "data types" is actually very simple. It can be nested arbitrarily many times. I guess this solution can be further improved by people with sophisticated skills, so feel free to improve it and use it in other packages.

\ExplSyntaxOn

\seq_new:N \g_relinput_stack_seq \tl_new:N \g_relinput_dump_tl

\NewDocumentCommand{\relinput}{O{./}m}{ % add the new relative input to the history path \seq_put_right:Nn \g_relinput_stack_seq {#1} % input the file \input{\seq_use:Nn \g_relinput_stack_seq {} #2} % remove the new relative input from the history \seq_gpop_right:NN \g_relinput_stack_seq \g_relinput_dump_tl }

\ExplSyntaxOff

I am not even sure, if the dump variable is needed, but \seq_gpop_right:NN wants two arguments. Clearly, this can easily be extended for the \include command. Just by adding

\NewDocumentCommand{\relinclude}{O{./}m}{
  % add the new relative input to the history path
  \seq_put_right:Nn \g_relinput_stack_seq {#1}
  % input the file
  \include{\seq_use:Nn \g_relinput_stack_seq {} #2}
  % remove the new relative input from the history
  \seq_gpop_right:NN \g_relinput_stack_seq \g_relinput_dump_tl
}

Example

The usage is very simple. Lets us say we have the following folder structure

src/
   main.tex
   chapter1/
      chapter1.tex
      tikz/
         tikz-picture.tex
   chapter2/
      chapter2.tex
      big-table.tex

Then in the main file main.tex we have

...
\begin{document}

\relinput[./chapter1]{chapter1} \relinput[./chapter2]{chapter2} ... \end{document}

In chapter1.tex

...
\relinput[./tikz]{tikz-picture}
...

In chapter2.tex

...
\relinput{big-table} % \relinput[./]{big-table} would also work
...