2

I use XFig to draw most of my figures (how old am I?) and I find convenient to export in "LaTeX+PDF" format, in which a simple LaTeX source. e.g., fig03.pdf_t (the extension is fixed), is created. That file takes care of typesetting, using the document settings, and uses \includegraphics from the graphicx package to include, in our example, the file fig03.pdf in a picture environment.

I usually have the XFig sources and the exported files in a ./figures sub-directory and I have \graphicspath{{figures/}} in the document source.

So far … but sometimes I like to put in evidence, in the document directory, a particular figure or I'm just lazy or confused and some of my figures are in ./ rather than in ./figures/.

I'd like to be able to write

\input\get{fig03}

and have LaTeX find the file for me.

How do I define the \get command?


In its most basic formulation \get just scans ./ and ./figures/ but it seems reasonable that this can be customized setting a list in the way of \graphicspath

gboffi
  • 911
  • If (for instance) foo.png is in the figures subdirectory and bar.png is in the current directory, `\documentclass{article} \usepackage{graphicx}

    \graphicspath{{figures/}}

    \begin{document}

    \includegraphics{foo} \includegraphics{bar}

    \end{document}` finds both files.

    – frougon Jan 23 '20 at 11:06
  • @frougon Yes, \includegraphics finds both but I'm using, I have to use \input to read into my document the LaTeX source prepared by XFig – gboffi Jan 23 '20 at 11:18
  • Workaround: Export xfig figures to tikz or pict2e, (the latter cannot do hatches), and only input one file (input{fig03.tikz}), or copy-paste the entire file into the latex document. The tikz-output is superior to latex+pdf output. Also, did you have a look at the import package, which provides some search-path setting machinery? – tkl Jan 29 '20 at 16:05

2 Answers2

3

If you can afford to modify the environment only for compiling this kind of document, one possibility is to add the needed directories to TEXINPUTS (see for instance here). Otherwise, in order to have your customizable list of paths to search, you can use the following:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \g_gboffi_search_path_seq

\prg_generate_conditional_variant:Nnn \str_if_eq:nn { x } { T, F, TF }

\cs_new_protected:Npn \gboffi_set_search_path:n #1
  {
    \seq_gclear:N \g_gboffi_search_path_seq
    \clist_map_inline:nn {#1}
      {
        % Append a '/' if not already present before appending a path to
        % \g_gboffi_search_path_seq.
        \str_if_eq:xnTF { \tl_item:nn {##1} { -1 } } { / }
          { \seq_gput_right:Nn \g_gboffi_search_path_seq {##1} }
          { \seq_gput_right:Nn \g_gboffi_search_path_seq {##1/} }
      }
  }

\msg_new:nnn { gboffi } { cannot-find-file }
  { Can't~find~file~'\exp_not:n {#1}.tex'. }

\bool_new:N \l__gboffi_found_flag_bool

\cs_new_protected:Npn \gboffi_input:n #1
  {
    \bool_set_false:N \l__gboffi_found_flag_bool

    \seq_map_inline:Nn \g_gboffi_search_path_seq
      {
        \file_if_exist:nT { ##1#1.tex }
          {
            \file_input:n { ##1#1.tex }
            \bool_set_true:N \l__gboffi_found_flag_bool
            \seq_map_break:
          }
      }

    \bool_if:NF \l__gboffi_found_flag_bool
      { \msg_error:nnn { gboffi } { cannot-find-file } {#1} }
  }

\NewDocumentCommand \myInputWithSearchPath { m }
  {
    \gboffi_input:n {#1}
  }

\NewDocumentCommand \mySetSearchPath { m }
  {
    \gboffi_set_search_path:n {#1}
  }
\ExplSyntaxOff

\begin{document}

  \mySetSearchPath{., figures dir 1, {path, with, commas}, figures dir 2}

  \myInputWithSearchPath{fig01}\par
  \myInputWithSearchPath{fig02}\par
  \myInputWithSearchPath{fig03}\par
  \myInputWithSearchPath{fig04}

  % This would print “Package gboffi Error: Can't find file 'non-existent.tex'.”
  % \myInputWithSearchPath{non-existent}

\end{document}

screenshot

I let you replace .tex with .pdf_t or whatever file extension you want to use. There are two places involved:

  • in the definition of \gboffi_input:n, where it reads \file_if_exist:nT { ##1#1.tex } and \file_input:n { ##1#1.tex };

  • in the error message printed for non-existent files:

    \msg_new:nnn { gboffi } { cannot-find-file }
      { Can't~find~file~'\exp_not:n {#1}.tex'. }
    
egreg
  • 1,121,712
frougon
  • 24,283
  • 1
  • 32
  • 55
  • Oops, maybe after all I do not want a customizable list of paths... On the other hand, I wonder if such a machinery could not be the core of a package, \usepackage{searchpath}\newsearch{fig}\figpath{{...},{...},{...}} and later \input\figsearch{fig03.pdf_t} – gboffi Jan 23 '20 at 12:39
  • 1
    You should do \prg_generate_conditional_variant:Nnn \str_if_eq:nn { e } { T,F,TF,p } so the conditional remains expandable. On the other hand, \str_if_eq:ee(TF) are already provided and expanding both arguments in the particular case is not a problem. – egreg Jan 23 '20 at 12:51
  • @egreg I've replaced the \cs_generate_variant:Nn \str_if_eq:nnTF { x } with \prg_generate_conditional_variant:Nnn \str_if_eq:nn { x } { p, T, F, TF }, thanks for the reminder (the fact that \str_if_eq:xnT is not expandable isn't a problem here). For now, I prefer using x variants over e ones when there is no downside, because some people have very old installations where e variants are very slow. – frougon Jan 23 '20 at 13:00
  • @gboffi If you didn't need a customizable list of paths, why did you write “it seems reasonable that this can be customized setting a list in the way of \graphicspath” in your question? – frougon Jan 23 '20 at 13:09
  • Because it's a nice feature and because I did not imagine that the solution could be so much over my head... – gboffi Jan 23 '20 at 13:21
  • @gboffi The code simply implements the obvious algorithm, caring about appending a trailing / to a path only when necessary. It is not because expl3 code uses normal spacing for a programming language, which TeX doesn't, that it is more difficult. I could put all this on a single line. Some people would probably upvote because it would be short and they wouldn't understand anything. That wouldn't make it good programming, though (i.e.: readable, without bad surprises, maintainable). – frougon Jan 23 '20 at 13:30
  • @egreg Thanks for your edit! Indeed, I had included generation of the predicate by mistake when adding the \prg_generate_conditional_variant:Nnn. – frougon Jan 23 '20 at 14:06
2

Maybe this?

\documentclass{article}

\newcommand{\get}[1]{%
\IfFileExists{./figures/#1}{%
        \input{./figures/#1} 
    }{%
        \input{./#1} 
    }
}


\begin{document}

\get{fig03.pdf_t}

\end{document}
Charles
  • 188