Currfile – Comprehensive Guide to Using the currfile Package in LaTeX

currfile

I am using a number of input files within 2 directories:

dirA/ which has a file dirA/A.tex which inputs dirA/dirB/B.text

I'm using currfile to print out the currfiledir which gives me

dirA/dirB

is there anyway I can make it just give me dirB?

Best Answer

This can be done with the help of the xstring package and its macro \StrBehind.

In your case, if \currfiledir returns dirA/dirB/, then

\StrBehind{\currfiledir}{/}

will return dirB/.

In fact the result is "all the stuff behind /"

If you also want to remove the trailing / you can use

\StrBehind{\currfiledir}{/}[\currfilesubdir]
\StrBefore{\currfilesubdir}{/}

In this way you first save the result of \StrBehind in the macro \currfilesubdir and then you use it as the argument of \StrBefore which does the opposite of \StrBehind. The result will be dirB.


Obviously this only works if you have your file in a subdirectory of second level like dirA/dirB/.

If your file is in a deeper subdirectory (let's say dirA/dirB/dirC/dirD/, you can opt for one of the following solutions to automate the whole process.

  1. Print the subdirectory name without the trailing /

    Add the following lines in your preamble:

    \newcommand{\currlastdir}{%
      \StrCount{\currfiledir}{/}[\numberofdirs]%
      \ifnum\numberofdirs=1\StrBefore{\currfiledir}{/}\else%
      \StrBehind[\numexpr\numberofdirs-1\relax]{\currfiledir}{/}[\currfilesubdir]%
      \StrBefore{\currfilesubdir}{/}\fi%
    }
    

    and then in your subdocument use

    \currlastdir
    

    instead of

    \currfiledir
    

    and you will get dirD printed.

  2. Print the subdirectory name with the trailing /

    Add the following lines in your preamble:

    \newcommand{\currlastdir}{%
      \StrCount{\currfiledir}{/}[\numberofdirs]%
      \ifnum\numberofdirs=1\currfiledir\else%
      \StrBehind[\numexpr\numberofdirs-1\relax]{\currfiledir}{/}\fi%
    }
    

    and then in your subdocument use

    \currlastdir
    

    instead of

    \currfiledir
    

    and you will get dirD/ printed.

Related Question