[Tex/LaTex] Minted inside tabular

codemintedtables

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

Best Answer

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

Related Question