LaTeX多文件編譯的方法總結html
在編寫LaTeX文檔的時候,因爲文檔的section較多,或者section的編寫時間各不相同,咱們可能碰到以下問題:ide
一、因爲想分開編寫各個sectionpost
二、preamble太多,想專門弄一個文件放preambleui
三、想用bibtex來生成參考文獻this
我分別參考了這個幾個網頁:111 222,再結合本身的上手體會,給出下面這個(還湊合的)解決方案。url
咱們首先創建一個文件夾「project」,在下面分別建立「main.tex」文件「sections」文件夾。在「main.tex」裏面寫入:spa
\documentclass[fleqn]{article} \usepackage{subfiles} \usepackage{D:/My_Preamble} %必須是絕對路徑,才能讓各個section*.tex在單獨編譯時使用到 \title{This is title} \author{Jay Chou} \date{\today} \begin{document} \maketitle Hello World! \subfile{sections/section1} \subfile{sections/section2} \bibliographystyle{unsrt} \bibliography{D:/My_Reference} %必須是絕對路徑。但各個section*.tex單獨編譯時使用不到(這是一個缺點) \end{document}
「section1.tex」文件這樣編寫,並放到「sections」文件夾裏:code
\documentclass[../main.tex]{subfiles} %兩個點表明返回上一級菜單 \newcommand{\AAA}{\textbf{abcdefg}} % 這個'\AAA'命令只在這個tex文件裏起做用 \begin{document} %From there on, type whatever you want. \section{This is section 1} Hello, this is section 1. \end{document}
其餘的section的tex文件照着這個格式寫就好了。「My_preamble.sty」文件以下編寫,並放到D盤目錄下:orm
\ProvidesPackage{msqmypreamble} \usepackage{amsmath, amssymb, amsthm} \usepackage{cite} %\usepackage{graphicx, graphics} % Allows including images % etc
% etc
% etc %\setlength{\voffset}{-2.0cm} %\setlength{\parskip}{0.2cm} \newtheorem{thm}{Theorem} \setcounter{thm}{0} \newcommand{\defn}{\overset{\Delta}{=}}
\newcommand{\st}{\textrm{~s.t.~}}
\newcommand{\supp}{\mathop{\rm supp}}
% etc
% etc
% etc
如今,把各個section共同的preamble都寫到「My_Preamble.sty」文件中,而後將這個文件放到一個路徑下,而後在「main.tex」文件中像4行那樣使用這個sty。爲何必須是絕對路徑,這是爲了讓以後的各個「section*.tex」文件也能找到這個文件在哪,從而使用裏面的命令。 在各個「section*.tex」文件中,你能夠再附加一個本section獨有的preamble,就想第三行的「\AAA」那樣。但這個「\AAA」只能在本tex文件中使用。htm
而後是bibtex。各個「section*.tex」在單獨編譯的時候沒法找到參考文件,及時在「main.tex」中bib文件的路徑是絕對路徑。這算是一個小瑕疵(很慚愧)。將bib文件單獨拿出來放在一個地方的做用是使得每次收集bibtex信息能夠集中在一塊兒。
以後,咱們能夠編譯這個「main.tex」文件,獲得包含各個section的總文件,也能夠單獨編譯各個「section*.tex」文件,而且各個「section*.tex」文件可使用My_Preamble這個preamble,很方便。但不足之處是,單獨編譯各個「section*.tex」文件時,若文件中應用了bib文件中的條目,輸出結果是顯示不出來的,以後在編譯總文件時才能正確顯示。
關於label的引用:各個「section*.tex」內部最好不要使用同名的label標號(例如 \label{eq:1} ),使用相同的標號會衝突的。同時,各個「section*.tex」之間能夠相互應用對方的標號,當編譯「main.tex」時,這些相互引用的標號就會顯示出來(單獨編譯某個「section*.tex」顯示不出來)。
最後是這個「subfiles.sty」文件,它的內容以下:
%% This is file `subfiles.sty', %% generated with the docstrip utility. %% %% The original source files were: %% %% subfiles.dtx (with options: `package') %% %% Copyright 2002 Federico Garcia %% \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{subfiles}[2002/06/08 Federico Garcia] \DeclareOption*{\PackageWarning{\CurrentOption ignored}} \ProcessOptions \RequirePackage{verbatim} \newcommand{\skip@preamble}{% \let\document\relax\let\enddocument\relax% \newenvironment{document}{}{}% \renewcommand{\documentclass}[2][subfiles]{}} \newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup} \endinput %% %% End of file `subfiles.sty'.
建立個txt文件,把如上代碼複製進去,而後把txt文件的文件名改成「subfiles.sty」,放到裝「main.tex」的文件就可使用了。
寫了這麼多,應該能 +1s 吧,蛤蛤蛤!