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?

4
  • I guess you can only try and find out
    – percusse
    Jan 14, 2016 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.
    – jmite
    Jan 14, 2016 at 6:24
  • See this one for example tex.stackexchange.com/questions/60155/… TikZ works in every mode
    – percusse
    Jan 14, 2016 at 7: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, 2016 at 3:08

2 Answers 2

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}
4
  • I tried this and it didn't work for me. Nov 13, 2017 at 5: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. Nov 13, 2017 at 10:03
  • @DavidCarlisle "run it twice" was the important bit of information in my case. Now it works for me.
    – BR123
    Oct 1, 2020 at 11:58
  • @DavidCarlisle Superb suggestion...
    – MadyYuvi
    Aug 31, 2021 at 11:01
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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .