1

I'm trying to input a separate file, chapter1.tex, into the mainbody.tex file. So in the mainbody.tex file I'm typing,

\documentclass[...]{book}
\blah
\blah 

\begin{document}
\input{\...\chapter1.tex}
\end{document}

Both files, chapter1.tex and mainbody.tex, are scripted with the same preamble. When I compile chapter1.tex the process ends without an error and the .pdf document is properly generated. But when I compile the mainbody.tex it gives me

! LaTeX Error: Can be used only in preamble.

and it is referring to the chapter1.tex file.

It seems that, as the main body.tex is being compiled, when the compiler reaches the \input command, it reads the preamble of the chapter1.tex file and it recognizes its commands, as commands of the main body of the mainbody.tex file, thus gives the error.

However, a mainbody.pdf file do generate even if the compiling process ends with an error.

Any idea of what causes the error and how can I get rid of it?

  • Welcome to TeX.SX! The backslash starts command/macro names and \chapter is interpreted as chapter command. This problem can be avoided by using the slash for path names: \input{/.../chapter1.tex}. The slash also works in the case of Windows. – Heiko Oberdiek Apr 17 '17 at 0:25
3

You are in fact inputting the preamble code twice - the second time after \begin{document}. So leave it out of chapter1.tex. If you need to compile chapter 1 separately, make a new main, or use \include and \includeonly in the main you have.

You may find this workflow template useful: subimport and includeonly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.