# Compiling a .tex file into many pdf without include?

I would like to have a single .tex file, with a main section and an appendix. I would want however to compile it into two distinct pdfs.

There are solutions using \include, see How to create individual chapter PDFs or Splitting a large document into several files, but this is not using a single .tex file. The reason I want a single .tex file is that I am using R Sweave, not showing input code in the main document, but want the input code to be shown in a companion document.

Following LaTeX/Sweave - How to reproduce code blocks in an appendix?, my .tex file will be:

\documentclass{article}
\begin{document}

Some stuff here to go in first pdf

<<block1,eval=TRUE,echo=FALSE>>=
plot(density(1:10))
@

\appendix
Some stuff here to go in second pdf

<<all-blocks>>=
<<block1>>
@
\end{document}

• In principle this is similar to common requests IF condition 1 make 1.pdf or IF condition 2 make 2.pdf OR OR see for example tex.stackexchange.com/questions/162042/… – user170109 Jan 9 '19 at 20:53
• I'm not quite sure I understand. Your linked How to create individual chapter PDFs has several answers that use a single tex file (similarly for KJO's link). Are they not sufficient? But furthermore, why do you require only a single tex file? Why not one tex file for the first part, and one tex file for the appendix, and include the code into both? – Teepeemm Feb 9 '19 at 13:52

You can try this

\newif\ifmain
%\maintrue   %to be commented or not
\documentclass{article}
\ifmain
\let\olddocument\document
\let\oldappendix\appendix
\long\def\document#1\appendix{\olddocument\oldappendix}
\else
\def\appendix{\end{document}}
\fi
\begin{document}

Some stuff here to go in first pdf

\appendix
Some stuff here to go in second pdf

\end{document}