# Preprocessing without compiling with etoolbox

I want to know if it is possible to do preprocessing without compiling with the etoolbox package. Basically, I want to do the following. Given a file of the following format:

\documentclass{article}
\usepackage{etoolbox}
\providebool{pre}
\setbool{pre}{true}

\begin{document}
This line is always there.

\ifbool{pre}{
This line is there if \texttt{pre}.
}{
This line is here if \texttt{\tilde pre}.
}

\end{document}


I want to know if there is a way of compiling it with so that I get as output a file containing the following text:

\documentclass{article}
\usepackage{etoolbox}
\providebool{pre}
\setbool{pre}{true}

\begin{document}
This line is always there.

This line is there if \texttt{pre}.

\end{document}


A similar feature is provided by the cleveref package via its poorman option, but it just automatically produces a sed script to modify the original file. If there is a way to achieve even that (i.e., producing automatically a sed script which can be run on the original file to get the required output) I'll be glad of learn about that too.

EDIT: clarification in response to @egreg: I actually want to get the preprocessed source file at the end of the compilation, rather than just the correctly conditionally compiled pdf output. For example, compiling the first example above with pdflatex will produce a pdf output that is exactly the same as that produced by compiling the second example. However, what I want at the end of the compilation is not just the right pdf output, but also a source file in which the rejected branches of the ifbool have been completely removed.

My use case is that I have a source file that is written to produce two different pdf outputs for two different recipients based on a flag (like the flag pre above). However, one of the outputs needs to be sent to a recipient who also requires the TeX source. I do not want this recipient to see the conditionals I put in to customize the output for them.

-
If you construct your document using This line is always there. \ifpre This line is there if \texttt{pre}.\else This line is here if \texttt{not pre}\fi then you can call the document from the command line using pdflatex \newif\pre\pretrue \input{file.tex} where you set \pretrue or \prefalse as needed. Then you don't need etoolbox. –  Werner Mar 19 '13 at 23:19
Do you want to get a file or only to do conditional compilation? –  egreg Mar 19 '13 at 23:54
@egreg: I actually want the TeX source. etoolbox already produces the right conditional pdf output on compilation. My use case is that I need to share the source file as well as the output, but I do not want the recipient to see the conditionals I put in place to customize the output for them. –  Piyush Mar 19 '13 at 23:59
Then TeX is not the right tool to use. –  egreg Mar 20 '13 at 0:00
@egreg: In fact, in my use case, TeX is the only tool I can use (any other tool would be hopelessly insufficient). Thanks for the response anyway. It does look like there is no readily available tool capable of achieving what I want to do. I had my hopes up just because cleveref (somewhat) surprisingly did have a similar feature which allowed me to remove instances of its use. –  Piyush Mar 20 '13 at 0:09

This will create a cleaned source file (but not a pdf):

\documentclass{article}
\usepackage{newfile}
\usepackage{etoolbox}
\providebool{pre}
%\setbool{pre}{true}
\setbool{pre}{false}
\begin{document}
\newoutputstream{preproc}
\openoutputfile{preproc}{preproc}
%
\ifbool{pre}{
\addtostream{preproc}{  This line is there if \texttt{pre}.}
}{
\addtostream{preproc}{  This line is here if \texttt{\textasciitilde pre}.}
}
\closeoutputstream{preproc}
\end{document}


Edit:

Following file main.tex provides preprocessed output files output.tex and output.pdf with the command pdflatex --jobname=output main

\begin{filecontents*}{output}
\documentclass{article}
\begin{document}
\input{preproc}
\end{document}
\end{filecontents*}
\documentclass{article}
\usepackage{newfile}
\usepackage{etoolbox}
\providebool{pre}
\setbool{pre}{true}
%\setbool{pre}{false}
\begin{document}
\newoutputstream{preproc}
\openoutputfile{preproc}{preproc}
%