\documentclass[luatex,fontsize=8pt,paper=b5,twoside]{jlreq}% \usepackage{amsmath,amssymb} \usepackage{booktabs,caption, tabularray} \usepackage{luwa-ul} \usepackage{KKsymbols} \usepackage[most]{tcolorbox} \usepackage{KKluaverb} %%% fonts (This can be omitted.) \makeatletter \RequirePackage[no-math]{fontspec} \RequirePackage[no-math,match,scale=1]{luatexja-fontspec} \RequirePackage[hiragino-pro,deluxe,expert]{luatexja-preset} \setmainfont{HiraMinPro-W3}[BoldFont=HiraMinPro-W6] \setmainjfont{HiraMinPro-W3}[BoldFont=HiraMinPro-W6] \newfontfamily{\sfhira@pre}{HiraKakuPro-W3}[BoldFont=HiraKakuPro-W6] \newjfontfamily{\sfhiraj@pre}{HiraKakuPro-W3}[BoldFont=HiraKakuPro-W6] \newfontfamily{\mchira@pre}{HiraMinPro-W3}[BoldFont=HiraMinPro-W6] \newjfontfamily{\mchiraj@pre}{HiraMinPro-W3}[BoldFont=HiraMinPro-W6] \newfontfamily{\gthira@pre}{HiraKakuPro-W3}[BoldFont=HiraKakuPro-W6,FontFace={eb}{\shapedefault}{HiraKakuStd-W8}] \newjfontfamily{\gthiraj@pre}{HiraKakuPro-W3}[BoldFont=HiraKakuPro-W6,FontFace={eb}{\shapedefault}{HiraKakuStd-W8}] \newfontfamily{\mghira@pre}{HiraMaruPro-W4} \newjfontfamily{\mghiraj@pre}{HiraMaruPro-W4} \renewcommand{\sffamily}{\sfhira@pre\sfhiraj@pre} \renewcommand{\mcfamily}{\mchira@pre\mchiraj@pre} \renewcommand{\gtfamily}{\gthira@pre\gthiraj@pre} \renewcommand{\mgfamily}{\mghira@pre\mghiraj@pre} \makeatother %%% %%% mapping \begin{luacode*} KKLuaVerb.presets["TeXStyle"] = { map = { DarkCyan = { "{", "}" }, OrangeRed = { "[", "]" }, DeepPink = { "(", ")" }, DarkGoldenrod = { "&", "$", "^", "_"}, CornflowerBlue = { "\\documentclass", "\\usepackage", "\\begin", "\\end", "\\section", "\\subsection", "\\chapter", "\\makeatletter", "\\makeatother", "\\ExplSyntaxOn", "\\ExplSyntaxOff" }, MediumPurple = { "\\def", "\\edef", "\\gdef", "\\xdef", "\\newcommand", "\\renewcommand", "\\providecommand", "\\let", "\\setcopy", "\\long", "\\global" }, Magenta = { "\\if", "\\else", "\\fi", "\\ifx", "\\ifdefined", "\\ifnum", "\\ifdim", "\\loop", "\\repeat", "\\noexpand" }, IndianRed = { "\\KKlvStart*", "\\KKlvEnd*" } }, options = { word_boundary = true, comment_char = "%", comment_color = "ForestGreen", escape_char = "\\", delimiters = { { start = "$", stop = "$", color = "SpringGreen3" }, { start = "\\[", stop = "\\]", color = "SpringGreen3" }, }, forced_tokens = { ["{"] = "DarkCyan", ["}"] = "DarkCyan", }, } } \end{luacode*} \begin{luacode*} KKLuaVerb.presets["LuaStyle"] = { map = { Magenta = { "if", "then", "else", "elseif", "end", "for", "while", "repeat", "until", "in", "do", "break", "return", "function" }, ProcessBlue = { "local", "true", "false", "nil" }, DarkGoldenrod = { "print", "require", "type", "tostring", "tonumber", "table", "string", "math", "io", "os", "debug", "coroutine", "self" }, DarkCyan = { "{", "}", "[", "]", "(", ")", "=", "+", "-", "*", "/", "#", "..", "==" } }, options = { word_boundary = true, comment_char = "--", comment_color = "Cerulean", escape_char = "%", delimiters = { { start = '"', stop = '"', color = "SkyBlue1" }, } } } \end{luacode*} %%% %%% hyperref \usepackage{hyperref} \hypersetup{ luatex, pdfencoding=auto, colorlinks=true, linkcolor=black, citecolor=black, urlcolor=DeepSkyBlue3, pdfborder={0 0 0}, } %%% %%% textboxes \makeatletter \newcommand{\ascb@parindent}[1]{% \setlength{\parindent}{#1}\relax% } \def\ascb@gtfamily{\gtfamily}\def\ascb@zw#1#2{#1\zw} \DeclareTColorBox{simple}{ o m O{.5} O{} }% {empty, left=2mm, right=2mm, top=-1mm, attach boxed title to top left={xshift=\ascb@zw{1.2}{11pt}}, boxed title style={empty,left=-2mm,right=-2mm}, colframe=black, coltitle=black, coltext=black, breakable, before upper={\ascb@parindent{\zw}},%%% underlay unbroken={\draw[black,line width=#3pt](title.east) -- (title.east-|frame.east) -- (frame.south east) -- (frame.south west) -- (title.west-|frame.west) -- (title.west); }, underlay first={\draw[black,line width=#3pt](title.east) -- (title.east-|frame.east) -- (frame.south east) ; \draw[black,line width=#3pt] (frame.south west) -- (title.west-|frame.west) -- (title.west); }, underlay middle={\draw[black,line width=#3pt](frame.north east) -- (frame.south east) ; \draw[black,line width=#3pt](frame.south west) -- (frame.north west) ;}, underlay last={\draw[black,line width=#3pt](frame.north east) -- (frame.south east) -- (frame.south west) -- (frame.north west) ;}, fonttitle=\ascb@gtfamily, IfValueTF={#1}{title=\hspace*{.1em}【#2】〈#1〉\hspace*{.1em}}{title=\hspace*{.1em}【#2】\hspace*{.1em}},#4} \def\KKfbox@SourceCode@phantom{\vphantom{あ}\vphantom{j}} \NewTCBListing{SourceCode}{ m m !o !O{DeepSkyBlue3} }{% enhanced, colback=black!70, colframe=Snow4, toptitle=-1mm, bottomtitle=-1mm, righttitle=-1mm, lefttitle=-1mm, arc=.5mm, title={\tcbox[on line, arc=.5mm, boxsep=0pt, boxrule=0pt, top=1mm, bottom=0.8mm, left=2mm, right=2.2mm, colback=gray!80, coltext=white]{% \raisebox{-.04\zw}{\KKfbox@SourceCode@phantom#1}}% }, fonttitle=\gtfamily\footnotesize, boxrule=0.8pt, breakable, before upper={\color{white}}, top=-0.5mm, bottom=-0.5mm, after title=\IfNoValueTF{#3}{}{{\hfill% \tcbox[on line, arc=.5mm, boxsep=0pt, boxrule=0pt, top=1mm, bottom=0.8mm, left=2mm, right=2.2mm, colback=white!80!black, coltext=#4]{\raisebox{-.04\zw}{\KKfbox@SourceCode@phantom#3}}}% }, listing only, listing options={ language={#2}, basicstyle=\ttfamily, keywordstyle=\ttfamily\color{white}, stringstyle=\itshape\color{white}, commentstyle=\small\gtfamily\color{DeepSkyBlue2}, showspaces=false,showtabs=false, breaklines=true,breakindent=0pt, showstringspaces=false, columns=fullflexible, tabsize=2, numbers=left,numbersep=1.5pt, numberstyle=\scriptsize\gtfamily\color{gray}, } } \NewTColorBox{OutPut}{ m !o !O{DeepSkyBlue3} }{% enhanced, colframe=Snow4, toptitle=-1mm, bottomtitle=-1mm, righttitle=-1mm, lefttitle=-1mm, arc=.5mm, colback=white, title={\tcbox[on line, arc=.5mm, boxsep=0pt, boxrule=0pt, top=1mm, bottom=0.8mm, left=2mm, right=2.2mm, colback=gray!40, coltext=DeepSkyBlue3]{\raisebox{-.04\zw}{\KKfbox@SourceCode@phantom#1}}% }, fonttitle=\gtfamily\footnotesize, boxrule=0.8pt, breakable, top=-0.5mm, bottom=-0.5mm, after title=\IfNoValueTF{#2}{}{{\hfill% \tcbox[on line, arc=.5mm, boxsep=0pt, boxrule=0pt, top=1mm, bottom=0.8mm, left=2mm, right=2.2mm, colback=white!80!black, coltext=#3]{\raisebox{-.04\zw}{\KKfbox@SourceCode@phantom#2}}}% }, bottom=2mm, top=2mm, } \makeatother %%% %%% titlepage \title{\texttt{KKluaverb} Package Documentation} \author{Kosei Kawaguchi a.k.a. KKTeX} \date{Version 2.1.2 (2026/01/27)} %%% \begin{document} \KKvUsePreset{TeXStyle} %%% title and toc \begin{titlepage} \maketitle \end{titlepage} \newpage \tableofcontents \newpage %%% \section{Outline} The \KKverb|KKluaverb| is a LaTeX package which provides a Lua-enhanced \KKverb|\verb| command, \KKverb|\KKverb|. \thinHighLight{It can be used in the table of contents, index, underline commands (e.g., \KKverb|\underLineKK| provided by the luwa-ul package), \KKverb|tblr| environment and so on.} \section{Acknowledgements / Credit} In devoloping this package, I made use of an algorithm which is used in ``bxrawstr'' (by Takayuki YATO)\footnote{% package: \url{https://gist.github.com/zr-tex8r/c7901658a866adfcd3cd66b6dfa86997}\par article: \url{https://zrbabbler.hatenablog.com/entry/20181222/1545495849}}. \section{Dependencies} This package internally uses the following packages: \begin{itemize} \item luatexbase, luacode \item pgfkeys \item xcolor \end{itemize} \section{Basic Usage of Lua-enhanced \KKverb|\verb|} \subsection{Fundamental Behavior} As mentioned in the ``Outline'' section, this package mainly provides enhanced \KKverb|\verb| command. The usage is not much different from the normal one. \KKvScanOff \begin{SourceCode}{Input}{TeX} \KKverb|\def\TEST{Test Text.}| \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} \KKverb|\def\TEST{Test Text.}| \end{OutPut} In the argument of the command, any linebreaks and blank lines are ignored. However, every space are preserved and rendered exactly as it appears. \subsection{In Verbatim Environments} When this package is loaded, the Lua-scanning which enables \KKverb|\KKverb| to detokenize its argument is activated. However, when \KKverb|\KKverb| used in verbatim environments such as ``lstlistings'' and ``tcblisting'', the argument cannot be decoded properly. \namiKK{In such cases, you should use \KKverb|\KKvScanOff| which deactivates the scanning process used internally in \KKverb|KKverb.lua|.} Of course, \KKverb|\KKvScanOn| re-activates the process. \subsection{Optional Usage} In addition to the main function of \KKverb|\KKverb|, some optional funcions are provided. \KKverb|\KKvOpChange| can change font and color of the output of \KKverb|\KKverb|. Also, it can activate or deactivate the scanner. The usage is as follows: \KKvScanOff \begin{SourceCode}{Input}{TeX} {\KKvOpChange{color=blue, font=\gtfamily, enabled=true}% \KKverb|\def\TEST{Test Text.}|} \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} {\KKvOpChange{color=blue, font=\gtfamily, enabled=true}% \KKverb|\def\TEST{Test Text.}|} \end{OutPut} \bigskip\begin{center} \captionof{table}{\KKverb|\KKvOpChange|} \begin{tblr}{ colspec = {l l l}, hline{1,Z} = {0.8pt}, hline{2} = {.4pt}, column{1} = {font=\ttfamily}, row{1} = {font=\bfseries, bg=gray9}, } Key & Default Value & Description \\ font & \KKverb|\ttfamily| & Sets the font for the verbatim text. \\ color & \KKverb|black| & Sets the text color. \\ enabled & \KKverb|true| & Activates or deactivates the scanning engine. \\ \end{tblr} \end{center}\bigskip Additionally, the \KKverb|enabled| option functions the same as \KKverb|\KKvScanOn| and \KKverb|\KKvScanOff|. Also, you can change delimiters by using \KKverb|\KKvSetDelims| as follows in order to change the delimiters of the command: \KKvScanOff \begin{SourceCode}{Input}{TeX} \KKvSetDelims{[<}{>]} Changed the delimiters: \KKverb[<\def\foo\s>] \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} \KKvSetDelims{[<}{>]} Changed the delimiters: \KKverb[<\def\foo\s>] \end{OutPut} \KKvSetDelims{|}{|} By default, the delimiters are set as "|". So you can reset them by this: \KKvScanOff \begin{SourceCode}{Input}{TeX} \KKvSetDelims{|}{|} \end{SourceCode} \KKvScanOn \subsection{Behavior in TOC, Index etc.} One of the strong points of \KKverb|\KKverb| is that it can be used in table of contents, index, and footnotes and more. While the traditional \KKverb|\verb| command always causes an error in such contexts, \KKverb|\KKverb| does not. This is achieved through the following logic, using the TOC as an example. When the TeX system generates the TOC, it produces an auxiliary file with the extension \KKverb|.toc|. In this file, entry data is typically stored in an expanded state. However, this package requires the raw text data to be passed unexpanded. This is because the text must be intercepted and processed by \KKverb|process_input_buffer| before TeX's tokenizer converts it into tokens. To resolve this, I implemented a mechanism that automatically prepends the \KKverb|\unexpanded| command to the starter flag when writing to auxiliary files: \KKcodeS+ % In TOC \noexpand\KKlvStart*\noexpand\KKlvEnd*% \KKcodeE Then, the content of the \KKverb|.toc| file is processed by TeX in the usual way subsequently, because the \KKverb|\unexpanded| is no longer present. \section{Basic Usage of Lua-enhanced \KKverb|lstlisting|} \subsection{Fundamental Behavior} \KKvScanOff \verb|\KKcodeS| and \verb|\KKcodeE| provide an environment-like output which is very close to the \verb|lstlisting|. The usage is very simple. The code between \verb|\KKcodeS| and \verb|\KKcodeE| is rendered as raw texts. If you want to attach linenumbers to the left of the each line, \verb|\KKcodeS+| will meet the request. Please note that you do not need to type \verb|\KKcodeE+|. \verb|\KKcodeS+| and \verb|\KKcodeE| is the proper pair. \begin{SourceCode}{Input}{TeX} \KKcodeS+ % A sample command \long\def\myprog#1{% \ifx#1\empty \relax \else \message{Processing...}% #1 \fi } \KKcodeE \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} \KKcodeS+ % A sample command \long\def\myprog#1{% \ifx#1\empty \relax \else \message{Processing...}% #1 \fi } \KKcodeE \end{OutPut} When you use this Lua-based environment, please keep the following points in mind: \begin{itemize} \item All indents and spaces are displayed. \item Line breaks and blank lines are rendered exactly as they appear in the source. \end{itemize} \subsubsection{\KKverb|\KKvLNChange|} This command can change the style of the linenumbers. \bigskip\begin{center} \captionof{table}{\KKverb|\KKvLNChange|} \begin{tblr}{ colspec = {l l l}, hline{1,Z} = {0.8pt}, hline{2} = {.4pt}, column{1} = {font=\ttfamily}, row{1} = {font=\bfseries, bg=gray9}, } Key & Default Value & Description \\ font & \KKverb|\ttfamily| & Sets the font for the linenumbers. \\ color & \KKverb|black!80| & Sets the color of the linenumbers. \\ size & \KKverb|\small| & Set the size of the linenumbers. \\ start & \KKverb|1| & Set the start number. \\ style & \KKverb|0| & Set the style. \\ \end{tblr} \end{center}\bigskip \KKvScanOff Please note that you do not have to use style key when you use this package in a normal way. This is because style1 and style2 are completely covered by \verb|\KKcodeS/E| or \verb|\KKcodeS+/E| environment. The following example illustrates this point. \begin{SourceCode}{Input}{TeX} {\KKvLNChange{style=1} \KKverb| % contents |} \KKcodeS % contents \KKcodeE \end{SourceCode} \noindent The two above produce identical output. To summarize, \begin{description} \item[\texttt{style=0}] This is the normal usage of the \verb|\KKverb|. \item[\texttt{style=1}] This style corresponds to \verb|\KKcodeS/E| environment. \item[\texttt{style=2}] This style corresponds to \verb|\KKcodeS+/E| environment. \end{description} \subsection{Additional Description} \KKvScanOff Some corner cases should be payed attention. \subsubsection{Note\#1} You cannot use \verb|\KKcodeS| and \verb|\KKcodeE| in the argument of \verb|\KKverb| command. When the scanner is activated, \verb|\KKcodeS| forcibly starts the encoding system, even it is wrapped by \verb|\KKverb|. If you need to display \verb|\KKcodeS| and \verb|\KKcodeE| as they are, deactivate the scanner by \verb|\KKvScanOff| and use \verb|\verb|. \subsubsection{Note\#2} Any text on the same line that falls "inside" the \verb|\KKcodeS| and \verb|\KKcodeE| boundaries is ignored, while text "outside" them is preserved. The following example will illustrate the behavior. \begin{SourceCode}{Input}{TeX} Not ignored. \KKcodeS+ Ignored. % Contents Ignored. \KKcodeE Not ignored. \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} Not ignored. \KKcodeS+ Ignored. % Contents Ignored. \KKcodeE Not ignored. \end{OutPut} \section{Color Mapping} \KKvScanOff \subsection{Basic Behavior} This package provides color-mapping option. You can specify the color of the keyword-color, comment-color, comment-token, etc. In order to set a colormap, you should use \verb|luacode*| environment as follows: \begin{SourceCode}{Input}{TeX} \begin{luacode*} KKLuaVerb.presets[""] = { map = { = { "", "",... }, = { "", "",... }, ... }, options = { word_boundary = true or false, word_components = "[]", comment_char = "", comment_color = "", escape_char = "", delimiters = { { start = '', stop = '', color = "" }, { start = '', stop = '', color = "" }, }, forced_tokens = { [""] = "", [""] = "", } } } \end{luacode*} \end{SourceCode} %%% オプション全部についてガッツリ説明書くこと! %%% 今のままだとわかりづらい! The ``color'' parameter here refers to those defined in the \verb|xcolor| package. You can set multiple presets, and can change or activate by \verb|\KKvUsePreset{}| command. \KKvScanOn \subsubsection{\KKverb|word_boundary, word_components| options} \KKvScanOff These two options are closely related to each other. In the argument of the \verb|\KKverb| command or inside of the \verb|\KKcodeS/E| environment, \KKvScanOn\KKvUsePreset{TeXStyle} the mapped keywords are colored as you specified. However, if the string is included in the other words, you need to prevent the system provided from wrongly color the string. For example, in the situation that \KKverb|\def| command is mapped as a keyword but \KKverb|\define| is not, the part of \KKverb|\def| in \KKverb|\define| should not be colored. For this purpose, I made a ``\KKverb|word_boundary| detector''. In advance, set the components of the ``word'' like this: \KKvUsePreset{LuaStyle} \KKcodeS+ word_components = "[A-Za-z0-9_]", \KKcodeE Then, activate the \KKverb|word_boundary| detector: \KKcodeS+ word_boundary = true, \KKcodeE By all this, any characters or tokens except for \KKverb|"[A-Za-z0-9_]"| works as a separater of the words or tokens, and each keywords are properly colored. By default, \KKverb|word_boundary| are set as ``\KKverb|true|'', and \KKverb|word_components| are set as \KKverb|"[A-Za-z0-9_]"|. \subsubsection{\KKverb|comment_char, comment_color, escape_char| options} These options are provided in order to set comment character, comment color, and escape character respectively. If you set \KKverb|comment_char|, the following texts in the same lina are colored as \KKverb|comment_color|. But, if you put the \KKverb|escape_char| before the \KKverb|comment_char|, the very token and the following texts are not colored as \KKverb|comment_color|. \subsubsection{\KKverb|delimiters, forced_token| options} These options are provided for the purpose of setting \KKverb|delimiter token| and the color of the texts between the tokens. Additionally, you can set multiple \KKverb|forced_token|. These are forced to be colored as you specify, even they are between the delimiters. \subsection{Example} \KKvScanOff \begin{SourceCode}{Input}{TeX}[Setting] \begin{luacode*} KKLuaVerb.presets["TeXStyle"] = { map = { DarkCyan = { "{", "}" }, OrangeRed = { "[", "]" }, DeepPink = { "(", ")" }, DarkGoldenrod = { "&", "$", "^", "_"}, CornflowerBlue = { "\\documentclass", "\\usepackage", "\\begin", "\\end", "\\section", "\\subsection", "\\chapter", "\\makeatletter", "\\makeatother", "\\ExplSyntaxOn", "\\ExplSyntaxOff" }, MediumPurple = { "\\def", "\\edef", "\\gdef", "\\xdef", "\\newcommand", "\\renewcommand", "\\providecommand", "\\let", "\\setcopy", "\\long", "\\global" }, Magenta = { "\\if", "\\else", "\\fi", "\\ifx", "\\ifdefined", "\\ifnum", "\\ifdim", "\\loop", "\\repeat", "\\noexpand" }, IndianRed = { "\\KKlvStart*", "\\KKlvEnd*" } }, options = { word_boundary = true, comment_char = "%", comment_color = "ForestGreen", escape_char = "\\", delimiters = { { start = "$", stop = "$", color = "SpringGreen3" }, { start = "\\[", stop = "\\]", color = "SpringGreen3" }, }, forced_tokens = { ["{"] = "DarkCyan", ["}"] = "DarkCyan", }, } } \end{luacode*} \end{SourceCode} \begin{SourceCode}{Input}{TeX}[Actual Use] \KKvUsePreset{TeXStyle} \KKcodeS+ \usepackage{KKluaverb} \begin{document} Hello, KKTeX! \end{document} \KKcodeE \KKcodeS { [ \ ( \% \$ # & _ ^ ) / ] } Inline mathmode: $a + b = \frac{x}{y}$ Display-style mathmode: \[a + b = \frac{x}{y}\] \KKcodeE \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} \KKvUsePreset{TeXStyle} \KKcodeS+ \usepackage{KKluaverb} \begin{document} Hello, KKTeX! \end{document} \KKcodeE \KKcodeS { [ \ ( \% \$ # & _ ^ ) / ] } Inline mathmode: $a + b = \frac{x}{y}$ Display-style mathmode: \[a + b = \frac{x}{y}\] \KKcodeE \end{OutPut} \section{Text Mapping} \KKvScanOff By using \verb|\KKvSetMap|, you can replace a certain text in the argument of \verb|\KKverb| and \verb|\KKcodeS/E| environment. \begin{SourceCode}{Input}{TeX}[Actual Use] \KKvSetMap{!}{〈EXCLAMATION〉} \KKvSetMap{?}{〈QUESTION〉} \KKverb|Wait, you really change them!?| \KKvSetMap{!}{!} \KKvSetMap{?}{?} \KKverb|Oh, how can I reset them!?| \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} \KKvSetMap{!}{〈EXCLAMATION〉} \KKvSetMap{?}{〈QUESTION〉} \KKverb|Wait, you really change them!?| \KKvSetMap{!}{!} \KKvSetMap{?}{?} \KKverb|Oh, how can I reset them!?| \end{OutPut} \KKvScanOff Using this, you can visualize all spaces in the argument of \verb|\KKverb| and \verb|\KKcodeS/E| environment. \KKvScanOn \namiKK{However, you have to use \KKverb|\KKvSpaceForce| if you want to reset.} By default, this package automatically replaces any spaces to Unicode character U+00A0 (Non-breaking Space). Therefore, you need to use the special command if you want to quit visualizing spaces. \KKvScanOff \begin{SourceCode}{Input}{TeX}[Actual Use] \KKvSetMap{ }{} \KKverb| Look! All spaces are visualized!! | \KKvSpaceForce \KKverb| Oh, spaces are no longer visualized. | \end{SourceCode} \KKvScanOn \begin{OutPut}{Output} \KKvSetMap{ }{} \KKverb| Look! All spaces are visualized!! | \KKvSpaceForce \KKverb| Oh, spaces are no longer visualized. | \end{OutPut} \end{document}