How to join two or more external .tex file using another .tex file and then compile?
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 loadsmypackage.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 importsmypackage.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 loadmypackage.sty
anddocmute
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}
please open \url{main.pdf}
\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.
I would recommend the standalone
package:
Notes:
- The
geometry
package was used to change thepaperheight
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}
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