關於分開編寫多個LaTeX文件的一點微小的總結


【轉載請注明出處】http://www.cnblogs.com/mashiqi

2016/11/05

 

方法1:

按照這個網址(https://www.dickimaw-books.com/latex/thesis/html/include.html)的說明,使用 \include

 

方法2:

在編寫LaTeX文檔的時候,由於文檔的section較多,或者section的編寫時間各不相同,我們可能碰到如下問題:

1、由於想分開編寫各個section

2、preamble太多,想專門弄一個文件放preamble

3、想用bibtex來生成參考文獻

我分別參考了這個幾個網頁:111 222,再結合自己的上手體會,給出下面這個(還湊合的)解決方案。

 

我們首先建立一個文件夾“project”,在下面分別創建“main.tex”文件“sections”文件夾。在“main.tex”里面寫入:

\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”文件夾里:

\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盤目錄下:

\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文件中使用。

然后是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 吧,蛤蛤蛤!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM