# Change macrocode environment colour when documenting package with ltxdoc

Need help in customising class ltxdoc. I would like to colour code macrocode environments so as to facilitate the visual distinction between package's macro code and it associated documentation.

I tried to create a new environment:

\newenvironment{sourcecode}%
{\color{blue}\begin{macrocode}}%
{\end{macrocode}}


That failed with a capacity size exceeded (set for the occasion to 100000).

I had a look at ltxdoc.dtx to see if I could simply import the macrocode environment. But its implementation is too cryptic for me.

Thanks for any insight here :-)

## 1 Answer

You can use the environment hooks from the package etoolbox in the first part of the .dtx.

MWE:

% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{xcolor}
\usepackage{etoolbox}
\AtBeginEnvironment{macrocode}{\color{blue}}
\begin{document}
\DocInput{dtxcolor.dtx}
\end{document}
%</driver>
% \fi
% \StopEventually{}
%
%This is a description of the code below.
%    \begin{macrocode}
\mathchardef\implies="3221 \mathchardef\impliedby="3220 \mathchardef\Implies=%
"3229 \mathchardef\Impliedby="3228 \mathchardef\implic="2221 \mathchardef
\implicby="2220 \mathchardef\Implic="2229 \mathchardef\Implicby="2228 \let
\imp\to%%
%    \end{macrocode}
% \Finale
\endinput


Result (with latex dtxcolor.dtx):

• When the macrocode environment is immediately preceded by with a \DescribeMacro{mymacro}, the mymacro in the margin gets coloured too. Adding some documentation text between \DescribeMacro{mymacro} and \begin{macrocode} resolves the colour issue. – JM. Marcastel Dec 6 '17 at 13:48
• @JM.Marcastel you might be able to patch DescribeMacro as well (with pretocmd etc.), for example put \color{black} in front and \color{blue} at the end. – Marijn Dec 6 '17 at 14:23