1

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.tex as 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/}
    
Evan Aad
  • 11,066
  • 3
    Good attempt. But do read the documentation of import package before use it, the syntax is not identical to \input. – user202729 Nov 22 '22 at 04:51
  • 1
    Besides your \newcommand won't work as expected, you can \let\SubDir\currdirname; that having said \let is a primitive TeX command so you might want to start reading TeXbook to know what it does exactly – user202729 Nov 22 '22 at 04:56
  • @user202729 I've corrected my example per the import package's documentation, but it didn't resolve the problem. – Evan Aad Nov 22 '22 at 05:00
  • @user202729 Replacing \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
  • 1
    Also should probably be \currfiledir instead of \currdirname be careful with what you use – user202729 Nov 22 '22 at 06:52
  • @user202729 I've followed your suggestion, and replaced \currdirname with \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
  • 1
    then use \let. – user202729 Nov 22 '22 at 07:30
  • @user202729 Ah! Yes! Now the output is almost as desired. Thank you! However, there's still one hitch: the subsubfolder is rendered as SubFolder//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
  • It should still be usable, on most operating systems it doesn't matter. (in fact that's most likely what the library uses as the path internally...) – user202729 Nov 22 '22 at 07:40
  • why do you need the paths? You can \input files 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
  • @DavidCarlisle I do not wish my code to be dependent on the structure of the file tree beyond the immediate next level of the hierarchy. This is common sense and good programming practice ubiquitously implemented in all programming languages that I'm familiar with, as well as in many operating systems in the form of shortcuts/aliases. – Evan Aad Nov 22 '22 at 09:14
  • exactly. You can use \usepackage{amsmath} which is \input{amsmath.sty} without forcing your document to hardwire /usr/local/texlive/2022/texmf-dist/tex/latex/amsmath/amsmath.sty why use paths for your local files? – David Carlisle Nov 22 '22 at 09:53

1 Answers1

0

The key is replacing the calls to \newcommand by \let in the Second Attempt, resulting in the following code. This produces a PDF file whose display is very close to the desired outcome:

[]
[SubFolder/]
[SubFolder//SubSubFolder/]

I have arrived to this solution thanks to user202729's step-by-step advice in the comments to my original post.

As can be seen, the subsubfolder's path is rendered with two forward slashes between the two folder names, rather than a single forward slash. To get a single forward slash, replace all occurrences of \import in the code by \subimport. The idea was given to me by user202729 in a comment to a related post.

Evan Aad
  • 11,066