4

I am not expert in tex programming, sorry if I am asking a simple question. I need to include an external tex file in a master one but after excluding its preamble and the commands of \begin{document} and \end{document}. In other words, I need to include only the main text between the \begin{document} and \end{document}. I would like to do that automatically rather than removing them manually, then include the external tex files.

Your help would be highly appreciated!

jarnosc
  • 4,266
Os2015
  • 43
  • 1
    To be clear, are we taking about including specifically a LaTeX document here rather than some arbitrary file? I ask because there are some specialist packages for 'bundling up' LaTeX documents together, and those approaches are different to grabbing part of an arbitrary file. – Joseph Wright May 15 '15 at 15:03
  • Search this site for docmute and look at the excellent questions and solutions then read the documentation for docmute at http://texdoc.net/texmf-dist/doc/latex/docmute/docmute.pdf. The two items to remember. First, this works reliable for only one level deep. If you go multiple levels with an inputted file calling another inputted file there are problems with some other LaTeX Packages. Second, ALL, and it means ALL of the preambles of the main and ALL inputted file must be identical or you may have some very hard to debug errors, if it compiles at all. I used this for a 200+ file doc. – R. Schumacher May 15 '15 at 15:05
  • Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – Martin Schröder May 15 '15 at 15:51
  • I highly recommend you look at the standalone package. Numerous examples on this site. – Peter Grill May 15 '15 at 16:55

1 Answers1

3

Just a quick example with docmute. Please note that I only put some packages in the preambles to emphasize that they must be identical or debugging will be difficult, if even possible.

IMPORTANT: To prevent really difficult debugging, make very sure that the individual *.tex file with preambles actually do compile individually before running the main file.

Note: You can still use input and include the usual way to import none standalone pieces of LaTeX code.

\begin{filecontents}{one.tex}
\documentclass[10pt,letterpaper]{article}
\usepackage{docmute}
\usepackage{graphicx}
\usepackage{longtable}
\begin{document}
This is the contents of FILE ONE
\end{document}
\end{filecontents}

\begin{filecontents}{two.tex}
\documentclass[10pt,letterpaper]{article}
\usepackage{docmute}
\usepackage{graphicx}
\usepackage{longtable}
\begin{document}
This is the contents of FILE TWO
\end{document}
\end{filecontents}


\documentclass{article}
\usepackage{docmute}
\usepackage{graphicx}
\usepackage{longtable}
\begin{document}
This is main document
\hrule

This is file one using \textit{input}.
\input{one}
\hrule

This is file one using \textit{include}.
\include{one}
\hrule

This is file two using \textit{input}.
\input{two}
\hrule

This is file two using \textit{include}.
\include{two}    

\end{document}