Text Formattinglistings | Format only Digits

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Tedd
Posts: 6
Joined: Wed Dec 19, 2012 3:33 am

listings | Format only Digits

Post by Tedd »

Hello guys.

With the listings package I want only digits in orange. No strings or comments. I've looked at many topics about it on the internet but are not very satisfactory. I'll explain below:

Code: Select all

variable = 40  % Format "40"

variable2 = variable1  % There is nothing formats

// This is a comment, not format nothing here 123  % There is nothing formats

var = 1..2 % Only "1" and "2"
Note: This is for RGSS, of RPG Maker.

Is this really possible? Thank you for your attention.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

listings | Format only Digits

Post by cgnieder »

Hi Tedd,

Welcome to the board!

I am not aware of an automatic way for this. The only way I can think of is a not very convenient one: declare the digits to be letters (as far as »listings« is concerned) and declare all digits that should be formatted as keywords with an own keywordstyle:

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}% monospaced font with bold variant

\usepackage{xcolor}
\colorlet{comment}{black!30}
\colorlet{digit}{orange}
\colorlet{string}{green!80!black}

\usepackage{listings}

\lstdefinelanguage{mylanguage}{
  morecomment  = [l]{//} ,
  commentstyle = \color{comment} ,
  morestring   = [b]" ,
  stringstyle  = \color{string} ,
  morekeywords = [0]{var,string} ,
  keywordstyle = [0]\bfseries ,
  sensitive    = true
}

\lstdefinestyle{mystyle}{
  language     = mylanguage ,
  basicstyle   = \ttfamily ,
  flexiblecolumns ,
  alsoletter   = {0,1,2,3,4,5,6,7,8,9} ,
  morekeywords = [1]{1,2,3,40} ,
  keywordstyle = [1]\itshape\color{digit}
}

\begin{document}

\begin{lstlisting}[style=mystyle]
 variable = 40
 variable2 = variable1
 string = "string123"
 // This is a comment, not format nothing here 123
 var = 1..2
\end{lstlisting}

\end{document}
dgits.png
dgits.png (10.92 KiB) Viewed 7028 times
Maybe minted has better possibilities?

Regards
site moderator & package author
Tedd
Posts: 6
Joined: Wed Dec 19, 2012 3:33 am

listings | Format only Digits

Post by Tedd »

Oh, really thanks cgnieder!

So you think the minted package is best for this case?

I'm new in LaTeX, I am studying just a month ago. I started with listings beacause i found more material, but i will find out about this package. As would be done in minted this example? Could you exemplify?

Thankful. Tedd.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

listings | Format only Digits

Post by cgnieder »

Tedd wrote:So you think the minted package is best for this case?
Actually I don't know. I fairly familiar with »listings« but I have never used »minted« before. I just heard that it is quite powerful, too. So I think you should at least take a look.

Regards
site moderator & package author
Tedd
Posts: 6
Joined: Wed Dec 19, 2012 3:33 am

listings | Format only Digits

Post by Tedd »

Oh, really, i started studying it now! It seems to be very good, thank you cgnieder!
Post Reply