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}

2 Answers 2

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,
    ...}
10
  • 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. Commented May 7, 2011 at 9: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
    Commented May 7, 2011 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). Commented May 7, 2011 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. Commented May 7, 2011 at 18:18
  • @Peter Grill: I don't obtain any of the problems you raise.
    – egreg
    Commented May 7, 2011 at 19:40
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

6
  • 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. Commented May 7, 2011 at 9:20
  • 2
    @Peter: by the way: you should write the formulas in this way: y &= ..., first the & and then the =. Compare the typesetting.
    – user2478
    Commented May 7, 2011 at 9:25
  • @Peter: I do not understand why you updated it. The solution is the same, see my edited answer
    – user2478
    Commented May 7, 2011 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). Commented May 7, 2011 at 19:00
  • @Peter: ah ok, I had the wrong value for basewidth, take 0.55em
    – user2478
    Commented May 7, 2011 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .