1

This question describes how to use tikzmarks inside minted code. However if the code spans longer than one page the result is not correct.

Is there a way to fix this?

MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{minted}

\begin{document}
\begin{minted}[escapeinside=??]{py}
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long?\tikzmark{mark}? function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  \end{minted}

  \begin{tikzpicture}[remember picture]
    \draw[overlay, latex-, thick] (pic cs:mark) --++ (1,1);
  \end{tikzpicture}
\end{document}

New MWE

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{minted}

\newcommand{\mymarkright}[2]{\tikzmark{#1}%
  \begin{tikzpicture}[overlay, remember picture]
    \draw [overlay, latex-, thick] (pic cs:#1) --++ (0.5,-0.5) node[anchor=west, align=center, rectangle, fill=red!30, draw] {#2};
\end{tikzpicture}}

\begin{document}
\begin{minted}[escapeinside=??]{py}
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
  # this is a a long?\mymarkright{a}{Here goes\\some text}? function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  \end{minted}

\end{document}

enter image description here

1

You need to add the overlay picture with the mark. To address all the comments below, I have rewritten the answer to contain a couple of pgf keys that allow you to customize the appearance of the arrow. I hope to have given the keys self-explanatory names.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newcounter{mymark}
\usepackage{minted}
\newcommand{\MarkMe}[2][]{\stepcounter{mymark}%
\tikzmark{mark-\number\value{mymark}}%
\begin{tikzpicture}[overlay,remember picture]
    \def\pv##1{\pgfkeysvalueof{/tikz/my mark/##1}}
    \draw[/tikz/my mark/arrow,line width=\pv{line width},red,shorten >=\pv{halo},
    shorten <=\pv{halo},
    preaction={draw,line width=\pv{line width}+2*\pv{halo},white,/tikz/my mark/arrow}] 
    ([yshift=2pt]pic cs:mark-\number\value{mymark}) 
    --++ (\pv{x},\pv{y}) node[above,/tikz/my mark/mark node]{#2};
  \end{tikzpicture}}
\tikzset{my mark/.cd,
mark node/.style={fill=red!30,draw},
line width/.initial=1pt,
halo/.initial=0.5pt,
x/.initial=1cm,
y/.initial=1cm,
arrow/.style={latex-}}
\begin{document}
\begin{minted}[escapeinside=??]{py}
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long?\MarkMe{bla}? function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  \end{minted}
\end{document}

enter image description here

As for the next iteration of your question: interesting! I did not know minted would do that. Anyway, here is a fix: use eso-pic or atbegshi to draw the stuff last. This can be combined with the above answer by wrapping the tikzpicture environment into \AddToShipoutPictureFG*{...}, and loading eso-pic, of course.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{minted}
\usepackage{eso-pic}

\newcommand{\mymarkright}[2]{\tikzmark{#1}%
\AddToShipoutPictureFG*{%
  \begin{tikzpicture}[overlay, remember picture]
    \draw [ latex-, thick] (pic cs:#1) --++ (0.5,-0.5) 
    node[anchor=west, align=center, rectangle, fill=red!30, draw] {#2};
\end{tikzpicture}}}

\begin{document}
\begin{minted}[escapeinside=??]{py}
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
  # this is a a long?\mymarkright{a}{Here goes\\some text}? function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  def function1(args):
    # this is a a long function
  \end{minted}

\end{document}

enter image description here

13
  • Thank you very much, this is what I wanted. Something else I would like to achieve is having the overlay to have higher opacity so it is possible to read if it lays on top of other text. Is it easy to achieve?
    – jagjordi
    Nov 16 '19 at 15:49
  • @jagjordi Oh yes, just add e.g opacity=0.3 to the \draw command. I changed the answer accordingly.
    – user194703
    Nov 16 '19 at 15:59
  • sorry, I was not clear, I ment the other way arround, makeint the drawings apear on top of the minted code
    – jagjordi
    Nov 16 '19 at 16:07
  • @jagjordi OK, I see. I added a proposal for that.
    – user194703
    Nov 16 '19 at 16:50
  • Thanks again, now I would like to have a rectangluar filled node to be on top of that? how can that be achieved?sorry for the multiple requests I thought that the answer to my MWE would be aplicable to my need
    – jagjordi
    Nov 16 '19 at 21:35

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.