2

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

jagjordi
  • 794

1 Answers1

2

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

  • 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. –  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. –  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
  • @jagjordi I rewrote the answer to become much more flexible, and to address this feature. –  Nov 16 '19 at 21:48
  • Thank you again. Your option now works great, but I don't understand why now it appears on top of the minted code, what is the "parameter" that achieves this result? I tried a similar code as your initial answer but my result was that the rectangle was not drawn on top, it appear in the same "layer" merged with minted text – jagjordi Nov 16 '19 at 22:20
  • @jagjordi I always was on top, I believe. It may now just look more prominent because of the halo. –  Nov 16 '19 at 22:21
  • you can see my result in this picture https://i.imgur.com/I6Y9E7V.png – jagjordi Nov 16 '19 at 22:22
  • @jagjordi Sorry, where does that result come from? (If your question is how to achieve a transparent node, use mark node/.style={fill=red,fill opacity=0.2,draw},.) –  Nov 16 '19 at 22:26
  • for some reasons I cannot share the exact code for the picture, I have instead updated the MWE to showcase a similar situation – jagjordi Nov 16 '19 at 22:35
  • @jagjordi Now I see what you mean. I didn't know that minted would do that. The crucial difference between both situations is that in the first case node is above lines that were already typeset, and now the node wants to be above nodes that are to be typeset. I did not expect minted to really do things line by line, but it apparently does.However, there is a very simple fix, which I added to the answer. You can combine the upper part of the answer with the fix, too. –  Nov 16 '19 at 22:46
  • Thank you again. Now it works perfectly, although there is something funny, if you add a bgcolor to the minted environment it stops working!! It is not a major issue to me but it would be good knowing what is going on – jagjordi Nov 17 '19 at 12:07