8

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

lockstep
  • 250,273
  • 7
    The first column is an r type one, so paragraphs are not allowed in it. – egreg Jul 03 '14 at 17:39
  • 4
    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! – cmhughes Jul 03 '14 at 17:52

1 Answers1

11

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.

enter image description here

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}

enter image description here

egreg
  • 1,121,712
  • 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. – Michaël Aug 10 '20 at 23:50
  • @Michaël How to add a background is a new question, I guess. – egreg Aug 11 '20 at 07: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! – Michaël Aug 12 '20 at 03:14