2

I use the following settings for my listings:

\lstset{
  %basicstyle=\ttfamily\tiny, % small
  basicstyle=\ttfamily\scriptsize, % middle
}

But the font in the listings is too bold for me. I tried a lot of things, but I couldn't change it.

I want to use a monospace (fixed) font in my listings, is there any other font I could use?

PS: I compile my tex files with a xelatex compiler.

EDIT #2:

Complete example:

\documentclass[12pt,german,a4paper]{scrreprt}

\usepackage{xltxtra}
\usepackage{lmodern}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{listings}

\begin{document}
\lstset{
    basicstyle=\ttfamily\scriptsize, % bold and monospace
    %basicstyle=\scriptsize, % not bold and not monospace
}
\pagestyle{plain}
\begin{lstlisting}
cntNodes++;
nodes = (DG_NODE_ID *) realloc(nodes, cntNodes * sizeof (DG_NODE_ID));
if (nodes == NULL) {
    fprintf(stderr, "Error: No memory left!\n");
    return false;
} /* if */
\end{lstlisting}
\end{document}
3
  • Make a complete example so that we can see what you use as default typewriter. Commented Mar 14, 2013 at 10:08
  • Remove the packages xltxtra, lmodern, xunicode. Then make a complete example. Commented Mar 14, 2013 at 10:14
  • So now it is really complete, I ensured also that it compiles.
    – MeJ
    Commented Mar 14, 2013 at 11:50

2 Answers 2

6

scale down the monospace font or use flexible colums or define a smaller letter space (see package fontspec). SourceCodePro is part of TL2012.

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Calibri}
\setmonofont[Scale=0.95,
%             FakeStretch=0.8,
             Mapping=tex-text]{SourceCodePro-Regular.otf}
\newfontface\SmallMono[FakeStretch=0.8,
                       Mapping=tex-text]{SourceCodePro-Regular.otf}
\usepackage{listings}
\usepackage{lipsum}

\lstset{
  basicstyle=\ttfamily\small, % middle
  columns=flexible
}
\begin{document}

\lipsum[1]
\begin{lstlisting}
\directlua{securityproblem("\today")}
\end{lstlisting}

\lstset{
  basicstyle=\SmallMono\small,
  columns=flexible}
\begin{lstlisting}
\directlua{securityproblem("\today")}
\end{lstlisting}

\end{document}

enter image description here

7
  • In PdfLatex I use a command \newcommand*\lstfont{\ttfamily\hyphenchar\font\m@ne\@noligs} to ensure that a proper minus sign "-" and not a rule is used (some fonts) in listings. How do I enforce the same thing in the example above? For example put y = --x in your source code and see the effect in the second listing.
    – Danie Els
    Commented Mar 14, 2013 at 11:42
  • 1
    @DanieEls: Simply don't use the Mapping=tex-text for the typewriter font. Commented Mar 14, 2013 at 12:23
  • 1
    or literate={-}{{\char"02D}}1 with \lstset
    – user2478
    Commented Mar 14, 2013 at 13:04
  • So I just added following line: \setmonofont[Scale=0.95]{SourceCodePro-Regular.otf} and now it works for me.
    – MeJ
    Commented Mar 14, 2013 at 13:52
  • 6
    @Herbert Since you're not a chat regular, I'd like to congratulate you here for your big achievement: reaching 100k rep might not mean much (it does, indeed), but at least it gives an occasion for thanking you for your big contribution to the TeX world.
    – egreg
    Commented Mar 15, 2013 at 20:59
1

If you put \usepackage{times} in your preamble, that will switch your monospace font to Courier, which is somewhat thinner than Computer Modern.

Alternatively, if you just put \renewcommand{\ttdefault}{pcr} in your preamble, that will switch your monospace font to Courier, but will not affect your other fonts.

You can also use \lstset{basicstyle=\fontfamily{pcr}\selectfont\scriptsize} to change the font to Courier temporarily.

5
  • But if I use your package, my headers will be also displayed thinner. I just want to change the font in the listings. And the font will not be displayed in monospace.
    – MeJ
    Commented Mar 14, 2013 at 9:40
  • If i just use: basicstyle=\scriptsize it is okay for me, but it isn't monospace.
    – MeJ
    Commented Mar 14, 2013 at 9:47
  • 1
    This won't work with xelatex. Commented Mar 14, 2013 at 10:06
  • I added some more information in my initial post!
    – MeJ
    Commented Mar 14, 2013 at 10:12
  • By loading the times package, you will also change the main text font from Latin Modern to Times Roman -- in addition to changing the monospaced font to Courier. The change to Times Roman text fonts may not be to the OP's liking.
    – Mico
    Commented Mar 14, 2013 at 16:55

You must log in to answer this question.

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