3

I would like to add some tikz in the listing environment. When the code is not long (less than a page), there is not any problem with the position of tikz.

enter image description here

But when the code is long, the tikz shows up on the wrong page. A minimal example of long code is as follows.

\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{positioning}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\begin{document}
a\\
a\\
a\\
a\\
a\\
a\\
a\\  
a\\
a\\
a\\
a\\
a\\
a\\
a\\  
a\\
a\\
a\\
a\\
a\\
a\\
a\\  
a\\
a\\
a\\
a\\
a\\
a\\
a\\ 
\begin{lstlisting}[escapechar= !]
a
a
a
a
a
a
THIS IS THE TEXT. !\tikzmark{a}!
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
\end{lstlisting}
\tikz[overlay,remember picture]{\node[fill=green, right=of a] (text) {\scriptsize \Huge HERE};}

\end{document}

How can i fix it.

5

Most likely I am not interpreting your question correctly, but I'm wondering what is wrong with putting the annotation right after \tikzmark.

\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{positioning}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\begin{document}
a\\
a\\
a\\
a\\
a\\
a\\
a\\  
a\\
a\\
a\\
a\\
a\\
a\\
a\\  
a\\
a\\
a\\
a\\
a\\
a\\
a\\  
a\\
a\\
a\\
a\\
a\\
a\\
a\\ 
\begin{lstlisting}[escapechar= !]
a
a
a
a
a
a
THIS IS THE TEXT. !\tikzmark{a}! !\tikz[overlay,remember picture]{\node[fill=green, right=of a] (text) {\Huge HERE};}!
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
\end{lstlisting}
\end{document}

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.