| 1 |
\documentclass{ctxdoc-en} |
| 2 |
\usepackage{minted} |
| 3 |
\usepackage{listings} |
| 4 |
\usepackage{xcolor} |
| 5 |
\usepackage{xcolor-material} |
| 6 |
\usepackage{codebox} |
| 7 |
|
| 8 |
% \renewcommand{\thecvcounter}{\thesection.\arabic{cvcounter}} |
| 9 |
\NewDocumentCommand{\init}{+v}{\hspace{\fill}Init~=~\textcolor{blue}{\bfseries#1}} |
| 10 |
\DeclareDocumentCommand\opt{m}{\texttt{#1}} |
| 11 |
\DeclareDocumentCommand\kvopt{mm} |
| 12 |
{\texttt{#1\breakablethinspace=\breakablethinspace#2}} |
| 13 |
\def\breakablethinspace{\hskip 0.16667em\relax} |
| 14 |
|
| 15 |
\title{\bfseries\pkg{codebox}:programming code box} |
| 16 |
\author{Nan Geng\\ \url{[email protected]}} |
| 17 |
\date{2022/01/28\qquad v1.0.4\thanks{\url{https://github.com/registor/codebox}} |
| 18 |
\thanks{\url{https://gitee.com/nwafu_nan/codebox}}} |
| 19 |
|
| 20 |
\begin{document} |
| 21 |
|
| 22 |
\maketitle |
| 23 |
|
| 24 |
\begin{abstract} |
| 25 |
|
| 26 |
\pkg{codebox} is a \pkg{tcolorbox}-based package developed with \LaTeX3, |
| 27 |
which provides environments \env{codebox} and \env{codeview}, and macros |
| 28 |
\tn{codefile} and \tn{cvfile} for typsetting programming source code box. |
| 29 |
|
| 30 |
The environments create codebox with it's body and |
| 31 |
macros is used to read in the source code file and output is in the codebox. |
| 32 |
|
| 33 |
The starred environments and macros are also provided |
| 34 |
to get codebox with comments at the bottom of box. |
| 35 |
|
| 36 |
All codebox style can be setted by \tn{codeset} macro |
| 37 |
or environment's and macro's key-value \oarg{options}. |
| 38 |
|
| 39 |
\end{abstract} |
| 40 |
|
| 41 |
\tableofcontents |
| 42 |
|
| 43 |
\section{introduction} |
| 44 |
|
| 45 |
\pkg{codebox} is a \LaTeX3 package for typesetting programming source code box. |
| 46 |
|
| 47 |
Both \env{codebox} and \env{codeview} environment are provided with enironment body. |
| 48 |
At the same time, both \tn{codefile} and \tn{cvfile} macros are created |
| 49 |
for reading source code file. |
| 50 |
|
| 51 |
The starred environments(\env{codebox*} and \env{codeview*}) and |
| 52 |
macros(\tn{codefile*} and \tn{cvfile*}) are also provided |
| 53 |
to get codebox with comments at the bottom of box. |
| 54 |
|
| 55 |
\section{interface} |
| 56 |
|
| 57 |
\subsection{\env{codebox} and \env{codebox*} environments} |
| 58 |
|
| 59 |
\begin{function}[added=2021-12-25,updated=2021-12-25]{codebox,codebox*} |
| 60 |
\begin{syntax} |
| 61 |
\tn{begin}\{codebox\}\oarg{options}\Arg{codebox title} |
| 62 |
..... |
| 63 |
\tn{end}\{codebox\} |
| 64 |
\tn{begin}\{codebox*\}\oarg{options}\Arg{codebox title} |
| 65 |
..... |
| 66 |
\tn{end}\{codebox*\} |
| 67 |
\end{syntax} |
| 68 |
Typesetting codebox with environment body. |
| 69 |
You can set the title of the codebox with \Arg{codebox title}. |
| 70 |
|
| 71 |
The appearance of the codebox is set by key-value in \oarg{options}. |
| 72 |
|
| 73 |
The starred environment \env{codebox*} is used to add comments at the bottom of the codebox, |
| 74 |
note that this needs to be done with \kvopt{\meta{comments}}{\meta{texts}} in \oarg{options}. |
| 75 |
|
| 76 |
Of course the key-value \oarg{options} can alse be set |
| 77 |
via the comma-separated key-value list of the \tn{codeset} macro. |
| 78 |
\end{function} |
| 79 |
|
| 80 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 81 |
\centering |
| 82 |
\begin{codebox}{CodeBox Title} |
| 83 |
#include <stdio.h> |
| 84 |
#include <stdlib.h> |
| 85 |
|
| 86 |
int main(void) |
| 87 |
{ |
| 88 |
printf("Hello World!\n"); |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
\end{codebox} |
| 93 |
\end{Verbatim} |
| 94 |
|
| 95 |
\begin{center} |
| 96 |
\begin{minipage}{0.85\textwidth} |
| 97 |
\begin{codebox}{CodeBox Title} |
| 98 |
#include <stdio.h> |
| 99 |
#include <stdlib.h> |
| 100 |
|
| 101 |
int main(void) |
| 102 |
{ |
| 103 |
printf("Hello World!\n"); |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
\end{codebox} |
| 108 |
\end{minipage} |
| 109 |
\end{center} |
| 110 |
|
| 111 |
\subsection{\tn{codefile} and \tn{codefile*} macros} |
| 112 |
|
| 113 |
\begin{function}[added=2021-12-25,updated=2021-12-25]{\codefile,\codefile*} |
| 114 |
\begin{syntax} |
| 115 |
\tn{codefile} \oarg{options} \Arg{codebox title} \Arg{code file} |
| 116 |
\tn{codefile*} \oarg{options} \Arg{codebox title} \Arg{code file} |
| 117 |
\end{syntax} |
| 118 |
Typesetting codebox from a source code file. |
| 119 |
You can set the title of the codebox with \Arg{codebox title}. |
| 120 |
|
| 121 |
The appearance of the codebox is set by key-value in \oarg{options}. |
| 122 |
|
| 123 |
The starred environment \tn{codefile*} is used to add comments at the bottom of the codebox, |
| 124 |
note that this needs to be done with \kvopt{\meta{comments}}{\meta{texts}} in \oarg{options}. |
| 125 |
|
| 126 |
Of course the key-value \oarg{options} can alse be set |
| 127 |
via the comma-separated key-value list of the \tn{codeset} macro. |
| 128 |
\end{function} |
| 129 |
\newpage |
| 130 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 131 |
\centering |
| 132 |
\codefile{CodeBox Title}{test.c} |
| 133 |
\end{Verbatim} |
| 134 |
|
| 135 |
\begin{center} |
| 136 |
\begin{minipage}{0.85\textwidth} |
| 137 |
\codefile{CodeBox Title}{test.c} |
| 138 |
\end{minipage} |
| 139 |
\end{center} |
| 140 |
|
| 141 |
\subsection{\env{codeview} and \env{codeview*} environments} |
| 142 |
|
| 143 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{codeview,codeview*} |
| 144 |
\begin{syntax} |
| 145 |
\tn{begin}\{codeview\}\oarg{options}\Arg{codeview title} |
| 146 |
..... |
| 147 |
\tn{end}\{codeview\} |
| 148 |
\tn{begin}\{codeview*\}\oarg{options}\Arg{codeview title} |
| 149 |
..... |
| 150 |
\tn{end}\{codeview*\} |
| 151 |
\end{syntax} |
| 152 |
Typesetting code viewer with environment body. |
| 153 |
You can set the title of the code viewer with \Arg{codeview title}. |
| 154 |
|
| 155 |
The appearance of the code viewer is set by key-value in \oarg{options}. |
| 156 |
|
| 157 |
The starred environment \env{codeview*} is used to add comments at the bottom of the codebox, |
| 158 |
note that this needs to be done with \kvopt{\meta{comments}}{\meta{texts}} in \oarg{options}. |
| 159 |
|
| 160 |
Of course the key-value \oarg{options} can alse be set |
| 161 |
via the comma-separated key-value list of the \tn{codeset} macro. |
| 162 |
\end{function} |
| 163 |
|
| 164 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 165 |
\centering |
| 166 |
\begin{codeview}{CodeViewer Title} |
| 167 |
#include <stdio.h> |
| 168 |
#include <stdlib.h> |
| 169 |
|
| 170 |
int main(void) |
| 171 |
{ |
| 172 |
printf("Hello World!\n"); |
| 173 |
|
| 174 |
return 0; |
| 175 |
} |
| 176 |
\end{codeview} |
| 177 |
\end{Verbatim} |
| 178 |
|
| 179 |
\begin{center} |
| 180 |
\begin{minipage}{0.85\textwidth} |
| 181 |
\begin{codeview}{CodeViewer Title} |
| 182 |
#include <stdio.h> |
| 183 |
#include <stdlib.h> |
| 184 |
|
| 185 |
int main(void) |
| 186 |
{ |
| 187 |
printf("Hello World!\n"); |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
\end{codeview} |
| 192 |
\end{minipage} |
| 193 |
\end{center} |
| 194 |
|
| 195 |
\subsection{\tn{cvfile} and \tn{cvfile*} macros} |
| 196 |
|
| 197 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{\cvfile,\cvfile*} |
| 198 |
\begin{syntax} |
| 199 |
\tn{cvfile} \oarg{options} \Arg{codeview title} \Arg{code file} |
| 200 |
\tn{cvfile*} \oarg{options} \Arg{codeview title} \Arg{code file} |
| 201 |
\end{syntax} |
| 202 |
Typesetting code viewer from a source code file. |
| 203 |
You can set the title of the code viewer with \Arg{codeview title}. |
| 204 |
|
| 205 |
The appearance of the code viewer is set by key-value in \oarg{options}. |
| 206 |
|
| 207 |
The starred environment \tn{vcfile*} is used to add comments at the bottom of the codebox, |
| 208 |
note that this needs to be done with \kvopt{\meta{comments}}{\meta{texts}} in \oarg{options}. |
| 209 |
|
| 210 |
Of course the key-value \oarg{options} can alse be set |
| 211 |
via the comma-separated key-value list of the \tn{codeset} macro. |
| 212 |
\end{function} |
| 213 |
|
| 214 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 215 |
\centering |
| 216 |
\cvfile*[comments=this is a simple C code]{CodeViewer Title}{test.c} |
| 217 |
\end{Verbatim} |
| 218 |
|
| 219 |
\begin{center} |
| 220 |
\begin{minipage}{0.85\textwidth} |
| 221 |
\cvfile*[comments=this is a simple C code]{CodeViewer Title}{test.c} |
| 222 |
\end{minipage} |
| 223 |
\end{center} |
| 224 |
|
| 225 |
\section{Options} |
| 226 |
|
| 227 |
The \pkg{codebox} package provides a number of options to set the style of the codebox. |
| 228 |
The following options can be set with \tn{codeset} macro. |
| 229 |
Also, these options can be set with the all environment's |
| 230 |
or command's \oarg{options}. |
| 231 |
|
| 232 |
\subsection{code engine} |
| 233 |
|
| 234 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{minted} |
| 235 |
\begin{syntax} |
| 236 |
minted = <\TTF> \init{true} |
| 237 |
\end{syntax} |
| 238 |
\opt{minted} is used to set code highlight engine, if it is \textbf{true} then |
| 239 |
the \pkg{minted} package is used, if it is \textbf{false} then |
| 240 |
the \pkg{listings} package is used. The default is \textbf{true}. |
| 241 |
\end{function} |
| 242 |
|
| 243 |
\subsection{language} |
| 244 |
|
| 245 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{lang} |
| 246 |
\begin{syntax} |
| 247 |
lang = \Arg{source code language} \init{C} |
| 248 |
\end{syntax} |
| 249 |
\opt{lang} is used to set source code language. |
| 250 |
The default is \textbf{C}. |
| 251 |
\end{function} |
| 252 |
|
| 253 |
\subsection{title prefix} |
| 254 |
|
| 255 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{pretitle} |
| 256 |
\begin{syntax} |
| 257 |
pretitle = \Arg{title prefix} \init{Code} |
| 258 |
\end{syntax} |
| 259 |
\opt{pretitle} is used to set prefix of code counter. |
| 260 |
The default is \textbf{Code}。 |
| 261 |
\end{function} |
| 262 |
|
| 263 |
\subsection{code highlight style} |
| 264 |
|
| 265 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{codestyle} |
| 266 |
\begin{syntax} |
| 267 |
codestyle = \Arg{highlight style} \init{codeblocks} |
| 268 |
\end{syntax} |
| 269 |
\opt{codestyle} is used to set code highlight style, valid only for the \pkg{minted} engine. |
| 270 |
The default is \textbf{codeblocks}. |
| 271 |
\end{function} |
| 272 |
|
| 273 |
\subsection{code fontsize} |
| 274 |
|
| 275 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{codesize} |
| 276 |
\begin{syntax} |
| 277 |
codesize = \Arg{fontsize macro} \init{\small} |
| 278 |
\end{syntax} |
| 279 |
\opt{codesize} is used to set code fontsize, valid only for \pkg{minted} engine. |
| 280 |
The default is\textbf{\tn{small}}. |
| 281 |
\end{function} |
| 282 |
|
| 283 |
\subsection{comment contents} |
| 284 |
|
| 285 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{comments} |
| 286 |
\begin{syntax} |
| 287 |
comments = \Arg{texts} \init{nothing} |
| 288 |
\end{syntax} |
| 289 |
\opt{comments} is used to set comment contents. |
| 290 |
The default is \textbf{nothing}. |
| 291 |
\end{function} |
| 292 |
|
| 293 |
\subsection{comment format} |
| 294 |
|
| 295 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{commentf} |
| 296 |
\begin{syntax} |
| 297 |
commentf = \Arg{format macros} \init{\small\sffamily} |
| 298 |
\end{syntax} |
| 299 |
\opt{commentf} is used to set comment format at codebox bottom. |
| 300 |
The default is \textbf{\tn{small}\tn{sffamily}}. |
| 301 |
\end{function} |
| 302 |
|
| 303 |
\subsection{code baseline stretch} |
| 304 |
|
| 305 |
\begin{function}[added=2021-12-26,updated=2021-12-26]{codestretch} |
| 306 |
\begin{syntax} |
| 307 |
codestretch = \Arg{float number} \init{1.0} |
| 308 |
\end{syntax} |
| 309 |
\opt{codestretch} is used to set code baseline stretch, valid only for \pkg{minted} engine. |
| 310 |
The default is\textbf{1.0}. |
| 311 |
\end{function} |
| 312 |
|
| 313 |
\subsection{seperation between line number and code} |
| 314 |
|
| 315 |
\begin{function}[added=2021-12-26,updated=2022-1-28]{linenumsep} |
| 316 |
\begin{syntax} |
| 317 |
linenumsep = \Arg{float number} \init{1.80} |
| 318 |
\end{syntax} |
| 319 |
\opt{linenumsep} is used to set the seperation between line number and code, |
| 320 |
valid only for \pkg{minted} engine. |
| 321 |
Note the unit is mm. |
| 322 |
The default is\textbf{3.0}. |
| 323 |
\end{function} |
| 324 |
|
| 325 |
\subsection{label} |
| 326 |
|
| 327 |
\begin{function}[added=2022-1-4,updated=2022-1-4]{label} |
| 328 |
\begin{syntax} |
| 329 |
label = \Arg{label name} \init{nothing} |
| 330 |
\end{syntax} |
| 331 |
\opt{label} is used to set \tn{ref} 's label name, |
| 332 |
it is for \env{codeview}/\env{codeview*} and \tn{cvfile}/\tn{cvfile*}. |
| 333 |
The default is \textbf{nothing}. |
| 334 |
\end{function} |
| 335 |
|
| 336 |
\section{The counter} |
| 337 |
|
| 338 |
\begin{function}[added=2021-12-28,updated=2021-12-28]{cvconuter} |
| 339 |
The \pkg{codebox} package provides a \texttt{cvcounter} counter |
| 340 |
that can be used to count code boxes with environment \env{codeview}/\env{codeview*} |
| 341 |
and the command \tn{cvfile}/\tn{cvfile*}. |
| 342 |
|
| 343 |
By default, if \tn{thechapter} exists, its parent counter is set to \textbf{chapter} |
| 344 |
otherwise it will be counted uniformly by full text. |
| 345 |
|
| 346 |
You can use |
| 347 |
\tn{renewcommand\{\tn{thecvcounter}\}\{\tn{thechapter.\tn{arabic\{cvcounter\}}}\}} |
| 348 |
or something like this macro to change the numbered output. |
| 349 |
\end{function} |
| 350 |
|
| 351 |
\section{Examples} |
| 352 |
|
| 353 |
The \pkg{codebox} package can be used in situations |
| 354 |
where the highlight programming source code needs to be typeset |
| 355 |
to avoid the use of screenshots. |
| 356 |
Code box can be with or without underline comments. |
| 357 |
|
| 358 |
\newpage |
| 359 |
|
| 360 |
\subsection{Java code} |
| 361 |
The language can be set with \tn{codeset} macro. |
| 362 |
|
| 363 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 364 |
\centering |
| 365 |
\codeset{lang=java} |
| 366 |
\codefile{Java CodeBox}{hellojava.java} |
| 367 |
\end{Verbatim} |
| 368 |
|
| 369 |
\begin{center} |
| 370 |
\begin{minipage}{0.85\textwidth} |
| 371 |
\codeset{lang=java} |
| 372 |
\codefile{Java CodeBox}{hellojava.java} |
| 373 |
\end{minipage} |
| 374 |
\end{center} |
| 375 |
|
| 376 |
\subsection{Python code} |
| 377 |
The language can be set with \opt{options}, of course you can label and ref it |
| 378 |
such as code \ref{code-test}. |
| 379 |
|
| 380 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 381 |
\centering |
| 382 |
\cvfile[lang=python,label=code-test]{Python CodeBox}{hellopy.py} |
| 383 |
\end{Verbatim} |
| 384 |
|
| 385 |
\begin{center} |
| 386 |
\begin{minipage}{0.85\textwidth} |
| 387 |
\cvfile[lang=python,label=code-test]{Python CodeBox}{hellopy.py} |
| 388 |
\end{minipage} |
| 389 |
\end{center} |
| 390 |
|
| 391 |
\newpage |
| 392 |
|
| 393 |
\subsection{listings engine} |
| 394 |
\pkg{listings} engine can be set with \kvopt{\meta{minted}}{\meta{false}}. |
| 395 |
|
| 396 |
\begin{Verbatim}[frame=none,numbers=left,gobble=2] |
| 397 |
\centering |
| 398 |
\cvfile[minted=false,lang=c]{C CodeBox}{test.c} |
| 399 |
\end{Verbatim} |
| 400 |
|
| 401 |
\begin{center} |
| 402 |
\begin{minipage}{0.85\textwidth} |
| 403 |
\cvfile[minted=false,lang=c]{C CodeBox}{test.c} |
| 404 |
\end{minipage} |
| 405 |
\end{center} |
| 406 |
|
| 407 |
\end{document} |