This recent question (Print blue brackets in one language and black in another) made me come up with (up to) two more questions regarding the listings package:

  1. Is there an option similar to opstyle that only affects one argument (using ", , or ; as first delimiter and = as a second) and one value belonging to that argument? (using = as first delimiter and ", , or ; as second)?
  2. Is it possible to adjust the used colour in the middle of the listings environment, without it showing up as comment or line, etc.? These solutions all define colours outside the actual environment.

Neither the listings manual, nor the WikiBooks article give conclusive answers (and also fail mentioning macros including \lst@thestyle I might add), so I've looked at listings.sty, unsurprisingly to no avail, partially because the .sty-file has absolutely no remarks on any line for explanation.

For your convenience, I've included the MWE of the opstyle example and picture from the thread earlier mentioned. In my case I'd like to for instance colour the part of string: () orange, but the {}><;& part red.



\newcommand\opstyle{\color{red}} % <--- customise operator style here


  alsoletter=0123456789,% to prevent \opstyle from being applied to digits

% Hook into listings

% helper macro
  \ifnum\lst@mode=\lst@Pmode%     % If we're in `Processing' mode...
    \def\lst@thestyle{\opstyle}%  % ... redefine the style locally


int i = 0;
if(i > 1) && (1/2 < 2) {

    // one-line comment ()={}><;&

    printf("string: ()={}><;&");

      block comment ()={}><&;

opstyle example

  • \lst@thestyle is an internal macro. The listings user's guide doesn't mention it, because the "average user" is not supposed to know about it. Besides, the .sty is meant not to contain any comments on the implementation; the .sty is generated from the .dtx (which stands for "Documented laTeX"); that's where you'll find all the comments. – jubobs May 12 '14 at 12:13
  • You comment that the .sty has no comments: like most latex packages it is distributed using dcostrip which removes all the comments, the original source is a full latex document, which you probably have as texmf-dist/source/latex/listings/listings.dtx but which you can view as pdf wth texdoc listings It says amongst other things: % If you want a special printing style, modify |\lst@thestyle|. – David Carlisle May 12 '14 at 12:17
  • 2
    The short answer to your first question is: no, that's not possible. The long answer is: maybe, but only with tremendous modifications of the way listings handles delimiters. I'm not sure I understand your second question. – jubobs May 12 '14 at 12:26
  • related: tex.stackexchange.com/questions/81646/… – jubobs May 12 '14 at 12:28
  • @Jubobs The second question is about a \textcolor{<color>}{<argument>} equivalent (or a macro thereof) in the listings environments (which will usually be read verbatim). – 1010011010 May 12 '14 at 13:47

Your Answer

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

Browse other questions tagged or ask your own question.