Arbitrarily preprocess a .tex file

Alec Jacobson

August 27, 2013

weblog/

Here's a rather magical two lines you can place at the top of your (single-file) latex file to preprocess it with and arbitrary stdin-stdout script (call your script foo.sh):

\input{|"cat \jobname.tex| sed -n '/^__END__/,$ p' | sed '1 d' | ./foo.sh"}
__END__

So for example let's say your tex file is:

\input{|"cat \jobname.tex| sed -n '/^__END__/,$ p' | sed '1 d' |                                          
sed 's/[Cc]ensor[A-z]*/XXXX/g'"}                                                                          
__END__                                                                                                   
\documentclass{article}                                                                                   
\begin{document}                                                                                          
\section{Censorship}                                                                                      
Censor words starting with censor.                                                                        
\end{document} 

Then the output will be

Output of censoring latex with preprocessing script

Note: If you're using pdflatex you need to enable shell scripts by adding the --shell-escape argument.