1

I am writing a report and want to include another TEX file in it, which I update separately. I use the input command

\documentclass[11pt,a4paper]{report}
\usepackage{amsmath,amssymb}
\usepackage{pdfpages}
\usepackage{float}
\begin{document}
...
\input{extra.tex}
...
\end{document}

In this extra.tex file I need only a certain part of the document, the body of the document between BEGIN and END or a certain section. In particular, I don't need to include the file's header.

\documentclass[11pt,a4paper]{report}
% header
\usepackage{amsmath,amssymb}
\usepackage{pdfpages}
\usepackage{float}
\begin{document}
...
% include only
I want to include only this, $E = mc^2$.
% end include
...
I don't want to include this.
...
\end{document}

How do I include only a certain part of this extra.tex file in the main report report.tex?

5
  • "I don't need to include the file's header.". Does this mean that extra.tex has a header you don't want to include? Apr 25, 2018 at 7:35
  • Yes. I want to edit and compile it also as a seperate document, and also include it in a wider report without copy and paste of every update in it. Apr 25, 2018 at 7:38
  • Could it be that you're actually attempting to do input certain exercises from a collection? Apr 25, 2018 at 7:40
  • No. It is more like including a chapter in a report, and I edit the chapter in a seperate file and also compile it as a stand-alone PDF. Apr 25, 2018 at 7:43
  • I have already two answers in other questions using the clipboard package to include [graphs] (tex.stackexchange.com/a/378703/11604) or footnotes of another document but it should work equally with any other chunk of text without problem (but taking care of do not break environments, maintain definitions and so on). That solve the question?
    – Fran
    Apr 25, 2018 at 9:33

2 Answers 2

1

This is really two questions in one. Let's have one main.tex where you want to include extra.tex:


Answer to question 1:

I don't need to include the file's header.

Sharelatex has a nice overview of how to work with multiple files. You can find it here. Here I will use the standalone package in your mainfile, which should do as you want:

\documentclass{article}
\usepackage[subpreambles=true]{standalone}
\begin{document}
  \input{extra}
\end{document}

Answer to question 2

How do I include only a certain part of this extra.tex file in the main report report.tex?

I think the best way would be to wrap the parts that you'd want to toggle into environments you could control. Here I suggest the comments package:

% extra.tex
\documentclass{article}
\usepackage{comment}
% Make new environments `hide' and `keep'.
% You could make more of these for the parts you want to toggle!
\excludecomment{hide}
\includecomment{keep}
\begin{document}
  \begin{keep}
      This text will be shown $E = mc^2$.
  \end{keep}
  \begin{hide}
      This text will be hidden.
  \end{hide}
\end{document}
2
  • For the second problem I found a better solution: define in the extra.tex header a \Hide{} which does nothing and in the main.tex define the \Hide{} commend as: \newcommand{\Hide}[2]{#2} Apr 25, 2018 at 9:22
  • I don't think this is a better solution. First: it will probably generate problems if you want to insert e.g. verbatim. I suspect there are many other cases that wouldn't let you do this. Second, it will make the syntax look bad if you have a lot of text in it. I would still use environments. For alternatives, you can as @Ignasi says checkout the conditional compilation topic at CTAN. Apr 25, 2018 at 9:26
0

Divide the extra.tex in two files, one with the preamble and commands \begin{document} and \end{document}, and the second with the contents. This way you can input the contents into the new document without problems.

And if you want to selectively include only fragments of your text, use one the conditional compilation packages to select/mark the desired fragments.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .