gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.74k stars 126 forks source link

% breaks compilation #323

Closed Try2Code closed 2 years ago

Try2Code commented 2 years ago

hi!

I encountered a compilation error that seems to get triggered by simple tex comment starting with '%'

\documentclass{beamer}
\usepackage{minted}
\begin{document}
\begin{frame}[fragile]
\frametitle{Python Code listing in Beamer}
\begin{minted}{python}
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
\end{minted}
\end{frame}%                                                                                                                                           
\end{document}

Putting '%' at the end of an existing line (l.6,8,9) leads to an error. putting it to a separate line does not lead to an error. Is this a known limitation? is this a parsing issue? I thought comments like this are normal latex syntax - am I right?

gpoore commented 2 years ago

This appears to be caused by beamer's fragile mode, so I don't believe there anything that minted can do about it. Typically we would expected comments % to work anywhere. However, some environments are specially defined so that nothing is allowed to follow them. I'm not sure if this is a bug, or if this is the way frame combined with fragile is designed (in which case it would be nice to have a better error message, if that's possible).

Try2Code commented 2 years ago

the point is that frames need to be fragile in order to use minted. So the conclusion is to avoid comments like this in frames with source code, right?

gpoore commented 2 years ago

Yes, you can't have comments like this with fragile frames, at least with the way that beamer currently works.

muzimuzhi commented 2 years ago

fancyvrb, which is used by minted to provide verbatim envs, also thinks a % right after \end{Verbatim} is invalid:

\documentclass{article}
\usepackage{fancyvrb}

\begin{document}
\begin{Verbatim}
content
\end{Verbatim}%
\end{document}

will get error

! FancyVerb Error:
  Extraneous input `%\end{}' between \end{Verbatim} and line end
Try2Code commented 2 years ago

Alright - thx a lot for the fast support @gpoore @muzimuzhi