Make >>> a keyword in listings enviorment
You can add >>>
with morekeywords
but you also have to change >
to be a letter with the alsoletter
option for it to work:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
language = Python ,
columns = flexible ,
escapeinside = {<@}{@>} ,
frame = lines ,
alsoletter = > ,
morekeywords = {>>>}
}
\begin{document}
\begin{lstlisting}[caption=Python Console, label=amb, numbers=none]
>>> print(Hello World)
\end{lstlisting}
\end{document}
If you want it blue but not all keywords blue you can add it to a different class of keywords and set a style for said class by using the optional argument of the corresponding options:
morekeywords = [2]{>>>} ,
keywordstyle = [2]\color{blue}\bfseries
As soon as you add \usepackage[T1]{fontenc}
(which in general is a good idea) to the example you won't be happy anymore, though:
>>
is turned into »
(as a ligature) then. In that case personally I'd use teletype as font for the code (I'd do that in any case, but that's just me) with basicstyle = \ttfamily
:
Since now the keywords no longer are bold we need to use a teletype font that has a bold series, eg with \usepackage{lmodern}
:
If you don't want teletype code but still want to use T1
encoding and if you don't need >>
to be treated as a ligature anywhere else in your document you could also add the microtype
package and disable the ligature
\usepackage{microtype}
\DisableLigatures[>]{encoding = T1}
(Using the microtype
package will do your document good, anyway…)
without teletype:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{microtype}
\DisableLigatures[>]{encoding = T1}
\lstset{
language = Python ,
columns = flexible ,
escapeinside = {<@}{@>} ,
frame = lines ,
alsoletter = > ,
morekeywords = [2]{>>>} ,
keywordstyle = [2]\color{blue}\bfseries
}
\begin{document}
\begin{lstlisting}[caption=Python Console, label=amb, numbers=none]
>>> print(Hello World)
\end{lstlisting}
\end{document}
with teletype:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{microtype}
\DisableLigatures[>]{encoding = T1}
\lstset{
basicstyle = \ttfamily ,
language = Python ,
columns = flexible ,
escapeinside = {<@}{@>} ,
frame = lines ,
alsoletter = > ,
morekeywords = [2]{>>>} ,
keywordstyle = [2]\color{blue}\bfseries
}
\begin{document}
\begin{lstlisting}[caption=Python Console, label=amb, numbers=none]
>>> print(Hello World)
\end{lstlisting}
\end{document}
One way to do this is to switch the font encoding, using \usepackage[T1]{fontenc}
. This changes a textmode >
into an actual >
sign, rather than ¿
.
This also parses >>
into a guillemet »
, but you can fix this by separating them like this: >{}>{}>
.
Full code:
\documentclass{article}
\usepackage{xcolor}
\usepackage[T1]{fontenc} % <-- here
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
language=Python,
escapeinside={<@}{@>},
}
\begin{document}
\begin{lstlisting}
<@\textcolor{blue}{\texttt{>{}>{}>}}@> print(Hello World) % <-- here
<@\textcolor{red}{Hello World}@>
\end{lstlisting}
\end{document}