26

How can I make the \lstinputlisting to search for files in a given directory? I'm thinking of a solution along the lines of the solution to this question.

I would like to have a solution like

\lstinputpath{/some/path/in/the/system}
\lstinputlisting{file.ext}

where file.ext resides in /some/path/in/the/system. I tried to search for such solution without luck. I found that inside of the definition of \lstinputlistings the authors use a \lst@inputpath. However, a naive modification of this macro doesn't yield any results.

1

2 Answers 2

27

The listings package provides a key called inputpath for specifying a path where \lstinputlisting should search for source files. Note that inputpath is only documented in the listings developer's guide, not in the user manual; if you haven't compiled the developer's guide, searching for "inputpath" in listings.dtx will lead you to the definition of the inputpath key. To use the latter, simply write

\lstset{inputpath=<path-in-question>}

somewhere in your document (not necessarily in your preamble, but after loading listings, of course). If you really insist on using a macro similar to \graphicspath, you can define an \lstinputpath macro yourself, like so

\newcommand*\lstinputpath[1]{\lstset{inputpath=#1}}

The code below assumes that the file sample.c resides in a subdirectory called test of your working directory.

enter image description here

\documentclass{article}

\usepackage{listings}

\newcommand*\lstinputpath[1]{\lstset{inputpath=#1}}

\lstinputpath{test}

\begin{document}
\lstinputlisting[
  language   = C,
  basicstyle = \ttfamily,
  frame      = single,
  caption    = {Hello world in C},
]{sample.c}
\end{document}
7
  • The use of the key is cleaner than redefining the macro. I didn't found that key when going through the code. Authors should really make that more clear.
    – adn
    Commented Mar 25, 2014 at 20:32
  • 2
    @adn See my edit.
    – jub0bs
    Commented Mar 25, 2014 at 21:31
  • Is there any way to bring up the developer's guide via texdoc? Commented Mar 31, 2014 at 1:49
  • 2
    Are multiple paths allowed? If so, could you modify the answer to demonstrate multiple paths?
    – kando
    Commented Sep 21, 2016 at 18:25
  • 1
    One important drawback that should be noted here is that the inputpath is always prepended even if the supplied argument contains slashes (and even if it starts with one i.e. if it is an absolute path).
    – stefanct
    Commented Aug 9, 2018 at 5:17
2

Alternative includes using \input@path LaTeX internal macro.

See also https://tex.stackexchange.com/a/24827/250119, Can a default path be set globally for \input{...} akin to \graphicspath{...}?.

e.g.

\documentclass{article}
\usepackage{listings}

\makeatletter
\def\input@path{{SubFolder/}}
\makeatother

\begin{document}
\lstinputlisting[
  language   = C,
  basicstyle = \ttfamily,
  frame      = single,
  caption    = {Hello world in C},
]{SubTest.tex}
\end{document}

Advantage: It works, supports multiple paths, and does not suffer from the disadvantage mentioned in the comment above

One important drawback that should be noted here is that the inputpath is always prepended even if the supplied argument contains slashes (and even if it starts with one i.e. if it is an absolute path).

Disadvantage:

  • As far as I can see \input@path is an internal macro of LaTeX, and its original purpose isn't even to allow users to extend the possible locations for \input...? (details later)
  • This only works because listings package documentation just happens to internally use \input to process the file internally. Otherwise it wouldn't work.

listings source code documentation

(by the way using \active to represent the constant 13 for \endlinechar is the opposite of self-documented code.)

1

You must log in to answer this question.

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