LaTeX lstlisting not highlighting keywords when basic style is ttfamily
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 d开发者_如何转开发on't specify the basic style, they're highlighted fine.
What am I missing?
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.
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}{}
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!
精彩评论