# Combining two files - document class error

I want to combine several latex files. I started with 'combine' package. I encounter a error of \newcounter{par}. Then I moved to using input or include, but then I faced unrecognized document class. Here is my example. This is my main(mother) file

\documentclass[12pt]{article}
\usepackage{lipsum}
\title{dsjgk}%
\author{gfdl}%
\date{today}%
\begin{document}
\section{first file}
\input{1}
\end{document}


And then here is the other file(child, or subfile)

\documentclass[12pt]{article}
\usepackage{lipsum}
\begin{document}
\section{Something 1}
HERErfhwejlkfhsfhkjsdhjklfdshjk fd
jfhgkldf
kjfshjlksfdh\\\
fhkjsdhgld
jkdskjd
\\
jfkkgjdhf
\lipsum
\end{document}


And here is the error:

 C:\Users\Yashar\Desktop\test\1.tex

! LaTeX Error: Can be used only in preamble.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.1 \documentclass
[12pt]{article}
?
! Emergency stop.


I will appreciate if you can help me on this.

• Delete everything from \documentclass to \begin{document} (inclusive) and \end{document} in the file you are inputting. – cfr Dec 16 '14 at 4:13
• Thank you for the hint. Then I won't be able to compile those files separately. Am I missing something ? – Yashar Dec 16 '14 at 4:26
• Is there a reason why you want to combine the files at the LaTeX level? Maybe all you want to do is concatenate the pdf files? In that case, this can be done by using e.g. pdftk. – yori Dec 16 '14 at 8:00
• Then you can use standalone as Ulrike Fischer shows. Or you can use pdfpages to include the compiled PDFs in a master file. Or combine them outside TeX as Yori suggests. – cfr Dec 16 '14 at 16:49

use a main file of the form

\documentclass[12pt]{article}
\usepackage{lipsum}
\title{dsjgk}%
\author{gfdl}%
\date{today}%
\begin{document}
\maketitle
\section{first file}
\input{body1}
\section{second file}
\input{body2}
\end{document}


then to have a document for just section 1 have

\documentclass[12pt]{article}
\usepackage{lipsum}
\title{first section as a document}%
\author{gfdl}%
\date{today}%
\begin{document}
\maketitle
\input{body1}

\end{document}


and the shared body of the section is in body1.tex

   \subsection{zzz}
hello.....

\subsection{zzzz}
hello again.....


and of course for section2 and any other sections have a body2.tex with the shared content and a wrapper document that includes it. Note that the wrapper document including the individual sections might use a different class or options to the main document, depending on requirements.

If the subfile has the same preamble than the main file (that means if it is ok that the subfile preamble is ignored) then you can use the standalone package:

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{standalone}
\title{dsjgk}%
\author{gfdl}%
\date{today}%
\begin{document}
\section{first file}
\input{1}
\end{document}