TransWikia.com

How to color only literal constant in listings package?

TeX - LaTeX Asked by WAifT39 on January 2, 2021

I’m trying to show some c++ codes and want to hightlight all the literal digits with colors. I have referred to the question Coloring digits with the listings package and Colored strings by listings package, but I still have two more requirements.

First, the names of variates are colored. Consider the following code:

int t1=0;

When using the code:

lstset{language=C++,
    keywordstyle=color{blue}bfseries,
    commentstyle=color{green},
    stringstyle=ttfamilycolor{red!50!brown},
    showstringspaces=false}‎
lstset{literate=%
   *{0}{{{color{red!20!violet}0}}}1
    {1}{{{color{red!20!violet}1}}}1
    {2}{{{color{red!20!violet}2}}}1
    {3}{{{color{red!20!violet}3}}}1
    {4}{{{color{red!20!violet}4}}}1
    {5}{{{color{red!20!violet}5}}}1
    {6}{{{color{red!20!violet}6}}}1
    {7}{{{color{red!20!violet}7}}}1
    {8}{{{color{red!20!violet}8}}}1
    {9}{{{color{red!20!violet}9}}}1
}

Then the 1 in t1 is colored.

Still, there are also some attribute in the literals, such as:

unsigned t0=0u;
unsigned long long t1=314159ull;
int t2=0x14;
float t3=1e-3f;

Can these attributes be recognized and colored?

Maybe the second requirement is too demanding. However, the first one is what I must solve.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP