Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm fiddling with source code display (Objective-C) and the listings package. I want to colour comments the same green that appears in XCode. I determined that it has an RGB value of 67, 133, 34. To use this I need to divide by 255 since this is the format/value range of input parameters to

\definecolor{green}{rgb}{0.34,0.52,0.14}

Is there any way to write a macro in latex that divides by 255? Thanks.

In response to comment:

If I do

\usepackage{listings} % For source code display.
\usepackage{courier} % For source code display.
\usepackage{xcolor}

\lstset{basicstyle=\footnotesize\ttfamily}
\lstset{language=[Objective]C}
\definecolor{green}{rgb}{67,133,34}
\lstset{commentstyle=\color{green}}

instead of

\usepackage{listings} % For source code display.
\usepackage{courier} % For source code display.
\usepackage{color}

\lstset{basicstyle=\footnotesize\ttfamily}
\lstset{language=[Objective]C}
\definecolor{green}{rgb}{0.34,0.52,0.14}
\lstset{commentstyle=\color{green}}

the comment in my source code listing vanishes.

share|improve this question
6  
With the xcolor package you can use \definecolor{mygreen}{RGB}{67,133,34} –  egreg Mar 13 '13 at 17:06
    
@egreg Thank you. If I do that it makes the comment in my source code listing vanish. –  Matt N. Mar 13 '13 at 17:08
1  
@MattN.: You have to use capital letters (RGB) to define colours using values from 0-255. –  Jake Mar 13 '13 at 17:11
1  
And I wouldn't redefine green, also. –  egreg Mar 13 '13 at 17:12
    
@Jake Indeed! I didn't know, thank you! –  Matt N. Mar 13 '13 at 17:13

1 Answer 1

up vote 10 down vote accepted

With the package xcolor you can define colors using the range 0–255 with the RGB color model:

\definecolor{mygreen}{RGB}{67,133,34}

(notice the uppercase letters). A more complicated way using color would be

\documentclass{article}
\usepackage{color}
\makeatletter
\newcommand{\defineRGBcolor}[2]{%
  \begingroup
  \def\color@values{\@gobble}%
  \@for\next:=#2\do{%
    \count0=\next\relax
    \multiply\count0 100
    \divide\count0 255
    \edef\color@values{\color@values,0.\number\count0}%
  }%
  \edef\x{\endgroup\noexpand\definecolor{#1}{rgb}{\color@values}}\x
}
\makeatother
\defineRGBcolor{agreen}{67, 133, 34}
\definecolor{bgreen}{rgb}{0.26,0.52,0.13}
\begin{document}
\textcolor{agreen}{ABCDEF}

\textcolor{bgreen}{ABCDEF}
\end{document}

The rgb value 0.26,0.52,0.13 is what really corresponds to 67,133,34 dividing by 255.

share|improve this answer
    
Thank you very much! Btw, do you by any chance know how to colour numbers (numerical constants) in source code listings using the listings package? I didn't find out how and now I suspect it might be impossible. (unless I write my own listings package?) –  Matt N. Mar 13 '13 at 17:33
1  
@MattN. That's a very different question. I believe there's something on the site. –  egreg Mar 13 '13 at 17:36
    
@MattN. You can find it here: tex.stackexchange.com/a/32176/24276 –  user24276 Mar 13 '13 at 17:59
    
you can use the same RGB syntax in the original color package as well in most back end drivers. (All of the original ones at least). –  David Carlisle Mar 13 '13 at 18:35
    
@user24276 Thank you and egreg! –  Matt N. Mar 13 '13 at 18:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.