# Fixed Width Font with LTXexample environment

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,
showstringspaces=false
}

\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}

-

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,
...}

-
This works, even with the new example that added to the MWE. I had just gotten to like the beramono font as Herbert suggested, but that solution fails with the new example. –  Peter Grill May 7 '11 at 9:21
@Peter Grill: the width of the Bera Mono characters is 0.6em. I'll add some code to the answer that allows to choose automatically the right width. –  egreg May 7 '11 at 11:38
Thanks, that works pretty good. Perhaps this is a more general typesetting issue, but there is additional spacing added where I don't think there should be. In the second example the listing shows { 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 7 '11 at 18:04
Actually found a case this additional spacing does matter. I added \intertext to 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 7 '11 at 18:18
@Peter Grill: I don't obtain any of the problems you raise. –  egreg May 7 '11 at 19:40

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,
showstringspaces=false}

\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}


-
Thanks. I liked this font, and it works almost all the time. There is one example, that I have added to the MWE example where I noticed that the alignment is not correct. If you know the adjustments necessary I'd like to continue with the beramono font. Changing the \basewidth as egreg suggested fixed things. –  Peter Grill May 7 '11 at 9:20
@Peter: by the way: you should write the formulas in this way: y &= ..., first the & and then the =. Compare the typesetting. –  Herbert May 7 '11 at 9:25
Thanks Herbert. I have updated the MWE. –  Peter Grill May 7 '11 at 17:50
@Peter: I do not understand why you updated it. The solution is the same, see my edited answer –  Herbert May 7 '11 at 18:47
Yes, the alignment looks good at first glance. BUT, in the listing there is an extra space following the slash in \intertext, and \multirow, which makes a difference if someone is using the listing as an example of how to do write LaTeX code (which is the whole purpose of the listing). I also do not like that spaces get added following and open brace, and before a closing brace. For example{Portrait} gets displayed as { Portrait }`, and this actually does matter for some of my macros (which I realize do need to fix someday). –  Peter Grill May 7 '11 at 19:00