This past week, I have been working on a script that converts some peculiar HTML to LaTeX in order to generate PDF. This HTML contains code samples not in PRE but in P with class assignments and a CSS that makes it look as one would expect a code sample to look. However, being in a P and not a PRE, the code samples include markup for emphasis using color and bold. I find that can process the code samples to escape the usual problem characters, such as underscores... even spaces. The embedded formatting is also easy to convert. The result is not very readable LaTeX, but that is OK because the LaTeX is being used simply as a stepping stone to PDF. Nobody is editing the LaTeX directly.
My biggest problem was with lines of code that started with spaces, which LaTeX eats. (I found this question because it was my problem.) Using verbatim does not work because of the embedded formatting. I also could not get verbatim or even Verbatim to work inside tables, and there are many tables that contain code snippets with embedded formatting.
After much banging on things, I found that, per line, I could do the following:
Plain text plain text plain text plain text plain text plain text plain text.\newline
{\tt{{\color{white}{}}\ \ \ some code \textit{here}}}\newline
{\tt{{\color{white}{}}\ \ \ some \textbf{more} code \textit{here}}}\newline
Plain text plain text plain text plain text plain text plain text plain text.
That was rather a lot to type, and managing all the curly braces was tricky, even in the script that did the conversion work, so I created an environment and applied that environment to each line... a script was doing the work, so that is not as much typing as it would be to do manually.
\newenvironment{codeLine} %
{\bgroup\tt\bgroup{\color{white}{}}}
{\egroup\egroup\newline}
With that, I could simplify the above to:
Plain text plain text plain text plain text plain text plain text plain text.\newline
\begin{codeLine}\ \ \ some code \textit{here}\end{codeLine}
\begin{codeLine}\ \ \ some \textbf{more} code \textit{here}\end{codeLine}
Plain text plain text plain text plain text plain text plain text plain text.

I am not sure why it works, but I found it when I thought of entering some white text as the first character of the line. That would work visually, but it would mess up "cut and paste" of the code examples, so I omitted the initial character and it still prevented the gobble up of initial spaces. A simple {} did not work (not for me, at least).
Apologies if this is bad style. I am not really a LaTeX expert. Mostly I just keep trying things until something seems to work... at least for a while. Wiser heads might be able to correct this or warn against it if it is working simply on a fluke that might disappear in the next LaTeX compiler release.
Also, the only package required for this would be the color package, \usepackage{xcolor}. It is possible that \color was not what was absolutely required. Other formatting work at the start of the line might work, but I do not know that.
tabularor eventabbing. – Werner Dec 06 '11 at 20:54