9

I'm new to LaTeX. I want to show Python code in an article.

The idea is to change de color of the >>> symbol.

When I use <@\textcolor{red}{my_text}@> and write a text in my_text variable, it works and change the color. But when my_text is >>> it shows ¿¿¿.

How can I show >>> in blue, for exemple. I've already tried changing morekeywords= variable. And didn't work too.

Thanks a lot.

\documentclass{article}
\usepackage{listings} 
\usepackage{color}

\lstset{
    language=Python,            
    columns=flexible,       
    escapeinside={<@}{@>},
    frame=lines
    }

\begin{document}

\begin{lstlisting}[caption=Python Console, 
                    label=amb,  numbers=none]
<@\textcolor{blue}{>>>}@> print(Hello World)
<@\textcolor{red}{Hello World}@>
\end{lstlisting}

\end{document}
cgnieder
  • 66,645
  • 2
    the > symbol does not occur in the default text font (computer modern), only in the symbol font. you can get the symbol with \texttt{>>>} without entering math mode, if you're happy with the "typewriter" style. – barbara beeton Aug 26 '16 at 14:17

2 Answers2

6

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}

enter image description here

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

enter image description here

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:

enter image description here

>> 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:

enter image description here

Since now the keywords no longer are bold we need to use a teletype font that has a bold series, eg with \usepackage{lmodern}:

enter image description here

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}

enter image description here

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}

enter image description here

cgnieder
  • 66,645
3

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}

enter image description here

Arun Debray
  • 7,126
  • 2
  • 30
  • 54