The LaTeX Highlighter


The LaTeX Highlighter


What it does

The LaTeX Highlighter (that is, the Rexx Highlighter operating in LaTeX mode) writes a Rexx program as a LaTeX environment, rxhlisting, that looks like the HTML: the colours, the weights, the slants and the underlines come from the same CSS (see predefined styles), and the box has the background of the style.

  • The box can break across pages, and it works anywhere (in lists, in two columns, in notes): it is plain TeX, not verbatim.
  • Every TeX special character is escaped, and the quotes are straight.
  • Blanks are one character wide, and a tab is eight blanks.
  • A line too long for the box goes on, indented, on the next.
  • A listing too wide for the box is set smaller (down to 60%) so that it fits.
  • Line numbers (-n, --startFrom) go in a gutter, with a rule.
  • Style patches work as everywhere else.

It works with LuaLaTeX, XeLaTeX and pdfLaTeX. For Unicode beyond the Latin scripts, use LuaLaTeX or XeLaTeX.

Selecting LaTeX mode

The -l and --latex options of the highlight utility select the LaTeX Highlighter. Add --prolog once, to get the definitions that go in the preamble of the document:

highlight --latex --prolog --style light program.rex > program.tex
highlight --latex --style dark --numberlines other.rex > other.tex

The prolog

The prolog loads xcolor and tcolorbox, and defines the macros and the environment. It goes in the preamble, once (loading it twice does no harm). Take it from the output of highlight --latex --prolog: it is everything before \begin{rxhlisting}.

Each listing describes its own look, and needs nothing but the prolog: listings highlighted separately, with different styles and patches, can go in the same document.

Fonts

The code is set in the document's \ttfamily, which should have bold and italic. Latin Modern Mono, the default, has no bold. With LuaLaTeX or XeLaTeX:

\usepackage{fontspec}
\setmonofont{DejaVu Sans Mono}[Scale=MatchLowercase]

With pdfLaTeX:

\usepackage[T1]{fontenc}
\usepackage[scaled=0.9]{DejaVuSansMono}

Customizing

\renewcommand\rxhfont{\footnotesize}       % The size (default: \small)
\tcbset{rxh/.style={arc=3pt}}              % More tcolorbox options for the box
\definecolor{rxhgutter}{HTML}{808080}      % The line numbers and their rule

The output

  • \begin{rxhlisting}[width]{fg}{bg} opens the box. fg and bg are the colours of the style (RRGGBB), and width is the length of the longest line, in characters: if it does not fit, the listing is set smaller.
  • \rxhL{...} is a line, and \rxhLN{digits}{number}{...} a numbered line.
  • \rxhT{flags/fg/bg}{text} is a token. flags are B, I and U (bold, italic, underline); fg and bg are RRGGBB, or nothing when they are those of the box.
  • \rxhs{} is a blank. Blanks, too, have the look the CSS gives them (in the dark style, for example, the blank operator is not coloured like the other blanks).
  • \rxhI{fg}{bg}{tokens} is code inline, in the middle of a paragraph, and \rxhsp{} an unbreakable blank. The highlight utility does not write them; md2latex does, for the Rexx mentioned in the prose.

For example, highlight --latex --style dark --numberlines gives:

\begin{rxhlisting}[32]{AAAAAA}{000000}
\rxhLN{2}{1}{\rxhT{I/00CC00/}{/*\rxhs{}Hello,\rxhs{}LaTeX\rxhs{}*/}}
\rxhLN{2}{2}{\rxhT{B/DDDDDD/}{Do}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{/FF66FF/}{i}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{B/DDDDDD/}{=}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{/33CCCC/}{1}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{B/DDDDDD/}{To}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{/33CCCC/}{3}}
\rxhLN{2}{3}{\rxhT{/CCCC00/}{\rxhs{}\rxhs{}}\rxhT{B/DDDDDD/}{Say}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{/FFFF00/}{"}\rxhT{/64B02A/}{Line}\rxhT{/FFFF00/}{"}\rxhT{/44FF44/}{\rxhs{}}\rxhT{/FF66FF/}{i}\rxhT{/CCCC00/}{\rxhs{}}\rxhT{I/00CC00/}{-{}-{}\rxhs{}100\%\rxhs{}Rexx}}
\rxhLN{2}{4}{\rxhT{B/DDDDDD/}{End}}
\end{rxhlisting}

A complete document

\documentclass[a4paper]{article}
\usepackage{fontspec}
\setmonofont{DejaVu Sans Mono}[Scale=MatchLowercase]
\input{prolog}      % The prolog, saved as prolog.tex
\begin{document}
Some text.
\input{program}     % The output of highlight --latex, saved as program.tex
\end{document}

Articles and books

To turn a whole RexxPub article or book into LaTeX, with its Rexx code highlighted, use md2latex. It writes its listings and its mentions with these same macros, and puts this same prolog in the preamble.

Programs that write LaTeX from the Highlighter's HTML, as md2latex does, can use three class methods of .LaTeX.Highlighter: prologText (the prolog, as a string), box(style) (the colours of the box, "FG BG") and token(style, classes, text[, where]) (a token, from its text and its HTML classes; where is block, the default, or inline).