Document ClassesProblem compiling a book

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
bgreen
Posts: 34
Joined: Wed Nov 22, 2023 11:02 am

Problem compiling a book

Post by bgreen »

I’m having trouble compiling a book. I have tried to follow the structure of Stefan Kottwitz’s book. Below is code, error and questions.

Firstly, I created a preamble. It differs from the original because there are no equations/mathematics involved so I have not loaded the amsmath and amsthm packages nor included the \newtheorem or \thereoemstyle commands, but included other packages.


I didn’t include natbob or tocbind packages because I used a list instead of a formal bibliography.

Code: Select all

\usepackage[english] {babel}
\usepackage{1modern}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage[a4paper, total={7in, 10in}]{geometry}
\usepackage{graphicx} 
\usepackage{blindtext}
\usepackage[section]{placeins}
\usepackage{subcaption}
\usepackage[colorlinks]{hyperref}
\pagestyle {empty}
\usepackage[labelformat=empty]{caption}
\usepackage {wrapfig}
\usepackage{microtype}
\usepackage{parskip}
\usepackage{geometry}
%\titleformat*{\section}{\fontsize{12}{14}\selectfont}
\graphicspath{ {D:/a_surf/BB/bgpage/} }\pagecolor{yellow!30}
Secondly, I created two chapter files

Code: Select all

\chapter{Introduction}
\section{Background}
\begin{document}
The 2004 edition of Matt Warshaw's Encyclopedia of Surfing describes bellyboarding as "all but 
\section {Challenges }
\end{document}[code]

[code] \chapter{New South Wales: Tweed Heads to the Central Coast} 
\begin{document}
\begin{figure}[htb]
  \centering
  \begin{subfigure}{0.50\textwidth}
    \includegraphics[width=\linewidth]{n4_1.jpg}
    \caption{Bellybogger.}
    \label{fig:subfig1}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.50\textwidth}
    \includegraphics[width=\linewidth]{n4_2.jpg}
    \caption{Ian Anderson .}
        \label{fig:subfig2}
  \end{subfigure}
  \caption{Source: Photos Dick Ash and Ian Anderson.}
  \label{fig:two_images}
\end{figure}
 Text
\end{document}
and then a top level document.

[/code]\documentclass{book}
\input{preamble}
\begin{document}
\tableofcontents
\addcontentsline{toc}{section}{Title of the section}
\include{Chapter1}
\include{Chapter2}
\end{document}[/code]


When I try to compile the preamble I receive this error:

! LaTeX Error: Can be used only in preamble.

I am unsure whether the problem is the preamble, the chapter codes or the top level document?

Any advice is appreciated,

Bob

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Problem compiling a book

Post by Stefan Kottwitz »

Hi Bob,

\begin{document} and \end{document} should only be in the main file once, not in the included files.

Stefan
LaTeX.org admin
bgreen
Posts: 34
Joined: Wed Nov 22, 2023 11:02 am

Problem compiling a book

Post by bgreen »

Hello Stefan,

Thanks for your prompt reply. I removed the begin/end documents from all files except the Master document.

Initially I kept getting the same error message, however, the next day I closed Texmaker and re-opened it, the code worked.

Bob
Post Reply