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?

12
  • Use a common preamble rather for you master document. You can't 'import ' packages into files that are meant for the document body.
    – user31729
    Mar 25, 2017 at 3:39
  • 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. Mar 25, 2017 at 3:59
  • 1
    @Roboticist: You can't compile the chapter file alone... you need a wrapping document of course.
    – user31729
    Mar 25, 2017 at 4:10
  • 1
    @Roboticist: \documentclass{book} \usepackage{yourmasterpreamblewitheverythingyouneed} \begin{document} \input{chapter1}\end{document}, or replace \input{chapter1} by \include{chapter1} (if really needed)
    – user31729
    Mar 25, 2017 at 4:17
  • 1
    What does didn't work mean? You cannot compile the chapter on its own regardless.
    – cfr
    Mar 25, 2017 at 4:18

1 Answer 1

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.

1
  • \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, 2017 at 5:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .