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.

\newtoggle{InString}{}% Keep track of if we are within a string
\togglefalse{InString}% Assume not initally in string

    {"}{{{\ProcessQuote{"}}}}1% Disable coloring within double quotes
    {'}{{{\ProcessQuote{'}}}}1% Disable coloring within single quote
\lstset{language=C++ , keywordstyle=\color{blue}\bfseries ,commentstyle=\color{white}, stringstyle=\ttfamily, showstringspaces=false}‎
‎‎using namespace std ;
 for (col = 1 ; col<= 5 ; col++)
      cout << "Enter your number : " ; 
      cin >> a[row][col] ; 

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?

  • 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 " " . – Prelude 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 – Prelude Jan 31 '12 at 9:01
  • tex.stackexchange.com/a/34952/6626 here is the answer why the above comment is necessary – Prelude Jan 31 '12 at 13:01

The following code produces the result I think you want:



using namespace std;
for (col = 1 ; col<= 5 ; col++) {
      cout << "Enter your number : "; 
      cin >> a[row][col];


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

// Test comment.
using namespace std ;
for (col = 1 ; col<= 5 ; col++) {
      cout << "Enter your number : "; 
      cin >> a[row][col];

enter image description here

  • 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 ! – Prelude 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 ! – Prelude 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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