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}
Best Answer
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
to
\lstset
.One can do without knowing the character width by saying something like