Lyx's "Insert -> Program Listing" is internally based on the listings
package, which provides a a plenty full of options to influence the typesetting. You can enter such listings
options on the "Advanced" page of the "Listing Settings" dialog. For instance, entering
keywordstyle={\color{blue}}
there would typeset all keywords in blue color. For a quick start take a look at other questions with the listings tag – or consult the (excellent!) listings
documentation
The reason you do not "see any difference" in your current setup may be a font issue: The culprit is the standard typewriter
font in LaTeX, which does not have a boldface version; however, the default of listings
is keywordstyle=\bfseries
, that is, keywords are typeset in boldface. So just try another typewriter
font in "Document -> Settings -> Fonts" and see if this makes a difference.
(BeraMono is a good font for typesetting listings.)
It is always good to post a minimal and compilable example, not just code snippets. This way, the answerers do not have to guess what's happening with your problem.
I guess that you are looking for something like this.
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{language=Python,
morekeywords={as,__init__,MyClass},
keywordstyle=\color{teal}\bfseries,
}
\begin{document}
\lstinputlisting{guess.py}
\end{document}
where guess.py
is your sample code snippet. I just added as on the last line to show that morekeywords
works.
class MyClass(Yourclass):
def __init__(self, myvar, yours):
bla bla bla... as
Here is the output.
You can also remove __init__
from morekeywords
option and use the answers in How to I emphazise all words beginning with ` in an lstlisting and Listings language definition keyword suffixes. So you may put the following code snippet into your preamble.
\lstset{language=Python,
morekeywords={as,MyClass},
keywordstyle=\color{teal}\bfseries,
keywordsprefix=_,
}
Let me know if this works for you.
Best Answer
The standard way of doing this is to use the
listings
package. It allows a wide range of formatting for the output, can choose to display only part of an input file and so on. The package also comes with a number of predefined languages it understands, including Python.