# Is it possible to use tikzmark inside minted?

As I've pointed out in a related question, I'd like to have some arrows pointing to parts of a source-code listing.

I know this is possible with the listings package, but is it possible with minted? That is, is there a way that I can insert a mark at a specific point inside the code minted is displaying?

• I guess you can only try and find out Jan 14 '16 at 5:56
• @percusse Well, it's more complicated than that, since minted is based on verbatim, you can't just do commands willy nilly, since they will print as text, rather than being interpreted as Latex commands. Jan 14 '16 at 6:24
• See this one for example tex.stackexchange.com/questions/60155/… TikZ works in every mode Jan 14 '16 at 7:07
• Doesn't that thread suggest the opposite and that it doesn't work? (Or that nobody there could figure it out, at least.)
– cfr
Jan 16 '16 at 3:08

Yes, it appears to work, you just need to specify an escape character (? here) to minted so you can insert the \tikzmark.

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

\begin{document}

\begin{minted}[escapeinside=??]{py}
def f(x):
y = x?\tikzmark{here}?2
return y
\end{minted}

See this bit:
\begin{tikzpicture}[remember picture]
\draw[overlay, ->, line width=5pt, red] (0,0) -- (pic cs:here);
\end{tikzpicture}
\end{document}

• I tried this and it didn't work for me. Nov 13 '17 at 5:41
• @AdamMiller "didn't work" isn't very informative:-) The above code makes the image shown, you need --shell-escape (for minted) and to run it twice (for tikzmark) If you got an error you could always ask a new question and show the error you got. Nov 13 '17 at 10:03
• @DavidCarlisle "run it twice" was the important bit of information in my case. Now it works for me. Oct 1 '20 at 11:58
• @DavidCarlisle Superb suggestion... Aug 31 at 11:01

Just for the fun of it, and in case somebody else stumbles on this: This idea can be combined with the standalone package as well, with a bit of tweaking:

\documentclass[multi=apage,varwidth=8cm]{standalone}
\usepackage{minted}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newenvironment{apage}[0]{}{}

\begin{document}

\begin{apage}

\begin{tabular}{p{0.3\textwidth}p{0.3\textwidth}}
\begin{minted}[escapeinside=??]{py}
def f(x):
y = x?\tikzmark{here1}?2
return y
\end{minted}
&
\begin{minted}[escapeinside=??]{py}
def f(x):
y = x?\tikzmark{here2}?2
return y
\end{minted}
\end{tabular}

\begin{tikzpicture}[remember picture]
\draw[overlay, ->, line width=5pt, red]
(pic cs:here1) -- (pic cs:here2);
\end{tikzpicture}

\end{apage}

\end{document}