Similar questions arise from time to time of (essentially) getting TeX to output a .tex file. The most practical way I found so far is to make all characters active (or variants that essentially amount to the same thing), and define them to store themselves to a buffer, which is then output to a file.
Then, the commands for which we wish to do more than just copy verbatim are redefined to do what we want (see the definition of \RLK@input below). There are subtelties, of course (only letters can appear in control sequence names, and not active characters).
Change InFile.tex to the file you want to "inline", and OutFile.tex to the output.
\def\RLKinputfile{InFile.tex}
\def\RLKoutputfile{OutFile.tex}
\catcode`\@=11\relax
\long\gdef\RLKgobble#1{}
\long\gdef\RLKfirstoftwo#1#2{#1}
\long\gdef\RLKsecondoftwo#1#2{#2}
\gdef\RLK{%
\begingroup%
\count0=1\relax%
\loop%
\catcode\count0=\active%
\lccode`~\count0\relax%
\lowercase{\protected\edef~{\RLKbuffer@append{\string~}}}%
\ifnum\count0<255%
\advance\count0 by 1\relax%
\repeat%
\lccode`\~`\\%
\lowercase{\let~\RLKcatchcs}%
%
\input \RLKinputfile %
\endgroup%
}
\gdef\RLKbuffer{}
\protected\def\RLKbuffer@append#1{\xdef\RLKbuffer{\RLKbuffer#1}}
\newif\if@catchcs@
\protected\def\RLKcatchcs{%
% catches the control word, naively stopping at the first non A-Za-z
\def\RLKcatchcs@csname{}%
\RLKcatchcs@aux}
\def\RLKcatchcs@aux#1{%
\@catchcs@false% We have reached the end...
\unless\ifnum`#1>`z\relax%
\unless\ifnum`#1<`A\relax%
\@catchcs@true% ...except if we are in the range A-z...
\fi\fi%
\ifnum`#1>`Z\relax%
\ifnum`#1<`a\relax%
\@catchcs@false% ...and not in the range Z-a.
\fi\fi%
\if@catchcs@%
\expandafter\RLKfirstoftwo%
\else%
\expandafter\RLKsecondoftwo%
\fi%
% If we haven't reached the end, catch one more.
{\edef\RLKcatchcs@csname{\RLKcatchcs@csname\string#1}%
\RLKcatchcs@aux}%
% If we have, stop, do the relevant \RLK@... if it exists, otherwise
% just output to the buffer. And don't forget to put #1 back in the stream.
{\ifcsname RLK@\RLKcatchcs@csname\endcsname%
\csname RLK@\RLKcatchcs@csname\expandafter\endcsname%
\else%
\RLKbuffer@append{\expandafter\RLKgobble\string\\\RLKcatchcs@csname}%
\fi%
#1%
}%
}
% Define \input{...} to have the desired behaviour: actually input the file.
% We could do the same for other commands (\usepackage, etc.)
\begingroup
\catcode`\{=\active
\catcode`\}=\active
\catcode`\(=1\relax
\catcode`\)=2\relax
\gdef\RLK@input{#1}(\RLKaux@input(#1))
\endgroup
\gdef\RLKaux@input#1{\expandafter\input \detokenize{#1} }
% Here it goes, we act.
\RLK
% Then output the \RLKbuffer to a file.
\newwrite\RLKwrite
\immediate\openout\RLKwrite \RLKoutputfile\relax
\newlinechar\endlinechar
\immediate\write\RLKwrite{\RLKbuffer}
\immediate\closeout\RLKwrite
% tex, pdftex will be stopped there,
% but latex, pdflatex will only see \relax.
\csname bye\endcsname
% this makes latex, pdflatex happy.
\documentclass{minimal}
\begin{document}
\end{document}
This should work with tex, latex, pdftex and pdflatex, and it shouldn't be hard to extend to other variants. Also, before people ask, RLK is just a random string of letters.