Text FormattingCode listing in texttt style

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
viveks
Posts: 1
Joined: Thu Sep 09, 2010 9:33 pm

Code listing in texttt style

Post by viveks »

Hi,

in my latex document I include code fragements in the following way:

Code: Select all

\begin{lstlisting}[language=python, basicstyle=\footnotesize, frame=single]
def getTime(self):
	return self.config.get("thresholds", "time")
\end{lstlisting}
However, I would like to display the code in texttt or verbatim style.

I am new to Latex and do not know how I can do this.

Thanks in advance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Code listing in texttt style

Post by frabjous »

You want the whole thing in a typewriter font?

If so, then change:

Code: Select all

basicstyle=\footnotesize
to

Code: Select all

basicstyle=\ttfamily\footnotesize
Unfortunately, that'll ruin the bold in standard LaTeX, since standard LaTeX does not have a bold typewriter font. If you want to keep parts of it in bold, you'll need to load a bold typewriter font. In the preamble, put:

Code: Select all

\usepackage[T1]{fontenc}
\usepackage{luximono}
or

Code: Select all

\usepackage[T1]{fontenc}
\usepackage{lmodern}
(This will change the other fonts slightly too.)

or

Code: Select all

\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
Or something else that you like. (Look here for LaTeX typewriter fonts; click through to see which have a bold variant.)
Post Reply