![是否有相當於 \graphicspath 的列表?](https://rvso.com/image/286965/%E6%98%AF%E5%90%A6%E6%9C%89%E7%9B%B8%E7%95%B6%E6%96%BC%20%5Cgraphicspath%20%E7%9A%84%E5%88%97%E8%A1%A8%EF%BC%9F.png)
如何使\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@path
LaTeX 內部巨集。
也可以看看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
與自記錄代碼相反。)