[Tex/LaTex] How to join two or more external .tex file using another .tex file and then compile

external filesinput

Suppose I have two tex files file1.tex and file2.tex. I want to join them in another tex file. I have tried to do this with the following one :

\documentclass{book}  
\begin{document}  
\chapter{Chapter-1}  
\section{Section-1}
\begin{theorem}
Theorem 1.1.1
\end{theorem}

\input{file1}
\input{file2}


\begin{lemma}
Lemma   
\end{lemma}
\end{document}  

But this shows error. Is it possible to do so ?

file1.tex is given below :
\input{mks}

\begin{document}





\begin{center}
{\textbf{\huge{Abstract Algebra}}}
\end{center}


\noindent\large{\textbf{Part-B}}
\begin{enumerate}
 \item Consider a group  $G$. Let $Z(G)$ be its centre. i.e.,$Z(G)=\{g \in G : gh=hg \mbox{~for all~} h \in G\}$. For $n \in \mathbf{N}$, the set of
 positive integers , define $J_n=\{(g_1,\dots,g_n)\in Z(G)\times \dots \times Z(G) : g_1\dots g_n=e\}.$ As a subset of the  direct product group 
$G\times \dots \times G$($n$ times direct product of the group $G$), $J_n$ is 
\begin{enumerate}
 \item not necessarily a subgroup.
\item a subgroup but not necessarily a normal subgroup.
\item a normal subgroup.
\item isomorphic to the direct product $Z(G)\times \dots \times Z(G)$($(n-1)$ times).
\end{enumerate}


 \item Let $G$ be a group of order $77$. Then the center of $G$ is isomorphic to
\begin{enumerate}[(a)]
\begin{multicols}{4}
 \item $\mathbf{Z}_{(1)}$
\item $\mathbf{Z}_{(7)}$
\item $\mathbf{Z}_{(11)}$
\item $\mathbf{Z}_{(77)}$
\end{multicols}
\end{enumerate}

\end{enumerate}




\end{document}  

where mks.tex is :

\documentclass[11pt,twoside,a4paper]{article}
\usepackage{amsthm,amsmath,amssymb,graphicx}
\usepackage{enumerate}

\usepackage[margin=0.45in]{geometry}
\usepackage{multicol}
\usepackage{tikz}
\DeclareMathOperator{\rank }{rank }
\DeclareMathOperator{\trace }{trace }
\DeclareMathOperator{\lcm }{lcm }
\DeclareMathOperator{\nullity }{nullity }  

 and file2.tex is :  


\documentclass{book}
\usepackage[a4paper,margin=.5in]{geometry} 

\usepackage{amsthm,xypic,graphicx}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\theoremstyle{definition}
\newtheorem*{example}{Example}
\newtheorem{definition}{Definition}
\theoremstyle{remark}
\newtheorem*{remark}{Remark}

\begin{document}
\markboth{Right}{Left}
\chapter{First Chapter}
\section{Section 1.1}
\begin{theorem}
 Theorem
\end{theorem}
\begin{lemma}
 lemma1
\end{lemma}

\begin{theorem}
 Theorem
\end{theorem}














$$
\xymatrix{& S\ar@{-}[ld]\ar@{-}[rd] &\\
\{0,a,b,s\}\ar@{-}[d] & &\{0,c\}\ar@{-}[ldd]\\
\{0,a\}\ar@{-}[rd] & & \\
& \{0\} &
}
$$
\begin{center}
 figure A.1 : chosen subsets of $S$ see book b119
\end{center}



$$
\xymatrix{ h_{C}(C) \ar[r]^{\eta(C)}\ar[d]^{h_C(f)} & T(C)\ar[d]^{T(f)} \\
h_C(X)\ar[r]^{\eta(X)} & T(X) 
}
$$
\begin{center}
 figure B.1 : commutativity of the rectangle
\end{center}


$$
\xymatrix{(h_C,T)\ar[rr]^{\theta= \theta_{C,T}}\ar[d]^{N_*(\alpha)} & & T(C)\ar[d]^{\alpha(C)}\\
(h_C,S)\ar[rr] & & S(C)
}
$$
\begin{center}
 figure B.3 : commutativity of the rectangle
\end{center}



$$
\xymatrix{(h_C,T)\ar[rr]^{\theta_C= \theta_{C,T}}\ar[d]^{N_*(f)} & & T(C)\ar[d]^{T(f)}\\
(h_D,T)\ar[rr]^{\theta_D=\theta{D,T}} & & T(D)
}
$$
\begin{center}
 figure B.4 : commutativity of the rectangle
\end{center}



$$
\xymatrix{h_C(C)\ar[r]^{\eta_{(C)}}\ar[d]^{h_C(f)} & & T(C)\ar[d]^{T(f)}\\
h_C(D)\ar[rr]^{\eta_{(D)}} & & T(D)
}
$$
\begin{center}
 figure B.5 : commutativity of the rectangle
\end{center}





$$
\xymatrix{A(U)\ar[rr]^{h(U)}\ar[d]^{r_{_{V,U}}} & & B(U)\ar[d]^{T_{U,V}}\\
A(V)\ar[rr]^{h(V)} & & B(V)
}
$$
\begin{center}
 figure B.6 : commutativity of the rectangle
\end{center}




\end{document}

Best Answer

I would recommend the standalone package:

enter image description here

Notes:

  • The geometry package was used to change the paperheight so as to make it easier to show an image here.
  • The color was used to make it easier to see the content fro the different files.
  • The filecontents package was used to package the separate files into one MWE .

Code:

\documentclass{book}

\usepackage{standalone}
\usepackage{xcolor}
\usepackage[paperheight=12.0cm]{geometry}

\usepackage{filecontents}
\begin{filecontents*}{file1.tex}
    \documentclass{book}
    \begin{document}\color{blue}  
        \chapter{Chapter-1}  
        \section{Section-1}
        Text from File 1.
    \end{document}
\end{filecontents*}

\begin{filecontents*}{file2.tex}
    \documentclass{book} 
    \begin{document}  \color{red}
        \chapter{Chapter-1}  
        \section{Section-1}
        Text from file 2.
    \end{document}
\end{filecontents*}


\begin{document}
\input{file1}
\input{file2}

\color{brown}
\chapter{Main File}
Text in main file.
\end{document}