# Minted inside tabular

I know this question has been asked before, but it didn't quite solve my issue. (How to embed a minted environment inside a tabular environment?)

The described workaround (bringing \end{minted} to a single line) does work, however only if the code comes second. When I try to put the minted-section into the first column I get this error:

! LaTeX Error: Something's wrong--perhaps a missing \item.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \begin{Verbatim}[commandchars=\\\{\}, ]


I used this code:

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


What can I do? Thank you

• The first column is an r type one, so paragraphs are not allowed in it. Jul 3 '14 at 17:39
• Welcome to the site! Stating @egreg's solution explicitly, try \begin{tabular}{p{0.5\textwidth}r}. Let us know if this resolves the issue :) Welcome! Jul 3 '14 at 17:52

If you look at the linked question, you'll see that the minted environment appears in the second column, which is declared as p type.

You can't have an enumerated list or a center environment or anything that requires breaking lines in a column declared as r.

So just declare the first column as p. For instance

\begin{tabular}{p{0.5\textwidth} r}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
& testing
\end{tabular}


will work.

Of course the alignment will be wrong. You can remedy by adding a minipage:

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{tabular}{p{0.5\textwidth} r}
\begin{minipage}[t]{0.5\textwidth}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{minipage}
& testing
\end{tabular}
\end{document}


• Now if one has to use a minipage, then c would be valid, wouldn't it? Also, in the first option, adding a background (\begin{minted}[bgcolor=black]{c}) gives weird results, so it seems minipage is required in any case. Aug 10 '20 at 23:50
• @Michaël How to add a background is a new question, I guess. Aug 11 '20 at 7:17
• Agreed, although I do know how to add a background, and I'd hope that a functioning solution to the current question wouldn't break this basic feature. In any case, this little exchange may be helpful to anyone searching for tabular minted backgrounded environments! Aug 12 '20 at 3:14