12

I am writing a book on Python and IPython. I want to insert in this book code listings of Python and IPython. For the code itself I have no problem customizing listings for my intents. Including code from IPython is a bit more tricky. I would like to make the In [x]: and Out [x]: of IPython in bold. For that I used so far the package fancyvrb, like that:

\begin{ipython}
\textbf{In [20]:} FirstQ*2
\textbf{Out[20]:} ['Jan', 'Feb', 'Mar', 'Jan', 'Feb', 'Mar']

\textbf{In [21]:} SomeList=[0]*6

\textbf{In [22]:} SomeList
\textbf{Out[22]:} [0, 0, 0, 0, 0, 0]
\end{ipython}

However it's a bit annoying to type the \textbf everytime for all the In's and Out's.

Can some one point a way in which I can define that first X columns in a verbatim environment are made in bold ?

I also tried with listings that:

\lstset{classoffset=0,
        morekeywords={In, Out,27},keywordstyle=\textbf,
    classoffset=1        
}
\begin{lstlisting}
In [27]: x=2
\end{lstlisting}

but only the words In and Out are made in Bold not the number and the brackets ...

Maybe I am asking the wrong question also ? Any insights on that will be welcomed ...

2
  • Shouldn't it be the other way around, bold the actual input and output and gray-out or normal text the In[n]/Out[n] ? Dec 22, 2010 at 11:57
  • it's a matter of taste. When you use IPython on real, the "In" is Blue, the "Out" is red. I managed also to define that with morecomment=[n][\color{red}\textbf]{In\ [}{]\:}, this will be gray in the printed version. But I'd like to keep things simple - so I chose bold face for now. Maybe if I release my book as PDF I will make everything in color. But the book should be as convenient as possible to read on gray scale.
    – oz123
    Dec 23, 2010 at 9:11

1 Answer 1

16

Whoof, I found a solution myself - I share it with you guys, since I see many people are giving positive feedback to my question:

\lstset{
morecomment=[n][\textbf]{In\ [}{]\:},
morecomment=[n][\textbf]{Out\ [}{]\:},
}

\begin{lstlisting}
In [27]: x=2, x=[123]
Out [27]: 2
\end{lstlisting}

Ipython Listings

3
  • Well done! And thanks for remembering to share your answer. Dec 21, 2010 at 12:20
  • That's a nice solution, but there's a worry that what's actually going on is you're defining the in and out to be comments, (right?), which means that they aren't "semantically" treated like code. I don't think this is a problem, but the purist would surely want a solution that still treats the text as code, rather than comment...
    – Seamus
    Dec 21, 2010 at 16:24
  • @Seamus, well so to speak, but what I care about is to differentiate the look of "In:" and "Out:" so one who reads my book, knows that in not code to type.
    – oz123
    Dec 22, 2010 at 11:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .