views:

67

answers:

1

Hi everyone.

I want to know if it is possible to colorize the numbers in lstlisting package from latex. For example i want all numbers be in red, even 0x0F (hex) and 0b00001111 (bin):

void SetaPWM2(unsigned char porcento)
{
  //100 * 256 = 25.600
  unsigned int val = porcento * PR2;
  val /= 25;
  //garante que tem apenas 10 bits
  val &= 0x03ff;
  //os 8 primeiros bits são colocados no CCPR1L
  CCPR2L = val >> 2;
  //os últimos dois são colocados na posição 5 e 4 do CCP1CON
  CCP2CON |= (val & 0b00001111) << 4;
}

If there is no way, is there any other package that can do it?

PS: I'm working with C language. Thanks

+2  A: 

Minted uses a Python library (Pygments) and can do any kind of highlighting for LaTeX as it is able to understand the code and not just keywords like listings does.

At least hex is supported directly, but in Pygments demo it fails with binary numbers and probably would be fine if you would just add on line to the highlighter code (probably a regexp similar to the one which parses hex).

EDIT:

In pygments\lexers\compiled.py line 60 has:

(r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),

which parses hex for C. You could add below it something like (r'0b[0-1]+[Ll]?', Number.Hex), (using Number.Hex because otherwise you would probably need to add Number.Bin to tokes.py or something).

Cloudanger
Can you give me some direction on where to modify the library in order to get binary values also highlighted?
RMAAlmeida
@RMAAx Edited the answer to give more information.
Cloudanger
Perfect! I'm having some trouble with tabs,buts this is a known bug.Thanks. (accepted and upvoted)
RMAAlmeida