LaTeX lstlisting not highlighting keywords when basic style is ttfamily
Asked Answered
C

3

19

I'm working on a LaTeX document and using lstlisting to display my Java source code. My setup looks like this:

\lstset{
  basicstyle=\ttfamily,
  keywordstyle=\bfseries,
  language=Java,
  frame=single,
  aboveskip=11pt,
  belowskip=11pt,
  breaklines=true,
  breakatwhitespace=false,
  showspaces=false,
  showstringspaces=false
}

The keywords are not highlighted bold when using ttfamily, but if I use small or don't specify the basic style, they're highlighted fine.

What am I missing?

Costin answered 25/3, 2010 at 11:47 Comment(0)
K
16

The default font in LaTeX, Computer Modern, does not provide a bold version of the teletype family. You can install this as the bold-extra package; see this FAQ entry for details.

Knoll answered 25/3, 2010 at 11:49 Comment(2)
@Lex: If this answer fixes your problem, you need to hit the check mark by it to "accept" the answer. See stackoverflow.com/faq for how to ask a good question. Welcome to SO!Affer
Right, I think I oversaw it yesterday, but thanks for the hint!Costin
P
1

Another option is to swap which fonts you're using for bold as described here: http://www.macfreek.nl/mindmaster/LaTeX_Bold_Typewriter_Font

Just putting this line at the top of my document worked for me on a pretty standard Ubuntu build:

\DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}
Platto answered 20/9, 2011 at 21:59 Comment(0)
A
1

There is an alternative approach (a bit simplee IMHO) here using a Courier font instead of the default Computer Modern:

% Using Courier font
\renewcommand{\ttdefault}{pcr}
\begin{lstlisting}[basicstyle=\ttfamily\bfseries]
y:=2
\end{lstlisting}

All credits goes of cause to Marco Daniel!

Anew answered 23/7, 2012 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.