For an explanation of literate programming, check out MeatBall:LiterateProgramming.
This is a fairly hackish technique for writing a literate .emacs file, involving two-mode-mode (see MultipleModes), which EdwardOConnor used to use (he now uses noweb).
For the top of your .tex file:
% An elcomment is a section of text that should also be included
% in the generated elisp as a comment. The argument is an
% integer specifying how many spaces to indent the generated
% comments.
\newenvironment{elcomment}[1]{}{}
% Elisp environments contain code that should appear both in the
% book and in the generated .emacs file.
\newenvironment{elisp}%
{\endgraf\noindent\verbatim}%
{\endverbatim}
% ElispTeXonly environments contain code that should appear only
% in the book.
\newenvironment{elispTeXonly}%
{\endgraf\noindent\verbatim}%
{\endverbatim}
% ElispEmacsLisponly environments contain code that should appear
% only in the generated elisp file.
\let\elispEmacsLisponly=\comment
\let\endelispEmacsLisponly=\endcomment
% For inline code that shouldn't find its way into .emacs,
% \verb|(foo bar baz)| is the way to go.And for the file local variables section of your .tex file:
mode: literate-emacs-lisp
You’ll also need a copy of lp-elisp.el: