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.
listings
offers these commands:
\lstinline
-- for code snippets (similar to \verb
but can highlight code)
\lstlisting
-- Environment where you can put your code in display mode.
\lstinputlisting
-- for including standalone
files.
Here is an example:
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{filename.java}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
\end{filecontents*}
\usepackage{listings}
\begin{document}
\lstset{language=Java}
This is where a code snippet: \lstinline!public static HelloWorld {! comes
And here is the displayed code:
\begin{lstlisting}[frame=single]
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
\end{lstlisting}
And now to include an external source file:
\lstinputlisting[frame=single]{filename.java}
\end{document}
For your case, use \lstinputlisting
Best Answer
As @percusse suggested in his comment, I'll provide you a solution similar to Mark a pseudocode block and insert comments near it, that adopts Daniel's method illustrated in Issues and potentiality of the tikzmark macro: dynamic box adaptation and in Highlight text in code listing while also keeping syntax highlighting. The basic idea is to
mark
the text highlighted to subsequently use the marker as reference to insert the annotation. This has been done by giving a name to the node used for the highlighting instead of let this job to the\tikzmark
macro.The solution leads to:
thanks to this code:
This has been the first time I used
callouts
thus maybe there are better approaches than mine to position pointers.The code has been developed by automatically
marking
the position of the highlighting box; when you use after thetikzpicture
to position nodes, you should refer to markers as(a1), (a2)
and so on;(a1)
should be the position of the first code highlighted.To highlight a piece of code, two new delimiters are defined and are ` ` . You can redefine yours by changing this line of code:
moredelim=**[is][\btHL]{new delim}{new delim}
where, of course,new delim
should be your new delimiter character.