It seems that tikzmark names can't be re-used. In the code below, I try to draw a box outline in the first lstlisting section, and a filled box in the second lstlisting section. In each lstlisting, I place two tikzmarks named "a" and "b". Unfortunately, the result is that the second lstlisting gets both the outline box and the filled box, but the first lstlisting gets neither.
If I rename the tikzmarks in one of the sections (say, "a1" and "b1") things work as expected. Is there a way to "forget" tikzmarks? I'm writing a document that will have a lot of code snippets, and it will be really tedious to keep track of unique tikzmarks for each of them.
Here's the sample code:
\documentclass{article}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc,shapes,positioning,tikzmark}
\begin{document}
\thispagestyle{empty}
\section{Box}
\begin{lstlisting}[escapechar=!,basicstyle=\ttfamily]
#include <stdio.h>
#include <math.h>
int main () {
double c=-1;
double z=0;
int i;
printf (``For c = %lf:\n'', c );
for ( i=0; i<10; i++ ) {
printf ( !\tikzmark{a}!``z %d = %lf\n''!\tikzmark{b}!, i, z );
z = pow(z,2) + c;
}
}
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\draw[red,rounded corners]
([shift={(-3pt,2ex)}]pic cs:a)
rectangle
([shift={(3pt,-0.65ex)}]pic cs:b);
\end{tikzpicture}
\section{Filled Box}
\begin{lstlisting}[escapechar=!,basicstyle=\ttfamily]
#include <stdio.h>
#include <math.h>
int main () {
double c=-1;
double z=0;
int i;
printf (``For c = %lf:\n'', c );
for ( i=0; i<10; i++ ) {
printf ( !\tikzmark{a}!"z %d = %lf\n"!\tikzmark{b}!, i, z );
z = pow(z,2) + c;
}
}
\end{lstlisting}
\begin{tikzpicture}[remember picture,overlay]
\draw[fill=gray,opacity=0.1]
([shift={(-3pt,2ex)}]pic cs:a)
rectangle
([shift={(3pt,-0.65ex)}]pic cs:b);
\end{tikzpicture}
\end{document}


ayou mean when stuff is overlaid on the page. So you don't really want the results of reusing names in this case, even if it was allowed. (Presumably, the first would be overwritten by the second or something.) – cfr Jan 11 '16 at 03:07