2

I'm using the following code to search for a file using LaTeX3:

% List of known image extensions:
\clist_new:N \l__htmv_grext_clist
\clist_set:Nn \l__htmv_grext_clist {png,PNG,jpg,JPG,jpeg,JPEG,gif,GIF}
\DeclareDocumentCommand \findFile {o m}
{
  \tl_set:Nn \l_tmpa_tl {#2}
  \file_if_exist:nF {#2}
  {
    \clist_map_inline:Nn \l__htmv_grext_clist
    {
      \file_if_exist:nT {#2.##1}
      {
        \tl_set:Nn \l_tmpa_tl {#2.##1}
        \clist_map_break:
      }
    }
  }
  \tl_show:N \l_tmpa_tl
}

In short, the idea is to allow the user to specify the full filename or to leave the extension off whereupon it will look in its list of known extensions for the first match.

This works fine ... unless the filename happens to also exist somewhere in the standard TeX search path. Moreover, if it exists with a standard TeX extension then that gets found by the initial check. The one that got me was a file called chord.tex, when I put \findFile{chord} then it finds the chord.tex rather than the local chord.png which it ought to find.

What I'd like most to do is to search in current and sub- directories only. I know that one can add or remove specific paths from the search path used in L3, but what I'd like to do is (temporarily, aka locally) clear everything (except the current and sub directories; I'd also be okay with a solution that had to have subdirectories explicitly added as the user could be asked to give a list of subdirectories to search in in the preamble).

  • You can search only in the current directory with ./file.ext, see the example in my question tex.stackexchange.com/questions/179580/… – Ulrike Fischer May 31 '16 at 11:53
  • @UlrikeFischer Good to know. So then I'd need to detect if the filename given had a leading path and if not prepend ./ to it. I think I can cope with that, though different OS's might need different systems ... – Andrew Stacey May 31 '16 at 12:51
  • @UlrikeFischer An answer? – Henri Menke Jun 17 '16 at 7:41
2

You can restrict the search to the current directory with ./file.ext, see the example in my question Search for files first in the texmf trees.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.