# How to embed a minted environment inside a tabular environment?

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?

• Please make you code a minimal working example (MWE). It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – jub0bs May 7 '13 at 15:46
• @Jubobs: I've edit the question so that the example is now a MWE. I'm sorry for not having done so to start with. – Jon Smark May 7 '13 at 18:50
• No problem, but it's a good habit to take on this site. You want to make it easy for people to help you, and posting MWE is an important step in that direction. That way, people can run your code and see what you mean with minimal effort on their part, and you're likely to get an answer much more quickly. – jub0bs May 7 '13 at 19:19

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}


• This answer of a followup question shows, how to remove the additional vertical space around environment minted` in the tabular cell. – Heiko Oberdiek Jul 6 '14 at 1:06