8

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}
Moriambar
  • 11,466
Peter Grill
  • 223,288

2 Answers2

14

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,
    ...}
egreg
  • 1,121,712
  • 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 07 '11 at 09:21
  • 1
    @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 07 '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 07 '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 07 '11 at 18:18
  • @Peter Grill: I don't obtain any of the problems you raise. – egreg May 07 '11 at 19:40
  • Intersting. I don't fully understand, but I think it has to do with this: Using Herbert's setting below of 0.55em works great. The \mybasewidth macro is returning the value of 4.4428pt, which is the cause of the problem for me. If I use basewidth=4.4428pt I see the same problem. So, I guess that for me the \mybasewidth is not returning the same value as it is for you. – Peter Grill May 07 '11 at 20:41
  • @Peter: If I add the lines \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 \ and intertext when I set the basewidth to 0.55em. – egreg May 07 '11 at 21:09
  • Not sure why we are getting different values from the same macro, nor why we see different behavior with the same settings. I still do not get correct behavior with 4.72385pt. Things are working correctly for me with 0.55em as Herbert suggested. I think I will stick with Herbert's solution as that seems to work, even though I do not like hard coding in values. If you are interested in debugging this problem further I'd be happy to assist and can email you the log files or whatever else is required, but I am happy with what works now so don't want to use up your time. Thanks. – Peter Grill May 07 '11 at 22:12
  • @Peter: probably you're using \SetTracking from 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. Only basicstyle=\small\ttfamily is sufficient. – egreg May 07 '11 at 22:16
  • Yes, this was the problem. Removing the \SetTracking fixed things. Thanks. – Peter Grill May 07 '11 at 22:45
3

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}

enter image description here

Moriambar
  • 11,466
  • 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 07 '11 at 09:20
  • 2
    @Peter: by the way: you should write the formulas in this way: y &= ..., first the & and then the =. Compare the typesetting. –  May 07 '11 at 09:25
  • @Peter: I do not understand why you updated it. The solution is the same, see my edited answer –  May 07 '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 07 '11 at 19:00
  • @Peter: ah ok, I had the wrong value for basewidth, take 0.55em –  May 07 '11 at 19:17
  • Herbert, these settings seem to work great for me. I do like egreg's solution to use a macro but that does not work for me, btu does for him for some reason. Thanks for your help. – Peter Grill May 07 '11 at 20:38