
我正在使用 Miktex 在 Windows 環境中工作。我的大多數文件都是使用 XeLaTeX 建立的,但我認為我的問題不是特定於編譯器的。
我想建立一個模板,其中包含我的所有套件、標題設定和圖形路徑,例如包含標題徽標。在使用範本的所有文件中,我只想有文件特定的設定和文件內容。我的問題是,文件可能位於與範本不同的資料夾中,但我仍然想包含範本資料夾中的圖片。我的設定如下所示:
.
├── templates/
│ ├── my_template.tex
│ └── pictures/
│ └── header_logo.pdf
└── documents/
├── my_document.tex
└── pictures/
└── some_picture.png
最小的例子可以是:
my_template.tex:
\documentclass[
11pt,
a4paper,
]{scrarticle}
\usepackage{scrextend}
\usepackage{scrlayer-scrpage}
%%%%% Graphics %%%%%
\usepackage{graphicx}
\graphicspath{{./pictures/}}
%%%% Title Setup %%%%%
\ihead{\includegraphics[width=6cm]{header_logo.pdf}}
\cfoot{}
\setlength{\headheight}{14pt}
我的文檔.tex:
\input{../templates/my_template.tex}
\graphicspath{{./pictures/}}
\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}
我現在的問題是,實現這項工作的標準方法是什麼。我發現這個問題非常有幫助,但我需要更多關於不同方法的詳細信息,以確定哪種方法合適。我不完全理解include
和在這種情況下的區別input
,或者我是否應該將我的模板變成一個類別。
答案1
如在https://tex.stackexchange.com/a/250/162943,\input
將重新輸入文件內容並includes
添加clearpages
且不能嵌套。我會堅持\input
。
該命令\graphicspath
接收資料夾清單作為參數:\graphcispath{{folder1}{folder2}}
,因此我建議使用模板圖像資料夾(模板/圖片)的完整路徑和文檔圖像資料夾(文檔/圖片)的相對路徑,我想這將始終具有同名。
my_template.tex:
\documentclass[
11pt,
a4paper,
]{scrarticle}
\usepackage{scrextend}
\usepackage{scrlayer-scrpage}
%%%%% Graphics %%%%%
\usepackage{graphicx}
\graphicspath{{C:/Path/to/template/image/folder}{./pictures/}}
%%%% Title Setup %%%%%
\ihead{\includegraphics[width=6cm]{header_logo.pdf}}
\cfoot{}
\setlength{\headheight}{14pt}
我的文檔.tex:
\input{../templates/my_template.tex}
\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}
另一種解決方案是使用符號連結。在項目圖像資料夾中建立模板圖像資料夾的符號鏈接,並僅使用\graphicspath{{./pictures/}}
.在這種情況下,您需要記住在每個includegraphics
.
答案2
為了避免絕對路徑,您可以使用該subfiles
套件(儘管它的主要用例有點不同)。所需的更改是:
透過添加
\begin{document}\end{document}
.在模板序言的末尾,載入包
subfiles
,然後設定圖形路徑,但讓它只引用與模板相關的圖片目錄,使用相對路徑。使用以下行開始您的真實文檔
\documentclass[rel.path to template]{subfiles}
在真實文件的序言中,將更多的本機圖片目錄新增至圖形路徑。
相對於其他解決方案的優點是,範本只需要知道與範本相關的圖片目錄,文件可以將其特定影像保留在不需要相同的目錄中。此外,不需要絕對路徑。
對於您的範例,這些變更如下所示:
% templates/my_template.tex
\documentclass[
11pt,
a4paper,
]{scrarticle}
\usepackage{scrextend}
\usepackage{scrlayer-scrpage}
%%%%% Graphics %%%%%
\usepackage{graphicx}
\usepackage{subfiles}
\graphicspath{{./pictures/}}% <<< PICTURES LOCAL TO template DIRECTORY
%%%% Title Setup %%%%%
\ihead{\includegraphics[width=6cm]{header_logo.pdf}}
\cfoot{}
\setlength{\headheight}{14pt}
\begin{document}% <<< NECESSARY FOR subfiles TO WORK
\end{document}% <<< NECESSARY FOR subfiles TO WORK
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% documents/my_document.tex
\documentclass[../templates/my_template]{subfiles}% <<< LOAD PREAMBLE FROM ../templates/my_template.tex
\makeatletter% <<< EXTEND THE GRAPHICS SEARCH PATH BY LOCAL DIRECTORIES
\edef\Ginput@path{\Ginput@path{./pictures/}}% <<<<<<<<<<<<<<<<<
\makeatother% <<<<<<<<<<<<<<<<<
\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}
答案3
正如其他答案之一中提到的,\input
直接複製另一個文件中的文本,就像您剛剛手動組合它們一樣。這通常是最簡單的事情,並且可以嵌套用於大型文件。\include
總是會創建一個新頁面來放置內容並且不能嵌套。
使用為您帶來的好處\include
是,在文件的開頭,您可以使用\includeonly
文件名列表,然後指定不同文件名的任何包含都將被忽略。我在非常大的專案中使用了它,其中使用了主要部分,\include
這樣當我處理其中一個部分時,我就不必等待其他部分編譯。然後我用了\input
小節,因為你不能嵌套\include
。您也可以透過輸入部分並包含子部分來以其他方式完成此操作。
將序言放在另一個文件中是很常見的。有一些方法可以比其他方法或多或少正確地做到這一點。最常見的方法是將您的前導碼(除了\documentclass
)放入單獨的文件中,您可以將其稱為任何文件,例如mypreamble.sty
,注意它必須以 結尾.sty
,然後您可以\usepackage{mypreamble}
像任何其他包一樣。您還必須將該行新增\ProvidesPackage{mypreabmle}
至前導碼檔案的頂部。也可以使用它來完成\include
,但不建議這樣做,因為\usepackage
會進行一些內部處理並允許可選參數等。
呼叫對多個檔案有用的其他套件之一,import
它允許像這樣使用命令:\import{path\to\secton}{sectionname.tex}
。這樣做的好處是,如果您想在已經匯入的檔案中匯入某些內容,您可以呼叫\subimport{relitive/path/}{plot1.tex}
它使用相對於匯入檔案的路徑,這非常有用。
答案4
格諾特和布萊斯的回答讓我真正嘗試將我的序言放入文件中.cls
。我必須重新排列一些文件,以便我的文件樹現在看起來像:
.
├── templates/
│ └── tex/
│ └── latex/
│ └── my_template/
│ ├── my_template.cls
│ └── header_logo.pdf
└── documents/
├── my_document.tex
└── pictures/
└── some_picture.png
my_template.cls 現在變成
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{my_template}
\LoadClass[
11pt,
a4paper,
]{scrarticle}
\RequirePackage{scrextend}
\RequirePackage{scrlayer-scrpage}
%%%%% Graphics %%%%%
\RequirePackage{graphicx}
\graphicspath{{./pictures/}}
%%%% Title Setup %%%%%
\ihead{\includegraphics[width=6cm]{header_logo.pdf}}
\cfoot{}
\setlength{\headheight}{14pt}
my_document.tex 變成
\documentclass{my_template}
\begin{document}
\includegraphics[width=6cm]{some_picture.png}
\end{document}
內部的圖形路徑my_template.cls
現在與檔案相關,my_document.tex
而 cls 直接從自己的位置提取圖形。為了工作,templates
目錄需要初始化為其自己的 texmf 根(我使用Miktex console --> Settings --> Directories --> Add
)。這種設定的優點是,我可以在 PC 上的任何位置使用該模板,而無需提供其所在目錄的相對或絕對路徑。
對於這種方法還有更多答案,以及例如可移動安裝的詳細資訊:
我應該將自己的 .sty 或 .cls 檔案放在哪裡,以使它們可供我的所有 .tex 檔案使用?
如何編寫一個 LaTeX 包,不僅捆綁 .sty 和 .cls 文件,還捆綁一些 .pdf 或 .eps 格式的標誌?
特別是與這回答預設路徑。