Failsafe ways of using verbatim commands
FWIW, ConTeXt provides a buffer
environment which is very useful for such situations. In ConTeXt MkII (pdftex and xetex engines), the contents of the buffer are stored in an external file, while in MkIV (luatex engine), the contents of a buffer are stored in memory. So, one could do something like what you want in ConTeXt as follows
\startbuffer[so-example]
\so{letterspacing}, \ul{underlining}
\st{overstriking} and \hl{highlighting}
\stopbuffer
\doifdefined\so{\typebuffer[so-example]}
LaTeX does not really have anything equivalent to buffers. What comes closet is the filecontents
package (see this question). So you can try (untested)
\begin{filecontents}{\jobname-so-example}
\begin{LTXexample}
\so{letterspacing}, \ul{underlining}
\st{overstriking} and \hl{highlighting}.
\end{LTXexample}
\end{filecontents}
\IfDefined\so{\input{\jobname-so-example}}
You can also wrap the last bit behind a macro:
\def\ShowExample#1
{\IfDefined{\csname#1\endcsame}{\input{\jobname-#1-example}}
this doesn't answer the question completely or directly, but maybe the technique can be adapted for your purpose. here is a small example file that "does its thing" and then prints itself out following the demonstration.
\documentclass{article}
\usepackage{wasysym}
\usepackage{verbatim}
\nofiles
\newcommand{\opluslhrim}{\mathbin{\rlap{$\Leftcircle$}{+}}}
\newcommand{\oplusrhrim}{\mathbin{{+}\llap{$\Rightcircle$}}}
\begin{document}
\section*{Semidirect sums}
Constructed using \texttt{wasy}'s left and right half circles:\\
$\Leftcircle \quad + \quad A \opluslhrim B \quad
+ \quad \Rightcircle \quad C \oplusrhrim D$
In display:
\[
\Leftcircle \quad + \quad A \opluslhrim B \quad
+ \quad \Rightcircle \quad C \oplusrhrim D
\]
\vspace{3\baselineskip}
\verbatiminput{\jobname.tex}
\end{document}
i've used this technique to accumulate a collection of "howto" files that give solutions for problems that are already solved, for reference by the ams production staff. of course, this technique does assume that there is nothing in the test code that will cause the job to halt with an error.