gpoore / minted

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

Passing newlines via `+v` argument causes error in 2.7 #356

Closed marcin-serwin closed 11 months ago

marcin-serwin commented 1 year ago

In version 2.6 the following code was working fine

\documentclass{article}

\usepackage{minted}

\begin{document}

\NewDocumentCommand{\foo}{+v}{\mintinline{latex}|#1|}
\foo{bar
baz}

\end{document}

but in version 2.7 it results in the error below

! Package fvextra Error: Missing end for environment Verbatim.

See the fvextra package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.3 baz
gpoore commented 1 year ago

Version 2.7 reads the verbatim argument for \mintinline differently than previous versions, and does not support line breaks within the verbatim argument. The \foo command is passing bar^^Mbaz to \mintinline, where the ^^M is a newline. You could define \foo with \NewDocumentCommand{\foo}{v} (v, not +v). Or you could replace the ^^M with spaces before passing it on to \mintinline.

marcin-serwin commented 1 year ago

You could define \foo with \NewDocumentCommand{\foo}{v} (v, not +v). Or you could replace the ^^M with spaces before passing it on to \mintinline.

Neither of these solutions will output the newlines though. I want the PDF output to be

bar
baz

not

bar baz
gpoore commented 1 year ago

This should do what you want. \mintinline isn't intended for code with newlines, though, so there's no guarantee that this won't stop working again in the future. A proper solution would probably involve wrapping #1 in a minted environment with something involving \scantokens, since the minted environment is intended for multi-line code.

\NewDocumentCommand{\foo}{+v}{%
  \begingroup
  \let\VerbEnv\Verbatim
  \let\endVerbEnv\endVerbatim
  \mintinline{latex}{#1}%
  \endgroup}
marcin-serwin commented 1 year ago

Thank you for the help. The proposed solution is still not ideal, since now \foo ends current paragraph and starts another so it's no longer "inline", but I can work around it.

gpoore commented 1 year ago

If you're trying to use it inline within paragraphs, you probably want something like this:

\makeatletter
\NewDocumentCommand{\foo}{+v}{%
  \begingroup
  \let\VerbEnv\Verbatim
  \let\endVerbEnv\endVerbatim
  \mintinline{latex}{#1}%
  \endgroup\@doendpe}
\makeatother