LaTeX forum ⇒ Text Formattinglistings | Format only Digits Topic is solved

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

listings | Format only Digits

Postby Tedd » Wed Dec 19, 2012 3:53 am

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:

  1. variable = 40 % Format "40"
  2.  
  3. variable2 = variable1 % There is nothing formats
  4.  
  5. // This is a comment, not format nothing here 123 % There is nothing formats
  6.  
  7. var = 1..2 % Only "1" and "2"


Note: This is for RGSS, of RPG Maker.

Is this really possible? Thank you for your attention.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
cgnieder
Site Moderator
Posts: 1948
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Thu Dec 20, 2012 1:29 am

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:

  1. \documentclass{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{beramono}% monospaced font with bold variant
  4.  
  5. \usepackage{xcolor}
  6. \colorlet{comment}{black!30}
  7. \colorlet{digit}{orange}
  8. \colorlet{string}{green!80!black}
  9.  
  10. \usepackage{listings}
  11.  
  12. \lstdefinelanguage{mylanguage}{
  13. morecomment = [l]{//} ,
  14. commentstyle = \color{comment} ,
  15. morestring = [b]" ,
  16. stringstyle = \color{string} ,
  17. morekeywords = [0]{var,string} ,
  18. keywordstyle = [0]\bfseries ,
  19. sensitive = true
  20. }
  21.  
  22. \lstdefinestyle{mystyle}{
  23. language = mylanguage ,
  24. basicstyle = \ttfamily ,
  25. flexiblecolumns ,
  26. alsoletter = {0,1,2,3,4,5,6,7,8,9} ,
  27. morekeywords = [1]{1,2,3,40} ,
  28. keywordstyle = [1]\itshape\color{digit}
  29. }
  30.  
  31. \begin{document}
  32.  
  33. \begin{lstlisting}[style=mystyle]
  34. variable = 40
  35. variable2 = variable1
  36. string = "string123"
  37. // This is a comment, not format nothing here 123
  38. var = 1..2
  39. \end{lstlisting}
  40.  
  41. \end{document}


dgits.png
dgits.png (10.92 KiB) Viewed 1534 times


Maybe minted has better possibilities?

Regards
Clemens
------------------------------
New German Q&A site about TeX: http://texwelt.de/wissen

Link:
BBcode: Topic is solved[/url]" title="Click to select post's link in BBcode format" onclick="this.focus();this.select();" />
HTML: Topic is solved</a>" title="Click to select post's link in HTML format" onclick="this.focus();this.select();" />
Hide post links
Show post links

Tedd
Posts: 6
Joined: Wed Dec 19, 2012 3:33 am

Postby Tedd » Thu Dec 20, 2012 11:21 pm

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.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
cgnieder
Site Moderator
Posts: 1948
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Thu Dec 20, 2012 11:26 pm

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

Link:
BBcode:
HTML:
Hide post links
Show post links

Tedd
Posts: 6
Joined: Wed Dec 19, 2012 3:33 am

Postby Tedd » Thu Dec 20, 2012 11:54 pm

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

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 2 guests