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/}
    
12
  • 3
    Good attempt. But do read the documentation of import package before use it, the syntax is not identical to \input.
    – user202729
    Nov 22, 2022 at 4: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, 2022 at 4:56
  • @user202729 I've corrected my example per the import package's documentation, but it didn't resolve the problem.
    – Evan Aad
    Nov 22, 2022 at 5:00
  • 1
    Also should probably be \currfiledir instead of \currdirname be careful with what you use
    – user202729
    Nov 22, 2022 at 6:52
  • 1
    then use \let.
    – user202729
    Nov 22, 2022 at 7:30

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .