LaTeX Books

Cookbook

LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

click here to read the whole story

LaTeX users online

In total there are 11 users online :: 1 registered, 0 hidden and 10 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: Bing [Bot] and 10 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 writeLaTeX
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 writeLaTeX
\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 1181 times


Maybe minted has better possibilities?

Regards
Clemens
------------------------------
New German Q&A site about TeX: http://texwelt.de/wissen
Support for my packages: http://www.mychemistry.eu/forums
User avatar
cgnieder
Site Moderator
 
Posts: 1867
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
Support for my packages: http://www.mychemistry.eu/forums
User avatar
cgnieder
Site Moderator
 
Posts: 1867
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: Bing [Bot] and 10 guests