summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/codehigh/codehigh.tex
blob: 35f648dfe1dd7a1356f90a6b2e0151885fbd26f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
%  -*- coding: utf-8 -*-
\documentclass[oneside]{book}
\usepackage[a4paper,margin=2cm]{geometry}

\newcommand*{\myversion}{2022D}
\newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)}
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi}

\setlength{\parindent}{0pt}
\setlength{\parskip}{4pt plus 1pt minus 1pt}

\usepackage{codehigh}
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  urlcolor=blue3,
  linkcolor=green3,
}

\begin{document}

\title{\textsf{\color{green3}Codehigh: Highlight Codes and Demos with l3RegEx and LPeg}}
\author{Jianrui Lyu (tolvjr@163.com)\\ \url{https://github.com/lvjr/codehigh}}
\date{\mydate}
\maketitle

\tableofcontents

\chapter{Package Interfaces}

\section{Introduction}

\verb!Codehigh! package uses \verb!l3regex!\footnote{\url{https://www.ctan.org/pkg/l3regex}}
package in \LaTeX3 Programming Layer to parse and highlight source codes and demos.
It is more powerful than \verb!listings! package, and more easy to use than \verb!minted! package.
But it is slower than both of them.
Therefore in LuaTeX the package provides another way to highlight code: using \verb!LPeg!%
\footnote{\url{http://www.inf.puc-rio.br/~roberto/lpeg/}}.
\verb!LPeg! is much more powerful and faster than \verb!l3regex!.

%At present, this package is in \underline{\color{red3}\textbf{experimental}} status.
%Don’t use it in important documents, unless you have time
%to update them for the newer versions of \verb!codehigh! package in the future.

\section{Highlighting Code}

There are several predefined languages: \verb!latex!, \verb!latex/latex2!, \verb!latex/latex3!,
\verb!latex/math! and \verb!latex/table!.
The following example is typeset by \verb!codehigh! environment with default option \verb!language=latex!.
\begin{codehigh}
%  -*- coding: utf-8 -*-
\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{codehigh}
\usepackage{hyperref}
\newcommand*{\myversion}{2021C}
\newcommand*{\mydate}{Version \myversion\ (\the\year-\mylpad\month-\mylpad\day)}
\newcommand*{\mylpad}[1]{\ifnum#1<10 0\the#1\else\the#1\fi}
\setlength{\abc}{1}
\begin{document}
% some comment
\section{Section Name}
\subsection*{Suction Name}
Math $a+b$.
\end{document}
\end{codehigh}

The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/latex2!.
\begin{codehigh}[language=latex/latex2]
\def\abcd#1#2{
  % some comment
  \unskip
  \setlength{\parindent}{0pt}%
  \setlength{\parskip}{0pt}%
  \setcounter{choice}{0}%
  \let\item=\my@item@temp
  \settowidth{\my@item@len}{\vbox{\halign{##1\hfil\cr\BODY\crcr}}}%
  \setcounter{choice}{0}%
}
\end{codehigh}
This language is for highlighting LaTeX2 classes and packages.
It highlights private commands and public commands with different colors.

The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/latex3!.
\begin{codehigh}[language=latex/latex3]
\cs_new_protected:Npn \__codehigh_typeset_demo:
  {
    \__codehigh_build_code:
    \__codehigh_build_demo:
    \dim_set:Nn \l_tmpa_dim { \box_wd:N \g__codehigh_code_box }
    \dim_set:Nn \l_tmpb_dim { \box_wd:N \g__codehigh_demo_box }
    \par\addvspace{0.5em}\noindent
    % more code
  }
\end{codehigh}
This language is for highlighting LaTeX3 classes and packages.
It highlights private commands/variables and public commands/variables with different colors.

The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/math!.
\begin{codehigh}[language=latex/math]
\begin{align}
  \pi\left[\frac13z^3\right]\sin(2x+1)_0^4 = \frac{64}{3}\pi
\end{align}
\end{codehigh}

The following example is typeset by \verb!codehigh! environment with option \verb!language=latex/table!.
\begin{codehigh}[language=latex/table]
\begin{tabular}[b]{|lc|r|}
\hline
One   &  Two  & Three \\
%\hline
Four  & Five  &   Six \\
\hline%\hline\hline
Seven & Eight &  Nine \\
\hline
\end{tabular}
\end{codehigh}

\section{Highlighting Demo}

The followings are typeset by \verb!demohigh! environment with option \verb!language=latex/table!.
\begin{demohigh}[language=latex/table]
\begin{tabular}{lccr}
\hline
 Alpha   & Beta  & Gamma  & Delta \\
\hline
 Epsilon & Zeta  & Eta    & Theta \\
\hline
 Iota    & Kappa & Lambda & Mu    \\
\hline
\end{tabular}
\end{demohigh}
\begin{demohigh}[language=latex/table]
\begin{tabular}{llccrr}
\hline
 Alpha & Beta  & Gamma & Delta & Epsilon & Zeta \\
\hline
 Eta   & Theta & Iota  & Kappa & Lambda & Mu \\
\hline
\end{tabular}
\end{demohigh}
Note that \verb!demohigh! environment will measure the width of source lines.
When it is too large, the result will be put below.

\section{Highlighting File}

Using \verb!\dochighinput! command, you can input and highlight some file.
The last chapter of this manual is typeset with the following code line:
\begin{codehigh}
\dochighinput[language=latex/latex3]{codehigh.sty}
\end{codehigh}

In reading an input file, lines starting wtih \verb!%%%! will be omitted,
and lines starting with \verb!%%>! will be extracted and typeset as normal text.

\section{Customization}

The following example changes default background colors with \verb!\CodeHigh! command:
\begin{codehigh}
\CodeHigh{language=latex/table,style/main=yellow9,style/code=red9,style/demo=azure9}
\end{codehigh}
\CodeHigh{language=latex/table,style/main=yellow9,style/code=red9,style/demo=azure9}
Note that \verb!codehigh! package will load \verb!ninecolors!%
\footnote{\url{https://www.ctan.org/pkg/ninecolors}} package for proper color contrast.
\begin{demohigh}
\begin{tabular}{lccr}
\hline
 Alpha   & Beta  & Gamma  & Delta \\
\hline
 Epsilon & Zeta  & Eta    & Theta \\
\hline
 Iota    & Kappa & Lambda & Mu    \\
\hline
\end{tabular}
\end{demohigh}

To modify or add languages and themes, please read the source files
\verb!codehigh.sty! and \verb!codehigh.lua! for reference.

\chapter{The Source Code}

\dochighinput[language=latex/latex3]{codehigh.sty}

\end{document}