I have set up the following file tree.
. [D] (working directory)
Test.tex [F]
SubFolder [D]
SubTest.tex [F]
SubSubFolder [D]
SubSubTest.tex [F]
I would like each file to print its path relative to the working directory, so that the PDF file produced by pdflatex Test will display:
[]
[SubFolder/]
[SubFolder/SubSubFolder/]
First Attempt
Using the currfile package.
Test.tex\documentclass{article} \usepackage{currfile} \input{SubFolder/SubTest} \newcommand{\Dir}{\currfiledir} \begin{document} [\Dir]\par [\SubDir]\par [\SubSubDir] \end{document}SubTest.tex\usepackage{currfile} \input{SubSubFolder/SubSubTest} \newcommand{\SubDir}{\currfiledir}SubSubTest.tex\usepackage{currfile} \newcommand{\SubSubDir}{\currfiledir}
This did not yield the desired result. The compilation failed with the following error message:
! LaTeX Error: File `SubSubFolder/SubSubTest.tex' not found.
Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)
Enter file name:
! Emergency stop.
<read *>
l.2 \input{SubSubFolder/SubSubTest}
^^M
End of file on the terminal!
Second Attempt
Adding the import package. (The idea was inspired by this answer.)
Test.tex\documentclass{article} \usepackage{currfile} \usepackage{import} \import{SubFolder}{SubTest} \newcommand{\Dir}{\currfiledir} \begin{document} [\Dir]\par [\SubDir]\par [\SubSubDir] \end{document}SubTest.tex\usepackage{currfile} \usepackage{import} \import{SubSubFolder}{SubSubTest} \newcommand{\SubDir}{\currfiledir}SubSubTest.texas before.
This time the compilation completed successfully, but the resulting PDF file displayed differently than desired, namely:
[]
[]
[]
Testing the import package on its own
For comparison, by doing away with the currfile package as in the following code, the compilation completed successfully, and the desired outcome was achieved. However, this did not really solve the problem, since the paths were now hard-coded rather than inferred automatically. However, it demonstrated that the import package was capable of navigating the file tree correctly.
Test.tex\documentclass{article} \usepackage{import} \import{SubFolder}{SubTest} \newcommand{\Dir}{\hspace{0pt}} \begin{document} [\Dir]\par [\SubDir]\par [\SubSubDir] \end{document}SubTest.tex\usepackage{import} \import{SubSubFolder}{SubSubTest} \newcommand{\SubDir}{SubFolder/}SubSubTest.tex\newcommand{\SubSubDir}{SubFolder/SubSubFolder/}
importpackage before use it, the syntax is not identical to\input. – user202729 Nov 22 '22 at 04:51\newcommandwon't work as expected, you can\let\SubDir\currdirname; that having said\letis a primitive TeX command so you might want to start reading TeXbook to know what it does exactly – user202729 Nov 22 '22 at 04:56importpackage's documentation, but it didn't resolve the problem. – Evan Aad Nov 22 '22 at 05:00\newcommands by\lets (with respect to my corrected code) in the Second Attempt version didn't help either. The compilation failed with the following error message:! Undefined control sequence. l.10 [\SubDir ]\par– Evan Aad Nov 22 '22 at 05:04\currdirnamewith\currfiledir. I've updated my post accordingly. Only the Second Attempt behaved differently than before. This time the Second Attempt's compilation completed successfully, but the resulting PDF file did not display as desired: all three directories were rendered as the empty string. – Evan Aad Nov 22 '22 at 07:27\let. – user202729 Nov 22 '22 at 07:30SubFolder//SubSubFolder/, with two forward slashes between the two folder names, rather than a single forward slash. This makes this string unusable as-is for the purpose of constructing other paths that are based on it. – Evan Aad Nov 22 '22 at 07:39\inputfiles relative to the working directory of the tex process, you do not need the path of the current file. – David Carlisle Nov 22 '22 at 09:03\usepackage{amsmath}which is\input{amsmath.sty}without forcing your document to hardwire/usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amsmath.stywhy use paths for your local files? – David Carlisle Nov 22 '22 at 09:53