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 trying to type my C++ homework by LaTeX in order to prepare it as a pdf file. I want to have my keywords in blue, my numbers in violet and my strings in reddish brown. I could do the two first by looking up the documentation and other questions here, but I have no idea how to change the color of my strings.

\documentclass{article}
‎‎
\usepackage{etoolbox}
\usepackage{xcolor}‎
\usepackage‎{listings}‎
‎
\newtoggle{InString}{}% Keep track of if we are within a string
\togglefalse{InString}% Assume not initally in string

\newcommand*{\ColorIfNotInString}[1]{\iftoggle{InString}{#1}{\color{violet}#1}}%
\newcommand*{\ProcessQuote}[1]{#1\iftoggle{InString}{\global\togglefalse{InString}}{\global\toggletrue{InString}}}%
\lstset{literate=%
    {"}{{{\ProcessQuote{"}}}}1% Disable coloring within double quotes
    {'}{{{\ProcessQuote{'}}}}1% Disable coloring within single quote
    {0}{{{\ColorIfNotInString{0}}}}1
    {1}{{{\ColorIfNotInString{1}}}}1
    {2}{{{\ColorIfNotInString{2}}}}1
    {3}{{{\ColorIfNotInString{3}}}}1
    {4}{{{\ColorIfNotInString{4}}}}1
    {5}{{{\ColorIfNotInString{5}}}}1
    {6}{{{\ColorIfNotInString{6}}}}1
    {7}{{{\ColorIfNotInString{7}}}}1
    {8}{{{\ColorIfNotInString{8}}}}1
    {9}{{{\ColorIfNotInString{9}}}}1
}‎
‎‎
\begin{document}‎‎‎‎
\lstset{language=C++ , keywordstyle=\color{blue}\bfseries ,commentstyle=\color{white}, stringstyle=\ttfamily, showstringspaces=false}‎
‎‎\begin{lstlisting}[basicstyle=\ttfamily]
#include<iostream>
‎‎using namespace std ;
 for (col = 1 ; col<= 5 ; col++)
      cout << "Enter your number : " ; 
      cin >> a[row][col] ; 
\end{lstlisting}
\end{document}‎

This is the output which I get:

Output that I get.

Where I want "Enter your number " in reddish-brown. So what should I do for that?

share|improve this question
    
You can define a new keywordstyle and set the words as special keywords –  Marco Daniel Jan 31 '12 at 8:54
    
I want having it for all strings which I type in my program , not only a few words , actually any thing which is between " " . –  Negin Jan 31 '12 at 8:57
    
I used the command which prevents the numbers between double qoutes being colored in violet , but don't know how to make them to be colored in brown in string , I don't know how to change no color for " " and ' ' to a specific color –  Negin Jan 31 '12 at 9:01
    
tex.stackexchange.com/a/34952/6626 here is the answer why the above comment is necessary –  Negin Jan 31 '12 at 13:01
add comment

1 Answer 1

up vote 6 down vote accepted

The following code produces the result I think you want:

\documentclass{article}
\usepackage{xcolor,listings}
\begin{document}

\lstset{language=C++,
    keywordstyle=\color{blue}\bfseries,
    commentstyle=\color{green},
    stringstyle=\ttfamily\color{red!50!brown},
    showstringspaces=false}‎
\lstset{literate=%
   *{0}{{{\color{red!20!violet}0}}}1
    {1}{{{\color{red!20!violet}1}}}1
    {2}{{{\color{red!20!violet}2}}}1
    {3}{{{\color{red!20!violet}3}}}1
    {4}{{{\color{red!20!violet}4}}}1
    {5}{{{\color{red!20!violet}5}}}1
    {6}{{{\color{red!20!violet}6}}}1
    {7}{{{\color{red!20!violet}7}}}1
    {8}{{{\color{red!20!violet}8}}}1
    {9}{{{\color{red!20!violet}9}}}1
}

‎‎\begin{lstlisting}[basicstyle=\ttfamily]
#include<iostream>
using namespace std;
for (col = 1 ; col<= 5 ; col++) {
      cout << "Enter your number : "; 
      cin >> a[row][col];
}
\end{lstlisting}

\end{document}

I removed everything not necessary, and modified the \lstset to include stringstyle=\ttfamily\color{red!50!brown}. The star before the first argument of the literate programming \lstset protects numerals in environments such as comments and strings.

enter image description here

Playing around a bit gets you much more horizontally compact code, which may be clearer to read. I've put additions in the square brackets here, but they can also go in \lstset

\begin{lstlisting}[flexiblecolumns=true,basicstyle=\sffamily]
#include<iostream>
// Test comment.
using namespace std ;
for (col = 1 ; col<= 5 ; col++) {
      cout << "Enter your number : "; 
      cin >> a[row][col];
}
\end{lstlisting}

enter image description here

share|improve this answer
    
I've examined that before , but if I want to have my numbers in violet this comment will not work anymore ! if you try it with my code ,where number's color is defined , I suppose all should change is \newcommand*\processcode , but I have no idea how to change it ! –  Negin Jan 31 '12 at 10:27
1  
The numbers are in violet now. –  qubyte Jan 31 '12 at 10:51
    
the above comments were all nececarry because they make the numbers that are among the strings not to get violet , for instance if we type "Ente444" ,444 will be in purpul and not in the color of string , and all these comments which you provided only work in the abstence of the comments I mentioned ! –  Negin Jan 31 '12 at 12:59
    
@Negin I suspect you are using the word "comment" to mean "command", which makes your comments somewhat confusing. –  Bruno Le Floch Jan 31 '12 at 13:14
1  
Took me a while to get there! I had to read the relevant part of the documentation three or four times before that star jumped out at me. –  qubyte Feb 1 '12 at 7:41
show 8 more comments

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.