5

I have some C pointer code that I would like to "single-step" to show what happens with e.g. dangling pointers. I use minted for syntax highlighting, and that seems to be causing the problem.

The code below compiles (with -shell-escape), but the code that should be escaped isn't. The reason seem to be a bug in pygments (see https://github.com/gpoore/minted/issues/70). I have also tried to solution of marvin2k that re-defined !, but failed to get that code to work.I have also tried various combinations of \begin{minted}[highlightlines=\only<1>{3}\only<2>{5}]{c} to no avail.

Does anyone have a solution? Edit: My platform is ubuntu 14.04, pygmentize 1.6, minted 2.4.1.

\documentclass{beamer}
\usepackage{minted,tikz}
\begin{document}
\begin{frame}[fragile]{Pointers}
\begin{minted}[linenos,escapeinside=||]{c}
  int main(void) {
    char *p;
    p=(char *)malloc(5); |\only<1>{$\Leftarrow$}|
    /* do stuff */
    p=(char *)malloc(7); |\only<2>{$\Leftarrow$}|
    free(p);
    return 0;
}
\end{minted}
\begin{tikzpicture}
  \node<1->[rectangle,draw] (p) {p};
  \node<1->[rectangle,draw,right of=p] (q1) {q1};
  \draw<1>[->] (p) -- (q1);
  \uncover<2->{\node[rectangle,draw,below right of=p] (q2) {q2};};
  \draw<2->[->] (p) -- (q2);
\end{tikzpicture}
\end{frame}
\end{document}

First slide showing non-escape LaTeX code

  • Completely unrelated but you shouldn’t cast the return value of malloc in C, it’s unnecessary and error-prone (since you could accidentally cast to the wrong pointer type and invoke inadvertent undefined behaviour): stackoverflow.com/q/605845/1968 – Konrad Rudolph Jan 23 '17 at 10:42
  • @KonradRudolph, unrelated...and you are right. It was a long time since I last did C and I haven't kept track of 'best practices'. I tried to modify my question but I don't know how to modify the image. – Niclas Börlin Jan 24 '17 at 13:03
6

As < and > are problematic with minted, you may define the \myonly command:

\newcommand\myonly[2]{\only<#1>{#2}}

MWE:

\documentclass{beamer}
\usepackage{minted,tikz}
\newcommand\myonly[2]{\only<#1>{#2}}
\begin{document}
\begin{frame}[fragile]{Pointers}
\begin{minted}[linenos,escapeinside=||]{c}
  int main(void) {
    char *p;
    p=(char *)malloc(5); |\myonly{1}{$\Leftarrow$}|
    /* do stuff */
    p=(char *)malloc(7); |\myonly{2}{$\Leftarrow$}|
    free(p);
    return 0;
}
\end{minted}
\begin{tikzpicture}
  \node<1->[rectangle,draw] (p) {p};
  \node<1->[rectangle,draw,right of=p] (q1) {q1};
  \draw<1>[->] (p) -- (q1);
  \uncover<2->{\node[rectangle,draw,below right of=p] (q2) {q2};};
  \draw<2->[->] (p) -- (q2);
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

  • That's odd...when I compile your solution (ubuntu 14.04, pygmentize v1.6, minted v2.4.1), my problem remains. What setup did you use? – Niclas Börlin Jan 22 '17 at 15:49
  • 1
    @NiclasBörlin I use TeXLive 2016, pygmentize 2.1.3 and minted 2.4.1. – Paul Gaborit Jan 22 '17 at 17:24
  • It looks like I need to upgrade pygmentize then... Do you run Linux or something else? – Niclas Börlin Jan 22 '17 at 17:29
  • 1
    @NiclasBörlin I use Ubuntu 16.10. – Paul Gaborit Jan 22 '17 at 17:29
4

The best solution I have so far is based on @VZ.'s overprint solution (https://tex.stackexchange.com/a/51618/35602). However, it would be nice with a solution that did not require the C code to be repeated...

\documentclass{beamer}
\usepackage{minted}
\usepackage{tikz}
\begin{document}
\begin{frame}[fragile]{Foo}
\begin{overprint}
\onslide<1>
\begin{minted}[linenos,highlightlines={3}]{c}
  int main(void) {
    char *p;
    p=(char *)malloc(5);
    /* do stuff */
    p=(char *)malloc(7);
    free(p);
    return 0;
  }
\end{minted}
\onslide<2>
\begin{minted}[linenos,highlightlines={5}]{c}
  int main(void) {
    char *p;
    p=(char *)malloc(5);
    /* do stuff */
    p=(char *)malloc(7);
    free(p);
    return 0;
  }
\end{minted}
\end{overprint}
\begin{tikzpicture}
  \node<1->[rectangle,draw] (p) {p};
  \node<1->[rectangle,draw,right of=p] (q1) {q1};
  \draw<1>[->] (p) -- (q1);
  \uncover<2->{\node[rectangle,draw,below right of=p] (q2) {q2};};
  \draw<2->[->] (p) -- (q2);
\end{tikzpicture}
\end{frame}
\end{document}

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.