You can use any (All)TeX macro you wish within listings by enclosing it within escape brackets,
(*@ \textcolor{blue}{code} @*)
The funny eyes are the escapes you define them as follows:
\lstset{escapeinside={(*@}{@*)}}
For highlighting the soul
package provides a nice highlight macro \hl
, try it in the following minimal
\documentclass{article}
\usepackage{soul,listings,xcolor}
\lstnewenvironment{teX}[1][]
{\lstset{language=[LaTeX]TeX}\lstset{escapeinside={(*@}{@*)},
numbers=left,numberstyle=\normalsize,stepnumber=1,numbersep=5pt,
breaklines=true,
%firstnumber=last,
%frame=tblr,
framesep=5pt,
basicstyle=\normalsize\ttfamily,
showstringspaces=false,
keywordstyle=\itshape\color{blue},
%identifierstyle=\ttfamily,
stringstyle=\color{maroon},
commentstyle=\color{black},
rulecolor=\color{black},
xleftmargin=0pt,
xrightmargin=0pt,
aboveskip=\medskipamount,
belowskip=\medskipamount,
backgroundcolor=\color{white}, #1
}}
{}
\begin{document}
\begin{teX}
\test{this is some code}
(*@ \textcolor{blue}{code} @*)
(*@ \hl{yellow code} @*)
\end{teX}
\end{document}
I normally use an environment for "listings" settings, but you can use any other code style as well.
Another useful trick is to use the "listings" setting for emphasis in macros, like this:
\gdef\emphasis#1{\lstset{emph={begin,end,#1},
emphstyle={\itshape\ttfamily\textcolor{blue}}}}
\gdef\hlemphasis#1{\lstset{emph={begin,end,#1},
emphstyle={\hl{blue}}}}
This way you can emphasize keywords.
Under the assumption that you want, not only to reference, but also to typeset your C++ source code somewhere in your document, you can use the listings
package, define a character to escape to LaTeX (left quote in my example), and write a \label{...}
in your source code within a pair of such escape characters. Just make sure you choose your escape character wisely; if it occurs in places where you don't intend to escape to LaTeX, you'll run into trouble when you try to typeset your code.
If you plan to always place your LaTeX labels within an end-of-line C++ comment, and you don't want those comments to be printed in your PDF, you can define a listings
"to-the-end-of-the-line" delimiter to uniquely identify such comments, using, for instance,
moredelim=[il][]{//latexlabel}
The listings
package will then detect any occurence of //latexlabel
and will not print it; note that listings
will print any unescaped text that follows //latexlabel
, though.
Feel free to customise/shorten that delimiter; //latexlabel
may be a bit too long for your taste.
\documentclass{article}
\usepackage{filecontents} % only to write your code to hello.cpp
\begin{filecontents*}{hello.cpp}
#include <iostream>
int main() {
std::cout << "Hello World" << std::endl; //latexlabel `\label{sc:greet}`
return 0; // another comment that does get typeset
}
\end{filecontents*}
\usepackage{listings}
\lstset
{
language=C++,
escapechar=`,
numbers=left,
moredelim=[il][]{//latexlabel},
}
\begin{document}
\section{Comments on the implementation}
In this program, line~\ref{sc:greet} greets the user.
\section{Source code}
\lstinputlisting{hello.cpp}
\end{document}
Best Answer
With the
verbatimbox
package, optional arguments (in the form of macros) can be passed to the package's verbatim environments. This allows all sorts of customization. In my MWE below, I test the code line number. If it is in the range 8-18, it applies a background\colorfield
to the listing. Also, I apply red line numbers if the code line number is in that range, or black numbers if it is outside of it (the default is no line numbers).In addition, I check that if on code line 11, I call attention to that line with a boxed comment.
You can choose the background color method, the colorized line number method, the highlighted box method, all three, some combination thereof, or something completely different, all of which are customized through your macro definitions. The package keeps track of line numbers through the counter
VerbboxLineNo
, which may be utilized in your macros.