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:

\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]

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:

        morekeywords={In, Out,27},keywordstyle=\textbf,
In [27]: x=2

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

  • 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] ? – Yossi Farjoun Dec 22 '10 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 '10 at 9:11

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

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

In [27]: x=2, x=[123]
Out [27]: 2

Ipython Listings

  • Well done! And thanks for remembering to share your answer. – Andrew Stacey Dec 21 '10 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 '10 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 '10 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.