11

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?

Jonas
  • 3,353
Prelude
  • 4,242
  • You can define a new keywordstyle and set the words as special keywords – Marco Daniel Jan 31 '12 at 08: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 08: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 09:01
  • http://tex.stackexchange.com/a/34952/6626 here is the answer why the above comment is necessary – Prelude Jan 31 '12 at 13:01

1 Answers1

13

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

qubyte
  • 17,299
  • 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
  • yes I mean comment , sorry ! – Prelude Jan 31 '12 at 14:26
  • I don't know how to edit my comments (This comments which I entered comment as command !) – Prelude Jan 31 '12 at 14:27
  • @Negin: You can only edit them for 5 minutes. I've altered the code. Note the * near the literate=. – qubyte Jan 31 '12 at 15:50
  • @MarkS.Everitt: Did you edit the input between the two runs? If I use your MWE from the main code, and only adjust the [flexiblecolumns=true,basicstyle=\sffamily], I get slightly different output (extra spacing before namespace). Also, don't know how the for in your MWE is aligned all the way to the left when there is a leading space in the MWE? – Peter Grill Jan 31 '12 at 17:25
  • There was some issue with spaces at the beginning of lines, so I retyped the code block. I'll make sure that the two match up when I'm not on a phone. – qubyte Jan 31 '12 at 23:49
  • @PeterGrill: Sorry, I forgot to direct the previous comment to you. – qubyte Feb 01 '12 at 01:33
  • @Negin: Check out the second image in my answer. Is that the behaviour that you are looking for? – qubyte Feb 01 '12 at 02:03
  • @MarkS.Everitt :yes it is exactly what I wans looking for . thank you . – Prelude Feb 01 '12 at 06:40
  • 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 01 '12 at 07:41