LaTeX users online

In total there are 14 users online :: 1 registered, 0 hidden and 13 guests (based on users active over the past 5 minutes)
Most users ever online was 1327 on Tue Nov 05, 2013 7:11 pm

Users browsing this forum: Google [Bot] and 13 guests

listings | Format only Digits Topic is solved

LaTeX Forum: Text Formatting

Add tags Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)

Postby Tedd on Wed Dec 19th, 2012

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  •  Open in online LaTeX editor
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.
Tedd
 
Posts: 6
Joined: Wed Dec 19th, 2012

Postby cgnieder on Thu Dec 20th, 2012

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  •  Open in online LaTeX editor
\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 1468 times


Maybe minted has better possibilities?

Regards
Clemens
------------------------------
New German Q&A site about TeX: http://texwelt.de/wissen
User avatar
cgnieder
Site Moderator
 
Posts: 1939
Joined: Sat Apr 16th, 2011
Location: Germany

Postby Tedd on Thu Dec 20th, 2012

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.
Tedd
 
Posts: 6
Joined: Wed Dec 19th, 2012

Postby cgnieder on Thu Dec 20th, 2012

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
Clemens
------------------------------
New German Q&A site about TeX: http://texwelt.de/wissen
User avatar
cgnieder
Site Moderator
 
Posts: 1939
Joined: Sat Apr 16th, 2011
Location: Germany

Postby Tedd on Thu Dec 20th, 2012

Oh, really, i started studying it now! It seems to be very good, thank you cgnieder!
Tedd
 
Posts: 6
Joined: Wed Dec 19th, 2012


Return to Text Formatting

LaTeX users online

Users browsing this forum: Google [Bot] and 13 guests