10

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 – percusse Jan 14 '16 at 05: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. – Joey Eremondi Jan 14 '16 at 06:24
  • See this one for example http://tex.stackexchange.com/questions/60155/minted-doesnt-escape-latex-code-inside-python-docstring TikZ works in every mode – percusse Jan 14 '16 at 07:07
  • 1
    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 03:08

2 Answers2

14

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

enter image description here

\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}
David Carlisle
  • 757,742
  • I tried this and it didn't work for me. – Adam Miller Nov 13 '17 at 05:41
  • 2
    @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. – David Carlisle Nov 13 '17 at 10:03
  • @DavidCarlisle "run it twice" was the important bit of information in my case. Now it works for me. – BR123 Oct 01 '20 at 11:58
  • @DavidCarlisle Superb suggestion... – MadyYuvi Aug 31 '21 at 11:01
  • I've been using the above to annotate the source code. One little problem I encountered along the way was if I accidentally have tikzmark with the same name, tikzmark{a} in one listing and the same thing in another, the drawings usually get weird. This happens even if the listing are on different pages. – Pyi Soe Mar 14 '24 at 14:06
  • @PyiSoe yes that's user error, \tikzmark{a} is like \label{a} and writs data to a command with name based on a to the aux file, if you do that twice the first one gets over-written you need to ensure the names used are unique – David Carlisle Mar 14 '24 at 14:14
  • @DavidCarlisle Thanks for your clarification. I think I can still manage name clash not to happen by using systematic naming convention. – Pyi Soe Mar 14 '24 at 14:39
2

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}