[Tex/LaTex] How to embed a minted environment inside a tabular environment

booktabsmintedtables

I'm using packages minted and booktabs, and would like to put a minted environment within a tabular one. However, the following does not work:

\documentclass[11pt]{article}
\usepackage{minted}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{rp{0.5\textwidth}}
\toprule
A & B\\
\midrule
testing &
\begin{minted}{text}
testing
\end{minted}\\
\bottomrule
\end{tabular}
\end{document}

Any ideas how this can be achieved?

Best Answer

Make sure \end{minted} is on a line of its own. In the following example, the line containing \end{minted} contains additional charcaters:

\documentclass{article}
\usepackage{minted}
\usepackage{booktabs}

\begin{document}

\noindent\begin{tabular}{rp{0.5\textwidth}}
\toprule
A & B\\
\midrule
testing & 
\begin{minted}{c}
int main() {
  printf("hello, world");
  return 0;
}
\end{minted} \\
\bottomrule
\end{tabular}

\end{document}

and this will trigger an error:

! FancyVerb Error:
  Extraneous input ` \\\end{}' between \end{minted} and line end
.
\FV@Error ... {FancyVerb Error:
\space \space #1
}

l.17 \end{minted} \

Simply moving the other characters to a new line solves the problem:

\documentclass{article}
\usepackage{minted}
\usepackage{booktabs}

\begin{document}

\noindent\begin{tabular}{rp{0.5\textwidth}}
\toprule
A & B\\
\midrule
testing & 
\begin{minted}{c}
int main() {
  printf("hello, world");
  return 0;
}
\end{minted} 
\\
\bottomrule
\end{tabular}

\end{document}

enter image description here