是否有相當於 \graphicspath 的列表?

是否有相當於 \graphicspath 的列表?

如何使\lstinputlisting搜尋給定目錄中的檔案?我正在考慮一個類似於解決方案的解決方案這個問題

我想要一個像這樣的解決方案

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

file.ext位於哪裡/some/path/in/the/system。我試著尋找這樣的解決方案,但沒有運氣。我發現\lstinputlistings作者的定義內部使用了\lst@inputpath.然而,對此巨集的簡單修改不會產生任何結果。

答案1

listings套件提供了一個鍵,用於指定搜尋來源檔案的inputpath路徑。\lstinputlisting請注意,inputpath僅記錄在listings開發人員指南中,不是在使用者手冊中;如果您還沒有編譯開發人員指南,請在中搜尋“inputpath”列表.dtx將引導您找到密鑰的定義inputpath。要使用後者,只需編寫

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

在文件中的某個位置(不一定在序言中,當然是在載入之後listings)。如果你真的堅持使用類似的宏\graphicspath,你可以\lstinputpath自己定義一個宏,就像這樣

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

下面的程式碼假設該檔案位於工作目錄的sample.c子目錄中。test

在此輸入影像描述

\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}

答案2

替代方案包括使用\input@pathLaTeX 內部巨集。

也可以看看https://tex.stackexchange.com/a/24827/250119,是否可以為 \input{...} 類似於 \graphicspath{...} 全域設定預設路徑?

例如

\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}

優勢:它可以工作,支援多路徑,並且不會遇到中提到的缺點上面的評論

這裡應該注意的一個重要缺點是,即使提供的參數包含斜線(即使它以 1 開頭,即如果它是絕對路徑),輸入路徑也總是會被添加到前面。

壞處

  • 據我所知\input@path是 LaTeX 的一個內部宏,它的原來的目的甚至不是允許用戶擴展\input...的可能位置? (稍後詳細介紹)
  • 這只有效,因為listings包裝文檔恰好內部用於\input在內部處理文件。否則就行不通。

列表原始碼文檔

(順便說一句,使用\active來表示常數 13 for\endlinechar與自記錄代碼相反。)

相關內容