Not sure why this problem has eluded me but I can't seem to get fixed width font in the code portion. I thought that this question on how to change code font would do the trick but it doesn't for me.
The following code for me produces non-aligned output (the & are not aligned in the code):
\documentclass{article}
\usepackage{amsmath}
\usepackage{showexpl}
\usepackage{xcolor}
\usepackage{multirow} % Newly added for 2nd example.
\lstset{
backgroundcolor=\color{yellow},
basicstyle=\small\ttfamily,% print whole listing small
keywordstyle=\color{blue}\bfseries\underbar,
numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
%columns=fixed,
%commentstyle=\color{red},
showstringspaces=false
}
\lstloadlanguages{[LaTeX]TeX}
\begin{document}
Here is an example of LaTeX code and its output:
\begin{LTXexample}[width=0.40\linewidth,preset=\vspace{1.5mm}]
\begin{alignat*}{4}
y &= -4 &+ 3 &+4 &-7 \\
y &= &+ 3 & &-7 \\
\intertext{Therefore}
a &= b &d &= cccc &e &= d \\
a &= bbbb &d &= c &e &= d
\end{alignat*}
\end{LTXexample}
\begin{LTXexample}[width=0.40\linewidth,preset=\vspace{1.5mm},rframe={},pos=b]
\begin{tabular}{|l|l|l|l|}
\multicolumn{4}{c}{Dimensions} \\
\multirow{4}{*}{Style}
&\multirow{2}{*}{Portrait} & Width \\
& & Height \\
&\multirow{2}{*}{Landscape}& Width \\
& & Height \\
\end{tabular}
\end{LTXexample}
\end{document}

{ Portrait }when it should be{Portrait}. Most of the time additional spacing in front or following macro args doesn't matter, but sometimes it does (well in some of my not so elegantly written macros it makes a difference). – Peter Grill May 07 '11 at 18:04\intertextto my original example. With the fixes suggested here, the listing gets displayed as\ intertext(Note space following the slash). So, in this case, the spacing is important and is incorrectly displayed in the listing. I find it strange that I am having so many issues with using a fixed width font, somehting that I think should be almost trivial. – Peter Grill May 07 '11 at 18:18\mybasewidthmacro is returning the value of 4.4428pt, which is the cause of the problem for me. If I usebasewidth=4.4428ptI see the same problem. So, I guess that for me the\mybasewidthis not returning the same value as it is for you. – Peter Grill May 07 '11 at 20:41\sbox0{\small\ttfamily A}and\edef\mybasewidth{\the\wd0 }before\lstset(and Computer Modern fonts), I get a value of 4.72385pt and perfect alignment. Actually I get a space between\andintertextwhen I set the basewidth to 0.55em. – egreg May 07 '11 at 21:09\SetTrackingfrom Herbert's answer. That's actually not required with mine (and useless, if the basewidth is set to the right value) and might explain the differences. Onlybasicstyle=\small\ttfamilyis sufficient. – egreg May 07 '11 at 22:16\SetTrackingfixed things. Thanks. – Peter Grill May 07 '11 at 22:45