Fixed Width Font with LTXexample environment
The problem is that the default setting of listings with "fixed columns" has a space 0.6em wide, while the characters in Computer Modern Typewriter are 0.5em wide. So you need only to add the options
columns=fixed,basewidth=.5em,
to \lstset
.
One can do without knowing the character width by saying something like
\sbox0{\small\ttfamily A}
\edef\mybasewidth{\the\wd0 }
\lstset{
basicstyle=\small\ttfamily,% print whole listing small
columns=fixed,basewidth=\mybasewidth,
...}
you should use a good looking typewriterfont like `beramono'
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.82]{beramono}
\usepackage{microtype}
\usepackage{amsmath}
\usepackage{showexpl}
\usepackage{xcolor}
\usepackage{multirow} % Newly added for 2nd example.
\lstset{
backgroundcolor=\color{yellow},
basicstyle=\small\ttfamily\SetTracking{encoding=*}{-60}\lsstyle,
basewidth=0.55em,
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}