Suppose I want to typeset a screen session in some programming language (GAP in my case, but it could be Python, BASH, or something else with an interactive read prompt just as well). So on my screen, I might have something like this:

gap> for i in [1..10] do
>   Print(i, ":");
> od;
gap> 2^10;
gap> 1/0;
Error, Rational operations: <divisor> must not be zero
not in any function at line 5 of *stdin*
you can replace <divisor> via 'return <divisor>;'
brk> 2^10;

That is, there is a command prompt gap>, after which the user enters a multi line input. After that comes a line of output, then another prompt gap>. This repeats, until a command triggers an error, at which point the prompt changes to brk>.

I would like to use the listings package (or something else) to

  1. color the prompts gap>, >, and brk>; say, blue and red;
  2. highlight keywords like for, do, and od; say, by making them bold;
  3. but only highlight the keywords in lines starting with a prompt, hence not inside the output of my commands.

The last point is the one that is causing me troubles. I couldn't figure out a way to do that. The result is often quite ugly when words like and, not, and in are highlighted as keywords in, e.g., an error message.

Similar questions have been asked before, but of those I managed to find, none asked for exactly this, resp. got an answer that does what I want.

For the record, here is the language definition I am using for now:


PS: To illustrate what I want, consider this image showing how my example from above could look (in addition to what I described above, I also set all user inputs in italics):

enter image description here

  • This is a duplicate of my question tex.stackexchange.com/questions/73893/… Apr 18, 2013 at 16:24
  • 1
    Hm, the answer to your question proposes to use mordelim as a solution -- if you look, I am already using it. But I don't see how that can overcome point 3 of my requirements, and the example picture in the solution to your question seems to indicate that it does not satisfy point 3, as "ans" in one of the output lines is printed in bold. But I need the keyword formatting to be only applied to inputs, not outputs.
    – Max Horn
    Apr 18, 2013 at 22:02
  • Hm, you are right, sorry for the confusion. Apr 19, 2013 at 7:00
  • Your specs are somewhat contradictory. For instance, in the text, you require the keywords to be bold, but they appear medium and green in your screenshot. Also, whether you actually want the user input to be slanted is unclear. Could you clarify, maybe by listing all your specs in one place?
    – jub0bs
    Mar 1, 2014 at 20:06
  • Yes, the spec says "keywords = bold" and the example instead makes them green -- either is fine, based on the assumption that once I know how to do it, I could change it suitably :-). The reason the "spec" and the screenshot differ comes from them being created at different times.
    – Max Horn
    Mar 2, 2014 at 14:42

2 Answers 2


This listings answer is about a year late, but I believe it covers all your requirements.

Edit: fixed a bug related to prompt "keywords" occurring in the middle of a line.

Desired output

desired output

Output obtained

output obtained




% switch to remember whether a prompt was found on the current line
% switch to flag whether a prompt `keyword' is an bona fide GAP prompt

% style of GAP keywords

% font shape of GAP input lines 

  basicstyle =\ttfamily,
  upquote=true,% <--- for straight single quotes

% only highlight keywords if a prompt has occured on the current line

      \color{#1}\upshape% customise the style of your > and gap> prompts here
      \aftergroup\GAPinputfshape% we trigger slanted shape after > or gap>

% Hook into InitVarsEOL (some hook executed right after an EOL) to:
% - reset the \ifprompt@GAP@ and \iftoolateforprompt@GAP@ switches
% - reset the font shape to \upshape

% Hook into PostOutput to signal that a GAP prompt
% can no occur on the current line


gap> for i in [1..10] do
>   Print(i, ":");
> od;
gap> 2^10;
gap> 1/0;
Error, Rational operations: <divisor> must not be zero
not in any function at line 5 of *stdin*
you can replace <divisor> via 'return <divisor>;'
brk> 2^10;
  • Pretty cool! I'll give it a closer look later. In the meantime, one its tiny issue I noticed: If there are extra whitespace between prompt and commands then the highlighting does not work (I tried to add an example, but found to way to make multiple spaces in a row visible in a comment). Anyway, hopefully that would be trivial to change?
    – Max Horn
    Mar 2, 2014 at 16:36
  • @Max See my edit. I'm not sure why, but it seems that using the InitVarsEOL hook (instead of the EveryLine hook) solves problem.
    – jub0bs
    Mar 2, 2014 at 16:58
  • Great, that works better. But there is another odd thing: If there is output which contains something that looks like a string, say "bla", then a subsequent > is treated as if it started a prompt. Likewise, a single " or ' throws the code off. So, I wonder, can one get it to not ignore string delimiters in output text? Just like you made it ignore (or rather: not color) keywords there? Seems harder to achieve...
    – Max Horn
    Mar 2, 2014 at 18:51
  • @Max See my edit regarding the first problem you report; I think I fixed it. I'll look into the second problem (single " or '), but it looks more complicated indeed.
    – jub0bs
    Mar 2, 2014 at 19:12
  • @Max Unfortunately, I can't think of an easy way to prevent " and ' from starting a string on your "output lines". Perhaps I'll think of something when I'm more familiar with listings... in a couple of years!
    – jub0bs
    Mar 2, 2014 at 19:19

The one drawback is that I process whole words, not subwords. Thus, od; is a separate word from od. That can be overcome, but not in this MWE. I copied your raw (unformatted) session output into the file session.in and went from there.


\parindent 0in

  \whiledo{\value{rowindex} < \nrecords}{%
    \getargsC{\csname record\roman{rowindex}\endcsname}%
    \ifthenelse{\equal{\argi}{>}  \OR%
                \equal{\argi}{gap>}  \OR%
    \whiledo{\value{wordindex} < \narg}{%
      \protectededef\thisword{\csname arg\roman{wordindex}\endcsname}%
      \ifthenelse{\equal{\thisword}{gap>}  \OR%
              \ifthenelse{\equal{\thisword}{for}  \OR%
                          \equal{\thisword}{in}  \OR%
                          \equal{\thisword}{do}  \OR%
                          \equal{\thisword}{od}  \OR%



enter image description here

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.