# How to join two or more external .tex file using another .tex file and then compile?

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}

• Can you be more precise? What is the code in file1 and file2 ? What are the errors? May 1, 2013 at 7:12
• @PaulPichaureau I have updated my post with two files. May 1, 2013 at 7:42
• It would be helpful if you reduced this to a MWE. Can the same problem not be reproduced with a subset of these files? May 1, 2013 at 7:55

I would recommend the standalone package:

# 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}

• this shows an error : "'standalone.sty' not found.\usepackage". I don't know what is this! May 1, 2013 at 7:50
• @SoumitraSen: You must be running a really old release (2009 perhaps?). I'd recommend you update to something more recent such as TeXLive2012. May 1, 2013 at 7:53
• Please, tell me one way so that I can update. I am using 'kile. on 'ubuntu10.04'. Thank you. May 1, 2013 at 8:02
• @SoumitraSen: That should be a separate question. But I would recommend you install TexLive 2012. May 1, 2013 at 8:26
• Each of the sub files cannot be compiled separately if it doesn't load xcolor. May 1, 2013 at 20:27

As a best practice, I arrange your scenario as follow to be more general. You have:

• a package mypackage.sty in which you put all packages used in both main input file and sub files (child files).

% mypackage.sty
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mypackage}[2013/05/01 v0.01 LaTeX package for my own purpose]
\RequirePackage{amsmath}
% put other packages here
\endinput

• the first input file input1.tex as a sub file (child file) as follows. It loads mypackage.sty package.

% input1.tex
\documentclass[preview,border=12pt]{standalone}
\usepackage{mypackage}
\begin{document}
Karl's students do not care about arrow tips.
$E \ne mc^2$
\end{document}

• the second input file input2.tex as a sub file as follows. It also imports mypackage.sty package.

% input2.tex
\documentclass{article}
\usepackage{mypackage}
\begin{document}
Karl's students do not care about dashing patterns.
$pV = nRT$
\end{document}

• the main input file main.tex as follows. It must load mypackage.sty and docmute packages. docmute package is used to import all stuffs (of the imported sub files) inside \begin{document} and \end{document}.

% main.tex
\documentclass{article}
\usepackage{mypackage}
\usepackage{docmute}

\begin{document}
\input{input1}
\input{input2}
\end{document}


The following simulates your scenario. Compile it with -shell-escape.

\documentclass{article}
\usepackage{filecontents}

% creating a package
\begin{filecontents*}{mypackage.sty}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mypackage}[2013/05/01 v0.01 LaTeX package for my own purpose]
\RequirePackage{amsmath}
% put other packages here
\endinput
\end{filecontents*}

% creating the first input file
\begin{filecontents*}{input1.tex}
\documentclass[preview,border=12pt]{standalone}
\usepackage{mypackage}
\begin{document}
Karl's students do not care about arrow tips.
$E \ne mc^2$
\end{document}
\end{filecontents*}

% creating the second input file
\begin{filecontents*}{input2.tex}
\documentclass{article}
\usepackage{mypackage}
\begin{document}
Karl's students do not care about dashing patterns.
$pV = nRT$
\end{document}
\end{filecontents*}

% creating the main input file
\begin{filecontents*}{main.tex}
\documentclass{article}
\usepackage{mypackage}
\usepackage{docmute}

\begin{document}
\input{input1}
\input{input2}
\end{document}
\end{filecontents*}

\usepackage{hyperref}
\begin{document}
\immediate\write18{pdflatex main}
\end{document}


## Notes

Some people use the combination of standalone document class and package. And some other people use the combination of subfiles document class and package.

But I believe my method above is much more flexible than these two methods because the included input files can make use of any document class.

• @doncherry: Upvoting this answer will get it up to 17 rep! May 1, 2013 at 21:12
• Sure, but I read neither the question nor the answer, so I won’t blindly upvote ;) May 1, 2013 at 21:13
• Oh my ghost, I redo what I did but now it is 1 rep higher. Jan 25, 2014 at 7:06

use the documentclass combine if you want to create a new document which combines several other TeX documents. Run texdoc combine to get the documentation for combine

• @Herbert can you show your answer with an example. I don,t know what is 'texdoc combine'. May 1, 2013 at 7:55
• You'll get the documentation of every class or package when running texdoc <name> in a terminal or command window
– user2478
May 1, 2013 at 7:56