| 1 |
% -*- coding: utf-8 -*- |
| 2 |
\documentclass[oneside]{book} |
| 3 |
\usepackage[a4paper,margin=2cm]{geometry} |
| 4 |
|
| 5 |
\newcommand*{\myversion}{2025C} |
| 6 |
\newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)} |
| 7 |
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi} |
| 8 |
|
| 9 |
\setlength{\parindent}{0pt} |
| 10 |
\setlength{\parskip}{4pt plus 1pt minus 1pt} |
| 11 |
|
| 12 |
\usepackage{codehigh} |
| 13 |
\usepackage{hyperref} |
| 14 |
\hypersetup{ |
| 15 |
colorlinks=true, |
| 16 |
urlcolor=blue3, |
| 17 |
linkcolor=green3, |
| 18 |
} |
| 19 |
\usepackage{tabularx,booktabs} |
| 20 |
|
| 21 |
\begin{document} |
| 22 |
|
| 23 |
\title{\textsf{\color{green3}Codehigh: Highlight Codes and Demos with l3RegEx and LPeg}} |
| 24 |
\author{Jianrui Lyu ([email protected])\\ \url{https://github.com/lvjr/codehigh}} |
| 25 |
\date{\mydate} |
| 26 |
\maketitle |
| 27 |
|
| 28 |
\tableofcontents |
| 29 |
|
| 30 |
\chapter{Package Interfaces} |
| 31 |
|
| 32 |
\section{Introduction} |
| 33 |
|
| 34 |
\verb!Codehigh! package uses \verb!l3regex!\footnote{\url{https://www.ctan.org/pkg/l3regex}} |
| 35 |
package in \LaTeX3 Programming Layer to parse and highlight source codes and demos. |
| 36 |
It is more powerful than \verb!listings! package, and more easy to use than \verb!minted! package. |
| 37 |
But it is slower than both of them. |
| 38 |
Therefore in LuaTeX the package provides another way to highlight code: using \verb!LPeg!% |
| 39 |
\footnote{\url{http://www.inf.puc-rio.br/~roberto/lpeg/}}. |
| 40 |
\verb!LPeg! is much more powerful and faster than \verb!l3regex!. |
| 41 |
|
| 42 |
%At present, this package is in \underline{\color{red3}\textbf{experimental}} status. |
| 43 |
%Don’t use it in important documents, unless you have time |
| 44 |
%to update them for the newer versions of \verb!codehigh! package in the future. |
| 45 |
|
| 46 |
\section{Highlighting Code} |
| 47 |
|
| 48 |
There are several predefined languages: \verb!latex!, \verb!latex/latex2!, \verb!latex/latex3!, |
| 49 |
\verb!latex/math! and \verb!latex/table!. |
| 50 |
The following example is typeset by \verb!codehigh! environment with default option \verb!language=latex!. |
| 51 |
\begin{codehigh} |
| 52 |
% -*- coding: utf-8 -*- |
| 53 |
\documentclass{article} |
| 54 |
\usepackage[a4paper,margin=2cm]{geometry} |
| 55 |
\usepackage{codehigh} |
| 56 |
\usepackage{hyperref} |
| 57 |
\newcommand*{\myversion}{2021C} |
| 58 |
\newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)} |
| 59 |
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi} |
| 60 |
\setlength{\abc}{1} |
| 61 |
\begin{document} |
| 62 |
% some comment |
| 63 |
\section{Section Name} |
| 64 |
\subsection*{Suction Name} |
| 65 |
Math $a+b$. |
| 66 |
\end{document} |
| 67 |
\end{codehigh} |
| 68 |
|
| 69 |
The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/latex2!. |
| 70 |
\begin{codehigh}[language=latex/latex2] |
| 71 |
\def\abcd#1#2{ |
| 72 |
% some comment |
| 73 |
\unskip |
| 74 |
\setlength{\parindent}{0pt}% |
| 75 |
\setlength{\parskip}{0pt}% |
| 76 |
\setcounter{choice}{0}% |
| 77 |
\let\item=\my@item@temp |
| 78 |
\settowidth{\my@item@len}{\vbox{\halign{##1\hfil\cr\BODY\crcr}}}% |
| 79 |
\setcounter{choice}{0}% |
| 80 |
} |
| 81 |
\end{codehigh} |
| 82 |
This language is for highlighting LaTeX2 classes and packages. |
| 83 |
It highlights private commands and public commands with different colors. |
| 84 |
|
| 85 |
The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/latex3!. |
| 86 |
\begin{codehigh}[language=latex/latex3] |
| 87 |
\cs_new_protected:Npn \__codehigh_typeset_demo: |
| 88 |
{ |
| 89 |
\__codehigh_build_code: |
| 90 |
\__codehigh_build_demo: |
| 91 |
\dim_set:Nn \l_tmpa_dim { \box_wd:N \g__codehigh_code_box } |
| 92 |
\dim_set:Nn \l_tmpb_dim { \box_wd:N \g__codehigh_demo_box } |
| 93 |
\par\addvspace{0.5em}\noindent |
| 94 |
% more code |
| 95 |
} |
| 96 |
\end{codehigh} |
| 97 |
This language is for highlighting LaTeX3 classes and packages. |
| 98 |
It highlights private commands/variables and public commands/variables with different colors. |
| 99 |
|
| 100 |
The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/math!. |
| 101 |
\begin{codehigh}[language=latex/math] |
| 102 |
\begin{align} |
| 103 |
\pi\left[\frac13z^3\right]\sin(2x+1)_0^4 = \frac{64}{3}\pi |
| 104 |
\end{align} |
| 105 |
\end{codehigh} |
| 106 |
|
| 107 |
The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/table!. |
| 108 |
\begin{codehigh}[language=latex/table] |
| 109 |
\begin{tabular}[b]{|lc|r|} |
| 110 |
\hline |
| 111 |
One & Two & Three \\ |
| 112 |
%\hline |
| 113 |
Four & Five & Six \\ |
| 114 |
\hline%\hline\hline |
| 115 |
Seven & Eight & Nine \\ |
| 116 |
\hline |
| 117 |
\end{tabular} |
| 118 |
\end{codehigh} |
| 119 |
|
| 120 |
There is also a \texttt{codehigh*} environment which shows each space character as \textvisiblespace{} in the code. |
| 121 |
|
| 122 |
\section{Highlighting Demo} |
| 123 |
|
| 124 |
The followings are typeset by \verb!demohigh! environment with option \verb!language=latex/table!. |
| 125 |
\begin{demohigh}[language=latex/table] |
| 126 |
\begin{tabular}{lccr} |
| 127 |
\hline |
| 128 |
Alpha & Beta & Gamma & Delta \\ |
| 129 |
\hline |
| 130 |
Epsilon & Zeta & Eta & Theta \\ |
| 131 |
\hline |
| 132 |
Iota & Kappa & Lambda & Mu \\ |
| 133 |
\hline |
| 134 |
\end{tabular} |
| 135 |
\end{demohigh} |
| 136 |
\begin{demohigh}[language=latex/table] |
| 137 |
\begin{tabular}{llccrr} |
| 138 |
\hline |
| 139 |
Alpha & Beta & Gamma & Delta & Epsilon & Zeta \\ |
| 140 |
\hline |
| 141 |
Eta & Theta & Iota & Kappa & Lambda & Mu \\ |
| 142 |
\hline |
| 143 |
\end{tabular} |
| 144 |
\end{demohigh} |
| 145 |
Note that \verb!demohigh! environment will measure the width of source lines. |
| 146 |
When it is too large, the result will be put below. |
| 147 |
|
| 148 |
There is also a \texttt{demohigh*} environment which shows each space character as \textvisiblespace{} in the code. |
| 149 |
|
| 150 |
\section{Highlighting File} |
| 151 |
|
| 152 |
Using \verb!\dochighinput! command, you can input and highlight some file. |
| 153 |
The last chapter of this manual is typeset with the following code line: |
| 154 |
\begin{codehigh} |
| 155 |
\dochighinput[language=latex/latex3]{codehigh.sty} |
| 156 |
\end{codehigh} |
| 157 |
|
| 158 |
In reading an input file, lines starting wtih \verb!%%%! will be omitted, |
| 159 |
and lines starting with \verb!%%>! will be extracted and typeset as normal text. |
| 160 |
|
| 161 |
\section{Customization} |
| 162 |
|
| 163 |
The following example changes default background colors with \verb!\CodeHigh! command: |
| 164 |
\begin{codehigh} |
| 165 |
\CodeHigh{language=latex/table,style/main=yellow9,style/code=red9,style/demo=azure9} |
| 166 |
\end{codehigh} |
| 167 |
\CodeHigh{language=latex/table,style/main=yellow9,style/code=red9,style/demo=azure9} |
| 168 |
Note that \verb!codehigh! package will load \verb!ninecolors!% |
| 169 |
\footnote{\url{https://www.ctan.org/pkg/ninecolors}} package for proper color contrast. |
| 170 |
\begin{demohigh} |
| 171 |
\begin{tabular}{lccr} |
| 172 |
\hline |
| 173 |
Alpha & Beta & Gamma & Delta \\ |
| 174 |
\hline |
| 175 |
Epsilon & Zeta & Eta & Theta \\ |
| 176 |
\hline |
| 177 |
Iota & Kappa & Lambda & Mu \\ |
| 178 |
\hline |
| 179 |
\end{tabular} |
| 180 |
\end{demohigh} |
| 181 |
|
| 182 |
To modify or add languages and themes, please read the source files |
| 183 |
\verb!codehigh.sty! and \verb!codehigh.lua! for reference. |
| 184 |
|
| 185 |
\section{Fake Verbatim Command} |
| 186 |
|
| 187 |
\CodeHigh{language=latex/table,style/main=gray9,style/code=gray9,style/demo=white} |
| 188 |
|
| 189 |
To ease the pain of writing verbatim commands |
| 190 |
(such as in \verb|tabularx| and \verb|tabularray| tables), |
| 191 |
This package provides \verb|\fakeverb| command. |
| 192 |
|
| 193 |
This command will remove the backslashes in the following control symbols |
| 194 |
before typesetting its content: |
| 195 |
|
| 196 |
\renewcommand\arraystretch{1.3} |
| 197 |
\begin{center} |
| 198 |
\begin{tabularx}{0.9\linewidth}{llX} |
| 199 |
\toprule |
| 200 |
Input & Result & Remark \\ |
| 201 |
\midrule |
| 202 |
\fakeverb{\\\\} & \fakeverb{\\} & |
| 203 |
Need to be escaped only when typesetting other control symbols in this table \\ |
| 204 |
\fakeverb{\\\{} & \fakeverb{\{} & |
| 205 |
Need to be escaped only when left and right curly braces are unmatched \\ |
| 206 |
\fakeverb{\\\}} & \fakeverb{\}} & |
| 207 |
Need to be escaped only when left and right curly braces are unmatched \\ |
| 208 |
\fakeverb{\\\#} & \fakeverb{\#} & |
| 209 |
Always need to be escaped \\ |
| 210 |
\fakeverb{\\\^} & \fakeverb{\^} & |
| 211 |
Need to be escaped only when there are more than one in a row \\ |
| 212 |
\texttt{\textbackslash\textvisiblespace} & \texttt{\textvisiblespace} & |
| 213 |
Need to be escaped only when more than one in a row or after control words \\ |
| 214 |
\fakeverb{\\\%} & \fakeverb{\%} & |
| 215 |
Always need to be escaped \\ |
| 216 |
\bottomrule |
| 217 |
\end{tabularx} |
| 218 |
\end{center} |
| 219 |
|
| 220 |
The argument of \verb|\fakeverb| command need to be enclosed with curly braces. |
| 221 |
Therefore it could be safely used inside \verb|tabularray| tables and other LaTeX commands. |
| 222 |
|
| 223 |
Here is an example of using \verb!\fakeverb! commands inside \verb|tabularx| environment: |
| 224 |
|
| 225 |
\begin{demohigh}[language=latex/table] |
| 226 |
\begin{tabularx}{0.5\textwidth}{lX} |
| 227 |
\hline |
| 228 |
Alpha & \fakeverb{\abc{}$&^_^uvw 123} \\ |
| 229 |
\hline |
| 230 |
Beta & \fakeverb{\bfseries\ \#\%} \\ |
| 231 |
\hline |
| 232 |
\end{tabularx} |
| 233 |
\end{demohigh} |
| 234 |
|
| 235 |
Here is another example of using \verb!\fakeverb! commands inside \verb|\fbox| command: |
| 236 |
|
| 237 |
\begin{demohigh}[language=latex/latex2] |
| 238 |
Hello\fbox{\fakeverb{$\left\\\{A\right.$\#}}Verb! |
| 239 |
\end{demohigh} |
| 240 |
|
| 241 |
\chapter{The Source Code} |
| 242 |
|
| 243 |
\dochighinput[language=latex/latex3]{codehigh.sty} |
| 244 |
|
| 245 |
\end{document} |