# Using minted in l3doc documentation

When using minted in a .dtx file with l3doc in any way one will lose syntax highlighting as shown below (where the first and fourth segment are minted outputs and the second and third verbatim):

You see that either some tokens are gobbled from the beginning (verbatim and minted) or all code is shown, but not highlighted (verbatim and minted).

Question: How to fix that behavior?

MWE:

% \iffalse meta-comment
%<*driver>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
\input l3docstrip.tex
\preamble
\endpreamble
\postamble
\endpostamble
\generate{\file{test.sty}{\from{test.dtx}{pkg,cfg}}}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%</driver>
%
%<*driver|class>
\RequirePackage{expl3}
%</driver|class>
%
%<*driver>
\documentclass{l3doc}
\usepackage{minted}
\begin{document}
\DocInput{test.dtx}
\end{document}
%</driver>
% \fi
%\iffalse
%<*driver>
%\fi
\begin{minted}{latex}
\begin{testme}
\item{example} Mustermann
\end{testme}
\end{minted}
\begin{verbatim}
\begin{testme}
\item{example} Mustermann
\end{testme}
\end{verbatim}
%\iffalse
%</driver>
%\fi
% \begin{verbatim}
% \begin{testme}
% \item{example} Mustermann
% \end{testme}
% \end{verbatim}
% \begin{minted}{latex}
% \begin{testme}
% \item{example} Mustermann
% \end{testme}
% \end{minted}
% \endinput


This is not about using minted for the macrocode (see l3doc in combination with minted)

• This is self-answered to share my experience working around a not working minted. If anyone has a better suggestion please answer too. – TeXnician Dec 28 '17 at 8:34

The l3doc class uses fancyvrb to enhance the verbatim environment. To make all verbatim environments fancy it does

\fvset{gobble=2}
\cs_gset_eq:NN \verbatim \Verbatim
\cs_gset_eq:NN \endverbatim \endVerbatim


The problem with that is the interference with packages like minted which also rely on fancyvrb. Knowing what l3doc does it is easy to patch for case 1 as one just has to set \fvset{gobble=0} in the preamble. However that would result in examples 3 and 4 printing percent characters. So when using a fancyvrb environment within the % logic one has to explicitly set \fvset{gobble=2}. That will result in the following:

As you can see the code is printed in all cases, but minted syntax highlighting will only work when leaving the percent prefixed context.

Code:

% \iffalse meta-comment
%<*driver>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
\input l3docstrip.tex
\preamble
\endpreamble
\postamble
\endpostamble
\generate{\file{test.sty}{\from{test.dtx}{pkg,cfg}}}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%</driver>
%
%<*driver|class>
\RequirePackage{expl3}
%</driver|class>
%
%<*driver>
\documentclass{l3doc}
\usepackage{minted}
\fvset{gobble=0}
\begin{document}
\DocInput{test.dtx}
\end{document}
%</driver>
% \fi
%\iffalse
%<*driver>
%\fi
\begin{minted}{latex}
\begin{testme}
\item{example} Mustermann
\end{testme}
\end{minted}
\begin{verbatim}
\begin{testme}
\item{example} Mustermann
\end{testme}
\end{verbatim}
%\iffalse
%</driver>
%\fi
% \fvset{gobble=2}
% \begin{verbatim}
% \begin{testme}
% \item{example} Mustermann
% \end{testme}
% \end{verbatim}
% \fvset{gobble=2}
% \begin{minted}{latex}
% \begin{testme}
% \item{example} Mustermann
% \end{testme}
% \end{minted}
% \endinput