7

I'm writing a commissioned chapter for a handbook and I want to produce a pdf of three specific sections of my document to send to a colleague who is writing a different chapter for the same handbook (this is because there are some issues that span both chapters and we want to minimize overlap). Obviously, I can just copy-paste the relevant sections into a fresh tex file and produce a pdf from there, but I'm curious if there is a way of telling LaTeX "produce a pdf that contains only sections xxx, yyy, and zzz of this tex file".

I'm running TeX Live 2009, Emacs 23, and Ubuntu 12.04. I'm happy to use the command line if I need to.

EDIT, since some people seem to be thinking along these lines: I know about \includeonly{}, and I've used it in the past for this specific purpose with some large documents. I don't want to use it here because this chapter is not long enough to get into the trouble of managing a separate tex file for each section. I just want to know if it is possible to have one tex file and then produce a pdf containing only selected parts of that one tex file.

Koldito
  • 913
  • 2
    \includeonly{} command? However, this requires splitting the source file into separate files... –  Jul 03 '14 at 09:31
  • By the way: TeXLive 2009 is a tiny bit, ehm, outdated(?) ;-) –  Jul 03 '14 at 09:34
  • Well, that's effectively equivalent to pasting the relevant sections into a new tex file. I might end up doing just that, but my question is specifically whether one can compile specific regions/sections of one tex file without going to the trouble of keeping each region/section in its own subfile. – Koldito Jul 03 '14 at 09:37
  • Christian: I know, I know :/ but then: http://itre.cis.upenn.edu/~myl/languagelog/archives/000606.html – Koldito Jul 03 '14 at 09:38
  • @Well, your code will run after the splitting anyway. Another quick-and-dirty-solution: Use a dummy command that drops the content of your sections not to end up in the selective pdf. –  Jul 03 '14 at 09:40
  • 1
    I cheated a bit, but I edited my solution so that you may have only one tex file and compile on demand the part you want, using filecontents and includeonly. – Clément Jul 03 '14 at 09:52
  • given the accepted answer, this looks like a duplicate of How to create individual chapter PDFs – cmhughes Jul 03 '14 at 10:41
  • No, it's not a duplicate. The question you link to is about compiling separate tex files into one pdf. As I say in my edit, this question is about compiling selected sections of a single tex file without splitting it into several subfiles. – Koldito Jul 03 '14 at 10:57
  • Why not just compile the complete PDF and then just split that (with e.g. pdftk or using the pdfpages package) ? – Daniel Apr 08 '18 at 22:14

2 Answers2

4

One of the most used way to do that is to split your main file in several files, and to include them with the include command.

\documentclass{article}
% Preamble

\begin{document}
\include{chap1}
\include{chap2}
\include{chap3}
\include{chap4}
\end{document}

chap1.tex, …, chap4.tex being tex files containing only the content of those chapter (no preamble whatsoever).

If you want to produce a pdf containing only the chapters 2 and 3, you simply have to add

\includeonly{chap2, chap3}

in your preamble. This will need several compilation, to get the proper references, hyperlinks, toc, etc.

EDIT A kind of weird way to achieve that without splitting your main file could be to create each file with the filecontents package, i.e.:

\documentclass{article}
% Preamble
\usepackage{filecontents}
\begin{filecontents*}{chap1.tex}
% The content of chapter 1
\end{filecontents*}
% The same goes for the other files.

\begin{document}
\include{chap1}
\include{chap2}
\include{chap3}
\include{chap4}
\end{document}

You keep everything in one tex file, that is the advantage.

Clément
  • 5,591
2

This is a quick and dirty solution, but I do not recommend it really (;-))

Use the command \printthis[false]{%

before the portions of code you do not want to end up in the .pdf file and add } after the end of that portions. It hides the content as if would be a comment.

\documentclass{book}

\usepackage{blindtext}
\usepackage{etoolbox}



\newcommand{\printthis}[2][true]{%
\ifbool{#1}{%
#2%
}{%
% Drop it!!!!!
}%
}% End of \printthis
%


\begin{document}

\chapter{First}
\blindtext

\printthis[false]{%
\chapter{Two}
\blindtext

\chapter{Three}
\blindtext

}% End of \printthis

\chapter{Four}
\blindtext


\end{document}