lvjr / codehigh

Highlight codes and demos with l3regex and lpeg

Home Page:https://ctan.org/pkg/codehigh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Highlight `macrocode` environment

pablgonz opened this issue · comments

I think it would be a great addition to your package if the macrocode environment could be colored. At the moment only the colordoc package (and with many patches) can do it.

% \iffalse meta-comment
%<*driver>
\documentclass[full]{l3doc}
\usepackage{unicode-math}
% Unicode-math changes the position of the glyphs, so the position 26A is now
% a letter `j'. The doc package ensures that mathcode and the | actually prints
% a j inside docstrip guards. This changes the mathcode of | to 07C:
\makeatletter
\def\mod@math@codes{\mathcode`\|="207C \mathcode`\&="2026
                    \mathcode`\-="702D \mathcode`\+="702B
                    \mathcode`\:="703A \mathcode`\=="703D }
\makeatother

\usepackage[contents]{colordoc}
% \show\docCodelineNo
\def\textnew#1{\color{color-def}#1\/}
\def\AltMacroFont{\fontencoding\encodingdefault
                  \fontfamily\ttdefault
                  \fontseries\mddefault
                  \fontshape\updefault
                  \small
                  }%

% Patching colordoc.sty to work with l3doc.cls
\ExplSyntaxOn
\cs_new_eq:cN { liii@xmacro@code } \__codedoc_xmacro_code:n
\ExplSyntaxOff
\begingroup
\makeatletter
\catcode`\[\@ne\catcode`\]\tw@
\@makebracesactive
\gdef{[\@openingbrace[\char'173]]
\gdef}[\@closingbrace[\char'175]]
\catcode`\|\z@\catcode`\%12
\catcode`\ \active\catcode`\\\active
|gdef|xmacro@code#1%    \end{macrocode}[|liii@xmacro@code[#1]|end[macrocode]]
|catcode`| 12|gdef|sxmacro@code#1%    \end{macrocode*}[|liii@xmacro@code[#1]|end[macrocode*]]
|endgroup

% Patching colordoc.sty to work with doc v3 (TeX Live 2023 & lualatex)
\usepackage{etoolbox}
\makeatletter
\@ifpackagelater{doc}{2022-06-01}%
  {\patchcmd\macro@finish
    {\ifnot@excluded % \if
      \edef\@tempa{\noexpand\SpecialIndex{\bslash\macro@namepart}}%
      \@tempa \fi}%
    {\maybe@index@macro \macro@namepart}
    {}{\FAILED}%
   \let\default@color\current@color
  }%
  {}
\makeatother
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \begin{documentation}
% \title{The \texttt{testpkg} package}
% \author{Me}
% \date{\today}
% \maketitle
% \tableofcontents
% \section{Documentation}
% \begin{function}{\testpkg_function:}
%   \begin{syntax}
        \verb|\testpkg_function:| The syntax is complicated.
%   \end{syntax}
% \end{function}
% \begin{function}{testpkgenv}
%   \begin{syntax}
        \verb|\begin{testpkgenv}| ... \verb|\end{testpkgenv}|
%   \end{syntax}
% \end{function}
% \end{documentation}
%
% \begin{implementation}
% \section{Implementation}
%    \begin{macrocode}
%<*package>
%<@@=testpkg>
%    \end{macrocode}
% \begin{variable}{\l_@@_tmp_tl}
%   A thorough description of this variable.
%    \begin{macrocode}
\tl_new:N \l_@@_tmp_tl
%    \end{macrocode}
% \end{variable}
% \begin{macro}{\testpkg_function:}
%   This function does things.
%    \begin{macrocode}
\NewDocumentCommand { \testpkg_function: } { }
  { \@@_do_things: }
%    \end{macrocode}
% \end{macro}
% \begin{environment}{testpkgenv}
%   This environment also does things.
%    \begin{macrocode}
\NewDocumentEnvironment { testpkgenv } { }
  { \@@_do_things: }
  { \@@_stop_doing_things: }
%    \end{macrocode}
% \end{environment}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \end{implementation}
% \PrintIndex
% \Finale
\endinput

imagen

And of course, just color for the sake of coloring (I use it in the scontents documentation).
Saludos