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
...
mainfile)", i.e., this directory does not need to be the directory where themainfile is located. However, in most cases this will be the directory of themainfile. – Nathanael Skrepek Feb 11 '24 at 21:06