1

I have to include code of subroutines in my document. To do that, I used lstlisting. I successfully include subroutines, but I want to make small change.

In visual studio, the function inputs are highlighted as gray. For example,

\begin{lstlisting}
double foo(double a){
    return a * a;
}
\end{lstlisting}

enter image description here}

In here, I want to highlight a as gray color. I can do this by adding

\lstset{
    OTHEROPTIONS,
    keywords=[2]{a},
    keywordstyle=[2]\color{gray},
}

enter image description here

However, if I set like this, it will affect the whole listings even the listings a is not an input. So, I want to do like following

\lstset{
    OTHEROPTIONS,
    keywordstyle=\color{blue},
    keywordstyle=[2]\color{gray},
}
\begin{lstlisting}
// This Code will have black `a`
void main(){
    double b = foo(a);
}
\end{lstlisting}
\begin{lstlisting}[keyword=0.5]
double foo(double a){
    return a * a;
}
\end{lstlisting}

enter image description here enter image description here

is there any possible way?


Complete code for first figure.

\documentclass[12pt]{paper}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{textcomp}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{
    tabsize=4,    
%   rulecolor=,
    language={C++},
        captionpos = t,
        basicstyle = \footnotesize\ttfamily,
        frame=lines,
        numbersep=5pt,
        numbers=left,
        numberstyle=\tiny,
        backgroundcolor=\color{white},
        aboveskip={1.5\baselineskip},
        columns=fixed,
        extendedchars=false,
        breaklines=true,
%        prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
        frame=single,
        showtabs=false,
        showspaces=false,
        showstringspaces=false,
        keywordstyle=\color[rgb]{0,0,1},
        keywordstyle=[2]\color{gray},
        commentstyle=\color{mygreen},
        stringstyle=\color{red},
        numberstyle=\color[rgb]{0.205, 0.142, 0.73},
}




% DOCUMENT STARTS
% HERE                                                                                                              
\begin{document}
\begin{lstlisting}[title=main.cpp]
// This code will have black `a`
void main(){
    double a = 0.5;
    cout << foo(a) << endl;
}
\end{lstlisting}
\end{document}
  • 2
    Can you add a complete MWE which begin with \documentclass and end with \end{document} – Salim Bou Dec 26 '16 at 11:33
  • I added complete code which will give you first figure. – Dohyun Dec 27 '16 at 3:59
4

Why don't you use escapechar option, and then set color on every item that you want? Really, I have no idea how to do that automatically.

add the following option to the lsset:

escapechar={|}, 

so you can use the code, below:

\begin{lstlisting}[title=main.cpp]
// This code will have black `a`
void main(){
    double |\color{gray}a| = 0.5;
    cout << foo(a) << endl;
}
\end{lstlisting}

the result is: enter image description here

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.