1

I'd like to organize a thesis-like document with ut-thesis package such that there is a main .tex file as the following:

\documentclass{ut-thesis}

\degree{X}
\department{X}
\gradyear{X}
\author{X}
\title{X}

\setcounter{tocdepth}{2}

\flushbottom

\begin{document}

\begin{preliminary}

\maketitle

\begin{abstract}

\end{abstract}

\tableofcontents

\end{preliminary}

\include{chap1}

\addcontentsline{toc}{chapter}{Bibliography}
\bibliographystyle{plain}
\bibliography{thesis}

\end{document}

where chap1.tex is:

\chapter{Introduction}
This chapter is your first plan...

Now, assume that I want to define some commands or add some packages into my chapter file. For example, I may need to add amsthm package, in addition to the following commands:

\renewcommand{\qedsymbol}{$\blacksquare$}

\newtheorem{thm}{Theorem}
\newtheorem{lem}{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem{cor}{Corollary}
\newtheorem{conj}{Conjecture}
\theoremstyle{definition}
\newtheorem{defn}{Definition}
\newtheorem{exmp}{Example}
\newtheorem{rem}{Remark} 

If I add anything before chapter{}, the following error will be raised:

Can be used only in preamble. \usepackage

How to import packages and commands into chapter files?

  • Use a common preamble rather for you master document. You can't 'import ' packages into files that are meant for the document body. –  Mar 25 '17 at 03:39
  • Why would you want to add a package into the chapter? Mayxbe I don't get the question but normally you define a preamble for the whole document (all chapters). Maybe have a look at my phd thesis template -- I still use the structure for other documents: https://bedienhaptik.de/latex-template/ – Dr. Manuel Kuehner Mar 25 '17 at 03:39
  • @ChristianHupfer: But if I add my required packages to the preamble of the master document, how can the chapter files use those packages?... –  Mar 25 '17 at 03:51
  • @Dr.ManuelKuehner: Because I need some definitions and theorems within a chapter's body. So, I need to define (or add) them in the chapter file. –  Mar 25 '17 at 03:52
  • 1
    A chapter file is not standalone. It's just a convenient segmentation so that's it easier for you. To the compiler it is still one file. Have you had a look at the link that I provided? At the very end of the page you will find a zip file. – Dr. Manuel Kuehner Mar 25 '17 at 03:59
  • @Dr.ManuelKuehner: Yes. It is too complicated compared to what I need. Furthermore, you've added some packages in CommonPackages.texBut I can not find any relationship between your chapter files and those preamble files. –  Mar 25 '17 at 04:06
  • 1
    @Roboticist: You can't compile the chapter file alone... you need a wrapping document of course. –  Mar 25 '17 at 04:10
  • You can use the definitions and so on in the chapter, but you need to define them in your main document's preamble. – cfr Mar 25 '17 at 04:13
  • @ChristianHupfer: I'm actually confused! Could you show me"implementation of such wrapping document" practically by an MWE? –  Mar 25 '17 at 04:14
  • @cfr: I couldn't. As a matter of test, I'd just added \usepackage{amsmath} in the main document, then tried to use cases environment in an equation one. It didn't work. –  Mar 25 '17 at 04:17
  • 1
    @Roboticist: \documentclass{book} \usepackage{yourmasterpreamblewitheverythingyouneed} \begin{document} \input{chapter1}\end{document}, or replace \input{chapter1} by \include{chapter1} (if really needed) –  Mar 25 '17 at 04:17
  • 1
    What does didn't work mean? You cannot compile the chapter on its own regardless. – cfr Mar 25 '17 at 04:18

1 Answers1

1

In general, one adds packages and defines macros common to the entire document in the preamble, which occurs after \documentclass and before \begin{document}, the start of the document environment.

One reason for this is the behavior of some packages that use \AtBeginDocument, which appends tokens to a token list that is expanded when the document environment begins, as explained here.

There are a few exceptions to this, but those exceptions are covered in the documentation and discussion about classes like memoir and maybe packages like cmap. In that case, one must use \RequirePackage, a topic that goes somewhat afield of this question here.

For the differences between \include and \input see this article. Again, you generally put common macros that you use document-wide into the preamble. That prevents an unexpected situation where you may be inside a scope (created by the grouping tokens { }, synonyms \bgroup ... \egroup, and additionally \begingroup ... \endgroup) and define a macro, etc. When you leave that scope, unless you used \global, that definition goes away. There are other caveats as well.

Doing things the usual way helps prevent unwanted errors, which can include not only the situation where document compilation halts, but also where compilation succeeds, yet subtle and unexpected errors might arise.

  • \RequirePackage cannot be used after \begin{document} either. On the other hand, it is possible to define new commands in the body of a document, and it doesn’t matter at all whether these definitions are put in the main file or in a file that is \input. – GuM Apr 24 '17 at 05:10