Debian Codegraph @

texmf-dist/source/latex/fvextra/fvextra.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2016-2026 by Geoffrey M. Poore <[email protected]>
4 % ---------------------------------------------------------------------------
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3 or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Geoffrey M. Poore.
16 %
17 % This work consists of the files fvextra.dtx and fvextra.ins
18 % and the derived filebase fvextra.sty.
19 %
20 % \fi
21 %
22 % \iffalse
23 %<*driver>
24 \ProvidesFile{fvextra.dtx}
25 %</driver>
26 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
27 %<package>\ProvidesPackage{fvextra}
28 %<*package>
29 [2026/02/25 v1.14.0 fvextra - extensions and patches for fancyvrb]
30 %</package>
31 %
32 %<*driver>
33 \documentclass{ltxdoc}
34
35 \makeatletter
36
37 \usepackage[T1]{fontenc}
38 \usepackage[utf8]{inputenc}
39 \usepackage{lmodern}
40 \usepackage{microtype}
41
42 \usepackage[svgnames]{xcolor}
43 \usepackage{upquote}
44 % The typesetting for macrocode doesn't use \@noligs, which upquote modifies.
45 % So apply the upquote fix to \verbatim@nolig@list as well, which is in macrocode.
46 \begingroup
47 \catcode`'=\active
48 \catcode``=\active
49 \g@addto@macro\verbatim@nolig@list{%
50 \let'\textquotesingle
51 \let`\textasciigrave
52 \ifx\encodingdefault\upquote@OTone
53 \ifx\ttdefault\upquote@cmtt
54 \def'{\char13 }%
55 \def`{\char18 }%
56 \fi\fi}
57 \endgroup
58 \usepackage{fvextra}
59
60 \usepackage{dingbat}
61 \usepackage{graphicx}
62
63 \usepackage{amsmath, amssymb}
64
65 \usepackage{environ}
66
67 \usepackage[many]{tcolorbox}
68 \tcbuselibrary{listings}
69 \tcbset{verbatim ignore percent}
70
71
72 \usepackage{hyperref}
73 \hypersetup{
74 pdftitle=The fvextra package: extensions and patches for fancyvrb,
75 pdfauthor=Geoffrey M. Poore,
76 pdfsubject={fvextra LaTeX package manual},
77 colorlinks=true,
78 allcolors=ForestGreen,
79 }
80 \usepackage{cleveref}
81
82 % A more robust \cmd
83 \let\cmd\Verb
84
85 % Create a short verbatim pipe that handles quotation marks properly
86 \begingroup
87 \catcode`\|=\active
88 \gdef\pipe@active@verbatim{%
89 \begingroup
90 \let\do\@makeother\dospecials
91 \catcode`\|=\active
92 \catcode`\`=\active
93 \catcode`\'=\active
94 \catcode`\<=\active
95 \catcode`\>=\active
96 \catcode`\-=\active
97 \catcode`\,=\active
98 \catcode`\ =\active
99 \pipe@active@verbatim@i}
100 \gdef\pipe@active@verbatim@i#1|{%
101 \endgroup
102 \begingroup
103 \def\FV@SV@pipe@active@verbatim{%
104 \FV@Gobble
105 \expandafter\FV@ProcessLine\expandafter{#1}}%
106 %\let\FV@BeginVBox\relax
107 %\let\FV@EndVBox\relax
108 %\def\FV@BProcessLine##1{\FancyVerbFormatLine{##1}}%
109 \BUseVerbatim{pipe@active@verbatim}%
110 \endgroup}
111 \AtBeginDocument{\let|\pipe@active@verbatim}
112 \endgroup
113
114 \renewenvironment{verbatim}%
115 {\VerbatimEnvironment\begin{Verbatim}[gobble=1]}%
116 {\end{Verbatim}}
117
118
119 \newcommand{\todo}[1]{}
120 %\newcommand{\todo}[1]{\textcolor{red}{TO~DO: \scantokens{#1}}}
121
122 \newcommand\pkg[1]{\textsf{#1}}
123
124 \def\MacroFont{%
125 \fontencoding\encodingdefault%
126 \fontfamily\ttdefault%
127 \fontseries\mddefault%
128 \fontshape\updefault%
129 \small}
130
131 \def\PrintMacroName#1{{\strut\MacroFont\color{DarkGreen}\footnotesize\string #1\ }}
132
133 \def\PrintDescribeMacro#1{\strut\MacroFont\textcolor{DarkGreen}{\string #1\ }}
134 \let\PrintDescribeEnv\PrintDescribeMacro
135 %\let\PrintMacroName\PrintDescribeMacro
136 \let\PrintEnvName\PrintDescribeEnv
137
138 \def\theCodelineNo{\textcolor{DarkGreen}{\sffamily\scriptsize{\arabic{CodelineNo}}}}
139
140
141 \renewenvironment{macro}{\macro@custom@arg}{}
142 \def\macro@custom@arg{%
143 \begingroup\makeatletter\macro@custom@arg@i}
144 \def\macro@custom@arg@i#1{%
145 \makeatother
146 \par\noindent
147 \ifstrempty{#1}%
148 {~\par}%
149 {\ttfamily\color{DarkGreen}\hspace*{-0.5in}%
150 \macro@custom@arg@split#1,\FV@Sentinel\par}%
151 \endgroup}
152 \def\macro@custom@arg@spacegobble#1{#1}
153 \def\macro@custom@arg@split#1,#2\FV@Sentinel{%
154 \expandafter\string\macro@custom@arg@spacegobble#1%
155 \if\relax\detokenize{#2}\relax
156 \expandafter\@firstoftwo
157 \else
158 \expandafter\@secondoftwo
159 \fi
160 {}{\\\hspace*{-0.5in}\macro@custom@arg@split#2\FV@Sentinel}}
161
162 \renewenvironment{environment}{\env@custom@arg}{}
163 \def\env@custom@arg{%
164 \begingroup\makeatletter\env@custom@arg@i}
165 \def\env@custom@arg@i#1{%
166 \makeatother
167 \ifstrempty{#1}%
168 {~\par}%
169 {\ttfamily\color{DarkGreen}\hspace*{-0.5in}%
170 \env@custom@arg@split#1,\FV@Sentinel\par}%
171 \endgroup}
172 \def\env@custom@arg@spacegobble#1{#1}
173 \def\env@custom@arg@split#1,#2\FV@Sentinel{%
174 \env@custom@arg@spacegobble#1{\textrm{ \textit{(env.)}}}%
175 \if\relax\detokenize{#2}\relax
176 \expandafter\@firstoftwo
177 \else
178 \expandafter\@secondoftwo
179 \fi
180 {}{\\\hspace*{-0.5in}\env@custom@arg@split#2\FV@Sentinel}}
181
182
183 \let\orig@footnote\footnote
184 \renewcommand{\footnote}{%
185 \begingroup
186 \let\do\@makeother
187 \dospecials
188 \catcode`\{=1
189 \catcode`\}=2
190 \new@footnote}
191 \newcommand{\new@footnote}[1]{%
192 \endgroup
193 \orig@footnote{\scantokens{#1}}}
194
195
196 \def\fvextra@optionlist@printopt#1(#2) (#3){%
197 \vspace{0.1in}%
198 \leavevmode%
199 \marginpar{\raggedleft\texttt{\textcolor{DarkGreen}{\small #1}}\ }%
200 \kern-\parindent\textsf{(#2)}\hfill(default: \texttt{#3})\\}
201
202 \newenvironment{optionlist}%
203 {~\par\vspace{-14pt}%
204 \def\pipechar{|}
205 \let\|\pipechar
206 \newcommand*\fvextra@optionlist@next{}%
207 \let\item@orig\item
208 \renewcommand*\item[1][]{%
209 \ifdefstring{\@currenvir}{optionlist}%
210 {\fvextra@optionlist@next%
211 \renewcommand*\fvextra@optionlist@next{\par}%
212 \fvextra@optionlist@printopt##1%
213 \ignorespaces}%
214 {\ifstrempty{##1}%
215 {\vspace{-4pt}\item@orig}
216 {\vspace{-4pt}\item@orig[##1]}}}}
217 {\par}
218
219
220 \CustomVerbatimEnvironment{VerbatimVerbatim}{Verbatim}{}
221 \CustomVerbatimEnvironment{VerbatimVerbatimBuffer}{VerbatimBuffer}{}
222
223 \newenvironment{example}
224 {\VerbatimEnvironment
225 \begin{VerbatimVerbatimBuffer}[
226 buffername=fvexample,
227 gobble=1,
228 afterbuffer={%
229 \vspace{1ex}%
230 \setlength{\parindent}{0pt}%
231 \setlength{\fboxsep}{1em}%
232 \fcolorbox{DarkGreen}{white}{%
233 \begin{minipage}{0.5\linewidth}%
234 \VerbatimInsertBuffer[buffername=fvexample]%
235 \end{minipage}%
236 \hspace{0.025\linewidth}%
237 {\color{DarkGreen}\vrule}%
238 \hspace{0.025\linewidth}%
239 \begin{minipage}{0.4\linewidth}%
240 \InsertBuffer[buffername=fvexample]%
241 \end{minipage}}%
242 \vspace{1ex}%
243 }]}
244 {\end{VerbatimVerbatimBuffer}}
245
246 \newenvironment{longexample}
247 {\VerbatimEnvironment
248 \begin{VerbatimVerbatimBuffer}[
249 buffername=fvexample,
250 gobble=1,
251 afterbuffer={%
252 \vspace{1ex}%
253 \setlength{\parindent}{0pt}%
254 \setlength{\fboxsep}{1em}%
255 \fcolorbox{DarkGreen}{white}{%
256 \begin{minipage}{0.94\linewidth}%
257 \VerbatimInsertBuffer[buffername=fvexample]%
258 {\color{DarkGreen}\hrulefill}%
259 \setlength{\fboxsep}{3pt}%
260 \InsertBuffer[buffername=fvexample]%
261 \end{minipage}}%
262 \vspace{1ex}%
263 }]}
264 {\end{VerbatimVerbatimBuffer}}
265
266 \newenvironment{tcbexample}
267 {\VerbatimEnvironment
268 \begin{VerbatimVerbatimBuffer}[
269 gobble=1,
270 buffername=tcbexample,
271 afterbuffer={
272 \begin{tcolorbox}[oversize=5em]
273 \VerbatimInsertBuffer[buffername=tcbexample]
274 \tcbline
275 \InsertBuffer[buffername=tcbexample]
276 \end{tcolorbox}
277 }]}
278 {\end{VerbatimVerbatimBuffer}}
279
280
281 \edef\hashchar{\string#}
282
283
284 %\EnableCrossrefs
285 %\CodelineIndex
286 %\RecordChanges
287
288 \makeatother
289 \begin{document}
290 \DocInput{fvextra.dtx}
291 %\PrintChanges
292 %\PrintIndex
293 \end{document}
294 %</driver>
295 % \fi
296 %
297 %
298 % \DoNotIndex{\newcommand,\newenvironment}
299 % \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
300 % \DoNotIndex{\@ne}
301 % \DoNotIndex{\advance,\begingroup,\catcode,\closein}
302 % \DoNotIndex{\closeout,\day,\def,\edef,\else,\empty,\endgroup}
303 % \DoNotIndex{\begin,\end,\bgroup,\egroup}
304 %
305 % \providecommand*{\url}{\texttt}
306 % \newcommand{\fvextra}{\pkg{fvextra}}
307 % \newcommand{\fancyvrb}{\pkg{fancyvrb}}
308 % \GetFileInfo{fvextra.dtx}
309 %
310 % \title{\vspace{-0.5in}The \fvextra\ package}
311 % \author{Geoffrey M.\ Poore \\ \href{mailto://[email protected]}{\texttt{[email protected]}} \\ \href{https://github.com/gpoore/fvextra}{\texttt{github.com/gpoore/fvextra}}}
312 % \date{\fileversion~from \filedate}
313 %
314 % \maketitle
315 %
316 % \begin{abstract}
317 % \noindent\fvextra\ provides several extensions to \fancyvrb, including automatic line breaking and improved math mode. \cmd{\Verb} is reimplemented so that it works (with a few limitations) inside other commands, even in movable arguments and PDF bookmarks. The new command \cmd{\EscVerb} is similar to \cmd{\Verb} except that it works everywhere without limitations by allowing the backslash to serve as an escape character. \fvextra\ also patches some \fancyvrb\ internals.
318 % \end{abstract}
319 %
320 %
321 % \pagebreak
322 % \begingroup
323 % \makeatletter
324 % ^^A https://tex.stackexchange.com/a/45165/10742
325 % \patchcmd{\@dottedtocline}
326 % {\rightskip\@tocrmarg}
327 % {\rightskip\@tocrmarg plus 4em \hyphenpenalty\@M}
328 % {}{}
329 % \makeatother
330 % \tableofcontents
331 % \endgroup
332 % \pagebreak
333 %
334 %
335 % \section{Introduction}
336 %
337 % The \fancyvrb\ package had its first public release in January 1998. In July of the same year, a few additional features were added. Since then, the package has remained almost unchanged except for a few bug fixes. \fancyvrb\ has become one of the primary \LaTeX\ packages for working with verbatim text.
338 %
339 % Additional verbatim features would be nice, but since \fancyvrb\ has remained almost unchanged for so long, a major upgrade could be problematic. There are likely many existing documents that tweak or patch \fancyvrb\ internals in a way that relies on the existing implementation. At the same time, creating a completely new verbatim package would require a major time investment and duplicate much of \fancyvrb\ that remains perfectly functional. Perhaps someday there will be an amazing new verbatim package. Until then, we have \fvextra.
340 %
341 % \fvextra\ is an add-on package that gives \fancyvrb\ several additional features, including automatic line breaking. Because \fvextra\ patches and overwrites some of the \fancyvrb\ internals, it may not be suitable for documents that rely on the details of the original \fancyvrb\ implementation. \fvextra\ tries to maintain the default \fancyvrb\ behavior in most cases. All reimplementations (\cref{sec:reimplemented-cmd}), patches (\cref{sec:patch}), and modifications to \fancyvrb\ defaults (\cref{sec:modifications}) are documented. In most cases, there are options to switch back to original implementations or original default behavior.
342 %
343 % Some features of \fvextra\ were originally created as part of the \pkg{pythontex} and \pkg{minted} packages. \fancyvrb-related patches and extensions that currently exist in those packages will gradually be migrated into \fvextra.
344 %
345 %
346 %
347 %
348 % \section{Usage}
349 %
350 % \fvextra\ may be used as a drop-in replacement for \fancyvrb. It will load \fancyvrb\ if it has not yet been loaded, and then proceeds to patch \fancyvrb\ and define additional features.
351 %
352 % The \pkg{upquote} package is loaded to give correct backticks (\texttt{\textasciigrave}) and typewriter single quotation marks (\texttt{\textquotesingle}). When this is not desirable within a given environment, use the option |curlyquotes|. \fvextra\ modifies the behavior of these and other symbols in typeset math within verbatim, so that they will behave as expected (\cref{sec:patch:math}). \fvextra\ uses the \pkg{lineno} package for working with automatic line breaks. \pkg{lineno} gives a warning when the \pkg{csquotes} package is loaded before it, so \fvextra\ should be loaded before \pkg{csquotes}. The \pkg{etoolbox} package is required. \pkg{color} or \pkg{xcolor} should be loaded manually to use color-dependent features.
353 %
354 % While \fvextra\ attempts to minimize changes to the \fancyvrb\ internals, in some cases it completely overwrites \fancyvrb\ macros with new definitions. New definitions typically follow the original definitions as much as possible, but code that depends on the details of the original \fancyvrb\ implementation may be incompatible with \fvextra.
355 %
356 %
357 % \subsection{Pandoc compatibility}
358 %
359 % \pkg{fvextra} supports line breaking in \href{https://pandoc.org/}{Pandoc} \LaTeX\ output that includes highlighted source code. Enabling basic line breaking at spaces is as simple as adding |\usepackage{fvextra}| and |\fvset{breaklines}| to the Pandoc Markdown |header-includes|.
360 %
361 % By default, more advanced line breaking features such as |breakanywhere|, |breakbefore|, and |breakafter| will not work with Pandoc highlighted output, due to the presence of the syntax highlighting macros. This can be fixed by using |breaknonspaceingroup|, which enables all line breaking features within macros. For example, the following YAML metadata in a Markdown document would redefine the Pandoc |Highlighting| environment to enable line breaking anywhere.
362 %
363 %\vspace{2ex}
364 %\hrule
365 %\vspace{2ex}
366 %\noindent\begin{minipage}{\linewidth}
367 %\begin{verbatim}
368 %---
369 %header-includes:
370 % - |
371 % ```{=latex}
372 % \usepackage{fvextra}
373 % \DefineVerbatimEnvironment{Highlighting}{Verbatim}{
374 % commandchars=\\\{\},
375 % breaklines, breaknonspaceingroup, breakanywhere}
376 % ```
377 %---
378 %\end{verbatim}
379 %\end{minipage}
380 %\vspace{2ex}
381 %\hrule
382 %\vspace{2ex}
383 %
384 %
385 %
386 %
387 % \section{General options}
388 % \label{sec:general-options}
389 %
390 % \fvextra\ adds several general options to \fancyvrb. All options related to automatic line breaking are described separately in \cref{sec:breaklines}. All options related to syntax highlighting using Pygments are described in \cref{sec:pygments}.
391 %
392 % \begin{optionlist}
393 %
394 % \item[backgroundcolor (string) (none)]
395 % Background color behind commands and environments. This is only a basic, lightweight implementation of background colors using |\colorbox|. For more control of background colors, consider \href{https://ctan.org/pkg/tcolorbox}{\pkg{tcolorbox}} or a similar package, or a custom background color implementation.
396 %
397 % Background colors are implemented with |\colorbox|, which prevents line breaking for |\Verb|, |\UseVerb|, and |\EscVerb|. Background colors are only compatible with \fvextra's reimplemented |\Verb| and |\UseVerb| (|extra=true|).
398 %
399 % When there is a background color in environments such as |Verbatim| that typeset blocks of text, some PDF readers and browsers can introduce rendering artifacts. These appear in between lines of text as extremely thin horizontal rules that are the color of the page behind the background. They tend to be more noticeable for dark background colors on a light page. \fvextra\ attempts to prevent rendering artifacts by slightly oversizing the |\colorbox| behind each line and then slightly overlapping these boxes. This overlap can be fine-tuned if necessary with |backgroundcolorboxoverlap|. If rendering artifacts are an issue with your software in spite of a small overlap, consider \href{https://ctan.org/pkg/tcolorbox}{\pkg{tcolorbox}} or a similar package, or a custom background color implementation.
400 %
401 % See |backgroundcolorvphantom| to customize the height of the background for each line of text, especially for |\Verb|, |\EscVerb|, and |BVerbatim|.
402 %
403 % When |backgroundcolor| is used with |Verbatim| with a |frame|, it may be necessary to adjust |framesep| and |fillcolor| depending on the desired result. |backgroundcolorpadding| provides a shortcut for this.
404 %
405 %
406 % \item[backgroundcolorboxoverlap (length) (0.25pt)]
407 % The amount by which |\colorbox|es that are used to provide |backgroundcolor| are oversized and then overlapped, to prevent rendering artifacts.
408 %
409 % When there is a background color in environments such as |Verbatim| that typeset blocks of text, some PDF readers and browsers can introduce rendering artifacts. These appear in between lines of text as extremely thin horizontal rules that are the color of the page behind the background. They tend to be more noticeable for dark background colors on a light page. \fvextra\ attempts to prevent rendering artifacts by slightly oversizing the |\colorbox| behind each line by |backgroundcolorboxoverlap|, and then slightly overlapping these boxes by the same amount.
410 %
411 % Chrome and Adobe Acrobat Reader generally work correctly even with no overlap (|backgroundcolorboxoverlap=0pt|). Many PDF previews in \TeX\ editing software and PDF readers need an overlap in the |0.1pt| to |0.15pt| range. Whether Firefox needs overlap, and if so the amount needed, seems to depend on resolution and possibly operating system. Safari and macOS Preview may show artifacts, particularly when zoomed out, even with overlaps of |0.25pt| or greater.
412 %
413 %
414 % \item[backgroundcolorpadding (length) (none)]
415 % Padding when |backgroundcolor| is set. For |\Verb|, |\UseVerb|, |\EscVerb|, and |BVerbatim|, this sets |\fboxsep| for the |\colorbox| that is used to create the background color. For |Verbatim|, \fancyvrb's frame options are used instead, particularly |framesep| and |fillcolor|.
416 %
417 % For |Verbatim|, this amounts to a shortcut for a combination of frame-related settings that provide padding when there is a background color. This is only intended for cases with |frame=none| or |frame=single|. In other cases, it may be better to modify the \fancyvrb\ frame settings directly, and it may be necessary to modify the \fancyvrb\ internals to obtain padding on all sides.
418 %
419 % For |Verbatim|, if |frame=single|, then this is equivalent to the following settings:
420 % \begin{itemize}
421 % \item |framesep=|\meta{\texttt{backgroundcolorpadding}}
422 % \item |fillcolor=\FancyVerbBackgroundColor|
423 % \end{itemize}
424 %
425 % For |Verbatim|, if |frame| has any value other than |single|, then this is equivalent to the following settings:
426 % \begin{itemize}
427 % \item |frame=single|
428 % \item |framerule=0pt|
429 % \item |rulecolor=\FancyVerbBackgroundColor|
430 % \item |framesep=|\meta{\texttt{backgroundcolorpadding}}
431 % \item |fillcolor=\FancyVerbBackgroundColor|
432 % \end{itemize}
433 %
434 %
435 % \item[backgroundcolorvphantom (macro) (\Verb{\vphantom{\"Apgjy}})]
436 % |\vphantom| or similar macro such as |\strut| that is inserted at the beginning of each line of text using |backgroundcolor|. This allows the height of the background for each line of text to be customized, especially for |\Verb|, |\EscVerb|, and |BVerbatim|. |backgroundcolorvphantom| will typically have no effect on |Verbatim|-based environments unless it is set to a size larger than |\strut|.
437 %
438 % \item[bgcolor (string) (none)]
439 % Alias for |backgroundcolor|.
440 %
441 % \item[bgcolorboxoverlap (length) (0.25pt)]
442 % Alias for |backgroundcolorboxoverlap|.
443 %
444 % \item[bgcolorpadding (length) (none)]
445 % Alias for |backgroundcolorpadding|.
446 %
447 % \item[bgcolorvphantom (macro) (\Verb{\vphantom{\"Apgjy}})]
448 % Alias for |backgroundcolorvphantom|.
449 %
450 %
451 % \item[beameroverlays (boolean) (false)]
452 % Give the |<| and |>| characters their normal text meanings, so that \pkg{beamer} overlays of the form |\only<1>{...}| will work. Note that something like |commandchars=\\\{\}| is required separately to enable macros. This is not incorporated in the |beameroverlays| option because essentially arbitrary command characters could be used; only the |<| and |>| characters are hard-coded for overlays.
453 %
454 % With some font encodings and language settings, |beameroverlays| prevents literal (non-overlay) |<| and |>| characters from appearing correctly, so they must be inserted using commands.
455 %
456 %
457 % \item[curlyquotes (boolean) (false)]
458 % Unlike \fancyvrb, \pkg{fvextra} requires the \pkg{upquote} package, so the backtick (\texttt{\textasciigrave}) and typewriter single quotation mark (\texttt{\textquotesingle}) always appear literally by default, instead of becoming the left and right curly single quotation marks (\texttt{`'}). This option allows these characters to be replaced by the curly quotation marks when that is desirable.
459 %
460 %\begin{example}
461 %\begin{Verbatim}
462 %`quoted text'
463 %\end{Verbatim}
464 %\end{example}
465 %
466 %\begin{example}
467 %\begin{Verbatim}[curlyquotes]
468 %`quoted text'
469 %\end{Verbatim}
470 %\end{example}
471 %
472 %
473 % \item[extra (boolean) (true)]
474 % Use \fvextra\ reimplementations of \fancyvrb\ commands and environments when available. For example, use \fvextra's reimplemented \cmd{\Verb} that works (with a few limitations) inside other commands, rather than the original \fancyvrb\ implementation that essentially functions as \cmd{\texttt} inside other commands.
475 %
476 %
477 % \item[fontencoding (string) (\meta{document font encoding})]
478 % Set the font encoding inside \pkg{fancyvrb} commands and environments. Setting |fontencoding=none| resets to the default document font encoding.
479 %
480 %
481 % \item[highlightcolor (string) (LightCyan)]
482 % Set the color used for |highlightlines|, using a predefined color name from \pkg{color} or \pkg{xcolor}, or a color defined via |\definecolor|.
483 %
484 %
485 % \item[highlightlines (string) (\meta{none})]
486 % This highlights a single line or a range of lines based on line numbers. The line numbers refer to the line numbers that \fancyvrb\ would show if |numbers=left|, etc. They do not refer to original or actual line numbers before adjustment by |firstnumber|.
487 %
488 % The highlighting color can be customized with |highlightcolor|.
489 %
490 % \begingroup
491 % \fvset{xleftmargin=2em}
492 %\begin{longexample}
493 %\begin{Verbatim}[numbers=left, highlightlines={1, 3-4}]
494 %First line
495 %Second line
496 %Third line
497 %Fourth line
498 %Fifth line
499 %\end{Verbatim}
500 %\end{longexample}
501 % \endgroup
502 %
503 % The actual highlighting is performed by a set of commands. These may be customized for additional fine-tuning of highlighting. See the default definition of |\FancyVerbHighlightLineFirst| as a starting point.
504 %
505 % \vspace{0.1in}
506 % $\bullet$ |\FancyVerbHighlightLineFirst|: First line in a range. \\
507 % \indent $\bullet$ |\FancyVerbHighlightLineMiddle|: Inner lines in a range. \\
508 % \indent $\bullet$ |\FancyVerbHighlightLineLast|: Last line in a range. \\
509 % \indent $\bullet$ |\FancyVerbHighlightLineSingle|: Single highlighted lines. \\
510 % \indent $\bullet$ |\FancyVerbHighlightLineNormal|: Normal lines without highlighting. \\
511 %
512 % \vspace{-0.1in}
513 % \noindent If these are customized in such a way that indentation or inter-line spacing is changed, then |\FancyVerbHighlightLineNormal| may be modified as well to make all lines uniform. When working with the |First|, |Last|, and |Single| commands, keep in mind that \fvextra\ merges all numbers ranges, so that |{1, 2-3, 3-5}| is treated the same as |{1-5}|.
514 %
515 % Highlighting is applied after |\FancyVerbFormatText|, so any text formatting defined via that command will work with highlighting. Highlighting is applied before |\FancyVerbFormatLine|, so if |\FancyVerbFormatLine| puts a line in a box, the box will be behind whatever is created by highlighting. This prevents highlighting from vanishing due to user-defined customization.
516 %
517 %
518 % \item[linenos (boolean) (false)]
519 % \fancyvrb\ allows line numbers via the options |numbers=|\meta{position}. This is essentially an alias for |numbers=left|. It primarily exists for better compatibility with the \pkg{minted} package.
520 %
521 %
522 % \item[mathescape (boolean) (false)]
523 % This causes everything between dollar signs |$...$| to be typeset as math. The ampersand |&|, caret |^|, and underscore |_| have their normal math meanings.
524 %
525 % This is equivalent to
526 % \begin{Verbatim}[gobble=2]
527 % codes={\catcode`\$=3\catcode`\&=4\catcode`\^=7\catcode`\_=8}
528 % \end{Verbatim}
529 % |mathescape| is always applied \emph{before} |codes|, so that |codes| can be used to override some of these definitions.
530 %
531 % Note that \fvextra\ provides several patches that make math mode within verbatim as close to normal math mode as possible (\cref{sec:patch:math}).
532 %
533 %
534 % \item[numberfirstline (boolean) (false)]
535 % When line numbering is used with |stepnumber| $\ne 1$, the first line may not always be numbered, depending on the line number of the first line. This causes the first line always to be numbered.
536 %
537 % \begingroup
538 % \fvset{xleftmargin=2em}
539 %\begin{longexample}
540 %\begin{Verbatim}[numbers=left, stepnumber=2,
541 % numberfirstline]
542 %First line
543 %Second line
544 %Third line
545 %Fourth line
546 %\end{Verbatim}
547 %\end{longexample}
548 % \endgroup
549 %
550 %
551 % \item[numbers (none \| left \| right \| both) (none)]
552 % \fvextra\ adds the |both| option for line numbering.
553 %
554 % \begingroup
555 % \fvset{xleftmargin=1.5em, xrightmargin=1.5em}
556 %\begin{example}
557 %\begin{Verbatim}[numbers=both]
558 %First line
559 %Second line
560 %Third line
561 %Fourth line
562 %\end{Verbatim}
563 %\end{example}
564 % \endgroup
565 %
566 %
567 % \item[retokenize (boolean) (false)]
568 % By default, \cmd{\UseVerb} inserts saved verbatim material with the catcodes (|commandchars|, |codes|, etc.\@) under which it was originally saved with \cmd{\SaveVerb}. When |retokenize| is used, the saved verbatim material is retokenized under the settings in place at \cmd{\UseVerb}.
569 %
570 % This only applies to the reimplemented \cmd{\UseVerb}, when paired with the reimplemented \cmd{\SaveVerb}. It may be extended to environments (\cmd{\UseVerbatim}, etc.\@) in the future, if the relevant commands and environments are reimplemented.
571 %
572 %
573 % \item[space (macro) ({\ttfamily\FancyVerbSpace})]
574 % Redefine the visible space character. Note that this is only used if |showspaces=true|. The color of the character may be set with |spacecolor|.
575 %
576 %
577 % \item[spacebreak (macro) (\Verb{\discretionary{}{}{}})]
578 % This determines the break that is inserted around spaces when |breaklines=true| and one or more of the following conditions applies: |breakcollapsespaces=false|, |showspaces=true|, or the space is affected by |breakbefore| or |breakafter|. If it is redefined, it should typically be similar to |\FancyVerbBreakAnywhereBreak|, |\FancyVerbBreakBeforeBreak|, and |\FancyVerbBreakAfterBreak| to obtain consistent breaks.
579 %
580 %
581 % \item[spacecolor (string) (none)]
582 % Set the color of visible spaces. By default (|none|), they take the color of their surroundings.
583 %
584 %\begin{longexample}
585 %\color{gray}
586 %\begin{Verbatim}[showspaces, spacecolor=red]
587 %One two three
588 %\end{Verbatim}
589 %\end{longexample}
590 %
591 %
592 % \item[stepnumberfromfirst (boolean) (false)]
593 % By default, when line numbering is used with |stepnumber| $\ne 1$, only line numbers that are a multiple of |stepnumber| are included. This offsets the line numbering from the first line, so that the first line, and all lines separated from it by a multiple of |stepnumber|, are numbered.
594 %
595 % \begingroup
596 % \fvset{xleftmargin=2em}
597 %\begin{longexample}
598 %\begin{Verbatim}[numbers=left, stepnumber=2,
599 % stepnumberfromfirst]
600 %First line
601 %Second line
602 %Third line
603 %Fourth line
604 %\end{Verbatim}
605 %\end{longexample}
606 % \endgroup
607 %
608 %
609 % \item[stepnumberoffsetvalues (boolean) (false)]
610 % By default, when line numbering is used with |stepnumber| $\ne 1$, only line numbers that are a multiple of |stepnumber| are included. Using |firstnumber| to offset the numbering will change which lines are numbered and which line gets which number, but will not change which \emph{numbers} appear. This option causes |firstnumber| to be ignored in determining which line numbers are a multiple of |stepnumber|. |firstnumber| is still used in calculating the actual numbers that appear. As a result, the line numbers that appear will be a multiple of |stepnumber|, plus |firstnumber| minus 1.
611 %
612 % This option gives the original behavior of \fancyvrb\ when |firstnumber| is used with |stepnumber| $\ne 1$ (\cref{sec:modifications:line-numbering}).
613 %
614 % \begingroup
615 % \fvset{xleftmargin=2em}
616 %\begin{longexample}
617 %\begin{Verbatim}[numbers=left, stepnumber=2,
618 % firstnumber=4, stepnumberoffsetvalues]
619 %First line
620 %Second line
621 %Third line
622 %Fourth line
623 %\end{Verbatim}
624 %\end{longexample}
625 % \endgroup
626 %
627 %
628 % \item[tab (macro) ({\rmfamily\fancyvrb's} \string\FancyVerbTab, \FancyVerbTab)]
629 % Redefine the visible tab character. Note that this is only used if |showtabs=true|. The color of the character may be set with |tabcolor|.
630 %
631 % When redefining the tab, you should include the font family, font shape, and text color in the definition. Otherwise these may be inherited from the surrounding text. This is particularly important when using the tab with syntax highlighting, such as with the \pkg{minted} or \pkg{pythontex} packages.
632 %
633 % \fvextra\ patches \fancyvrb\ tab expansion so that variable-width symbols such as |\rightarrowfill| may be used as tabs. For example,
634 %
635 % \begingroup
636 % \fvset{frame=single, rulecolor=DarkGreen, gobble=4}
637 % \begin{VerbatimVerbatim}[breaklines, obeytabs, showtabs]
638 % \begin{Verbatim}[obeytabs, showtabs, breaklines,
639 % tab=\rightarrowfill, tabcolor=orange]
640 % First Second Third And more text that goes on for a while until wrapping is needed
641 % First Second Third Forth
642 % \end{Verbatim}
643 % \end{VerbatimVerbatim}
644 % \begin{Verbatim}[obeytabs, showtabs, breaklines,
645 % tab=\rightarrowfill, tabcolor=orange]
646 % First Second Third And more text that goes on for a while until wrapping is needed
647 % First Second Third Forth
648 % \end{Verbatim}
649 % \endgroup
650 %
651 %
652 % \item[tabcolor (string) (none)]
653 % Set the color of visible tabs. By default (|none|), they take the color of their surroundings.
654 %
655 %
656 % \item[vargsingleline (boolean) (false)]
657 % This determines whether \fvextra's |\Verb| and |\SaveVerb| take multi-line (but not multi-paragraph) verbatim arguments, or if they instead require arguments to be on a single line like the original \fancyvrb\ commands.
658 %
659 %
660 % \end{optionlist}
661 %
662 %
663 %
664 %
665 % \section{General commands}
666 %
667 % \subsection{Inline-only settings with \cmd{\fvinlineset}}
668 % \DescribeMacro{\fvinlineset\marg{options}}
669 %
670 % This is like \cmd{\fvset}, except that options only apply to commands that typeset inline verbatim, like \cmd{\Verb} and \cmd{\EscVerb}. Settings from \cmd{\fvinlineset} override those from \cmd{\fvset}.
671 %
672 % Note that \cmd{\fvinlineset} only works with commands that are reimplemented, patched, or defined by \fvextra; it is not compatible with the original \fancyvrb\ definitions.
673 %
674 %
675 % \subsection{Custom formatting for inline commands like \cmd{\Verb} with \cmd{\FancyVerbFormatInline}}
676 %
677 % \DescribeMacro{\FancyVerbFormatInline}
678 %
679 % This can be used to apply custom formatting to inline verbatim text created with commands like \cmd{\Verb}. It only works with commands that are reimplemented, patched, or defined by \fvextra; it is not compatible with the original \fancyvrb\ definitions. The default definition does nothing; it is equivalent to |\newcommand{\FancyVerbFormatInline}[1]{#1}|.
680 %
681 % This is the inline equivalent of \cmd{\FancyVerbFormatLine} and \cmd{\FancyVerbFormatText}. In the inline context, there is no need to distinguish between entire line formatting and only text formatting, so only \cmd{\FancyVerbFormatInline} exists.
682 %
683 %
684 % \subsection{Custom formatting for environments like \cmd{Verbatim} with \cmd{\FancyVerbFormatLine} and \cmd{\FancyVerbFormatText}}
685 %
686 % \DescribeMacro{\FancyVerbFormatLine}
687 % \DescribeMacro{\FancyVerbFormatText}
688 %
689 % \fancyvrb\ defines |\FancyVerbFormatLine|, which can be used to apply custom formatting to each individual line of text in environments like \cmd{Verbatim}. By default, it takes a line as an argument and inserts it with no modification. This is equivalent to |\newcommand{\FancyVerbFormatLine}[1]{#1}|.\footnote{The actual definition in \fancyvrb\ is |\def\FancyVerbFormatLine#1{\FV@ObeyTabs{#1}}|. This is problematic because redefining the macro could easily eliminate |\FV@ObeyTabs|, which governs tab expansion. \fvextra\ redefines the macro to |\def\FancyVerbFormatLine#1{#1}| and patches all parts of \fancyvrb\ that use |\FancyVerbFormatLine| so that |\FV@ObeyTabs| is explicitly inserted at the appropriate points.}
690 %
691 % \fvextra\ introduces line breaking, which complicates line formatting. We might want to apply formatting to the entire line, including line breaks, line continuation symbols, and all indentation, including any extra indentation provided by line breaking. Or we might want to apply formatting only to the actual text of the line. \fvextra\ leaves |\FancyVerbFormatLine| as applying to the entire line, and introduces a new command |\FancyVerbFormatText| that only applies to the text part of the line.\footnote{When |breaklines=true|, each line is wrapped in a |\parbox|. |\FancyVerbFormatLine| is outside the |\parbox|, and |\FancyVerbFormatText| is inside.} By default, |\FancyVerbFormatText| inserts the text unmodified. When it is customized, it should not use boxes that do not allow line breaks to avoid conflicts with line breaking code.
692 %
693 % \begingroup
694 % \fvset{xrightmargin=2em}
695 %\begin{longexample}
696 %\renewcommand{\FancyVerbFormatLine}[1]{%
697 % \fcolorbox{DarkBlue}{LightGray}{#1}}
698 %\renewcommand{\FancyVerbFormatText}[1]{\textcolor{Green}{#1}}
699 %
700 %\begin{Verbatim}[breaklines]
701 %Some text that proceeds for a while and finally wraps onto another line
702 %Some more text
703 %\end{Verbatim}
704 %\end{longexample}
705 % \endgroup
706 %
707 %
708 %
709 %
710 % \section{Reimplemented commands}
711 % \label{sec:reimplemented-cmd}
712 %
713 % \fvextra\ reimplements parts of \fancyvrb. These new implementations stay close to the original definitions while allowing for new features that otherwise would not be possible. Reimplemented versions are used by default. The original implementations may be used via |\fvset{extra=false}| or by using |extra=false| in the optional arguments to a command or environment.
714 %
715 % Reimplemented commands restrict the scope of catcode-related options compared to the original \fancyvrb\ versions. This prevents catcode-related options from interfering with new features such as |\FancyVerbFormatInline|. With \fvextra, the |codes| option should only be used for catcode modifications. Including non-catcode commands in |codes| will typically have no effect, unlike with \fancyvrb. If you want to customize verbatim content using general commands, consider |formatcom|.
716 %
717 % \subsection{\cmd{\Verb}}
718 % \DescribeMacro{\Verb*\oarg{options}\meta{delim~char~or~\{}\meta{text}\meta{delim~char~or~\}}}
719 %
720 % The new \cmd{\Verb} works as expected (with a few limitations) inside other commands. It even works in movable arguments (for example, in \cmd{\section}), and is compatible with \pkg{hyperref} for generating PDF strings (for example, PDF bookmarks). The \fancyvrb\ definition did work inside some other commands, but essentially functioned as \cmd{\texttt} in that context.
721 %
722 % By default, \cmd{\Verb} takes a multi-line (but not multi-paragraph) verbatim argument. To restore the \fancyvrb\ behavior of requiring a single-line argument, set |vargsingleline=true|.
723 %
724 % \cmd{\Verb} is compatible with |breaklines| and the relevant line-breaking options.
725 %
726 % Like the original \fancyvrb\ implementation, the new \cmd{\Verb} can be starred (|\Verb*|) and accepts optional arguments. While \fancyvrb's starred command |\Verb*| is a shortcut for |showspaces|, \fvextra's |\Verb*| is a shortcut for both |showspaces| and |showtabs|. This is more similar to the current behavior of \LaTeX's \Verb{\verb*}, except that \Verb{\verb*} converts tabs into visible spaces instead of displaying them as visible tabs.
727 %
728 % \begin{description}
729 % \item[Delimiters] A repeated character like normal \cmd{\verb}, or a pair of curly braces |{...}|. If curly braces are used, then \meta{text} cannot contain unpaired curly braces. Note that curly braces should be preferred when using \cmd{\Verb} inside other commands, and curly braces are \emph{required} when \cmd{\Verb} is in a movable argument, such as in a \cmd{\section}. Non-ASCII characters now work as delimiters under pdfTeX with \pkg{inputenc} using UTF-8.\footnote{Under pdfTeX, non-ASCII code points are processed at the byte rather than code point level, so \cmd{\Verb} must treat a sequence of multiple bytes as the delimiter.} For example, \Verb{\Verb§verb§} now works as expected.
730 % \item[Limitations inside other commands] While the new \cmd{\Verb} does work inside arbitrary other commands, there are a few limitations.
731 % \begin{itemize}
732 % \item |#| and |%| cannot be used. If you need them, consider \cmd{\EscVerb} or perhaps \cmd{\SaveVerb} plus \cmd{\UseVerb}.
733 % \item Curly braces are only allowed in pairs.
734 % \item Multiple adjacent spaces will be collapsed into a single space.
735 % \item Be careful with backslashes. A backslash that is followed by one or more ASCII letters will cause a following space to be lost, if the space is not immediately followed by an ASCII letter. For example, |\Verb{\r \n}| becomes \texttt{\Verb{\r \n}}, but |\Verb{\r n}| becomes \texttt{\Verb{\r n}}. Basically, anything that looks like a \LaTeX\ command (control word) will gobble following spaces, unless the next character after the spaces is an ASCII letter.
736 % \item A single |^| is fine, but avoid |^^| because it will serve as an escape sequence for an ASCII command character.
737 % \end{itemize}
738 % \item[Using in movable arguments] \cmd{\Verb} works automatically in movable arguments, such as in a \cmd{\section}. \cmd{\protect} or similar measures are not needed for \cmd{\Verb} itself, or for any of its arguments, and should not be used. \cmd{\Verb} performs operations that amount to applying \cmd{\protect} to all of these automatically.
739 % \item[\pkg{hyperref} PDF strings] \cmd{\Verb} is compatible with \pkg{hyperref} for generating PDF strings such as PDF bookmarks. Note that the PDF strings are \emph{always} a literal rendering of the verbatim text, with all \fancyvrb\ options ignored. For example, things like |showspaces| and |commandchars| have no effect. If you need options to be applied to obtain desired PDF strings, consider a custom approach, perhaps using \cmd{\texorpdfstring}.
740 % \item[Line breaking] |breaklines| allows breaks at spaces. |breakbefore|, |breakafter|, and |breakanywhere| function as expected, as do things like |breakaftersymbolpre| and |breakaftersymbolpost|. Break options that are only applicable to block text like a |Verbatim| environment do not have any effect. For example, |breakindent| and |breaksymbol| do nothing.
741 % \end{description}
742 %
743 %
744 % \subsection{\cmd{\SaveVerb}}
745 %
746 % \DescribeMacro{\SaveVerb\oarg{options}\marg{name}\meta{delim~char~or~\{}\meta{text}\meta{delim~char~or~\}}}
747 %
748 % \cmd{\SaveVerb} is reimplemented so that it is equivalent to the reimplemented \cmd{\Verb}. Like the new \cmd{\Verb}, it accepts \meta{text} delimited by a pair of curly braces |{...}|. It supports \cmd{\fvinlineset}. It also adds support for the new |retokenize| option for \cmd{\UseVerb}.
749 %
750 % By default, \cmd{\SaveVerb} takes a multi-line (but not multi-paragraph) verbatim argument. To restore the \fancyvrb\ behavior of requiring a single-line argument, set |vargsingleline=true|.
751 %
752 %
753 % \subsection{\cmd{\UseVerb}}
754 %
755 % \DescribeMacro{\UseVerb*\oarg{options}\marg{name}}
756 %
757 % \cmd{\UseVerb} is reimplemented so that it is equivalent to the reimplemented \cmd{\Verb}. It supports \cmd{\fvinlineset} and |breaklines|.
758 %
759 % Like \cmd{\Verb}, \cmd{\UseVerb} is compatible with \pkg{hyperref} for generating PDF strings such as PDF bookmarks. Note that the PDF strings are \emph{always} a literal rendering of the verbatim text, with all \fancyvrb\ options ignored. For example, things like |showspaces| and |commandchars| have no effect. The new option |retokenize| also has no effect. If you need options to be applied to obtain desired PDF strings, consider a custom approach, perhaps using \cmd{\texorpdfstring}
760 %
761 % There is a new option |retokenize| for \cmd{\UseVerb}. By default, \cmd{\UseVerb} inserts saved verbatim material with the catcodes (|commandchars|, |codes|, etc.\@) under which it was originally saved with \cmd{\SaveVerb}. When |retokenize| is used, the saved verbatim material is retokenized under the settings in place at \cmd{\UseVerb}.
762 %
763 % For example, consider |\SaveVerb{save}{\textcolor{red}{#%}}|:
764 % \SaveVerb{save}{\textcolor{red}{#%}}
765 % \begin{itemize}
766 % \item |\UseVerb{save}| $\Rightarrow$ \UseVerb{save}
767 % \item |\UseVerb[commandchars=\\\{\}]{save}| $\Rightarrow$ \UseVerb[commandchars=\\\{\}]{save}
768 % \item |\UseVerb[retokenize, commandchars=\\\{\}]{save}| $\Rightarrow$ \UseVerb[retokenize, commandchars=\\\{\}]{save}
769 % \end{itemize}
770 %
771 %
772 %
773 %
774 %
775 % \section{New commands and environments}
776 % \label{sec:new-cmd-env}
777 %
778 % \subsection{\cmd{\EscVerb}}
779 % \DescribeMacro{\EscVerb*\oarg{options}\marg{backslash-escaped~text}}
780 %
781 % This is like \cmd{\Verb} but with backslash escapes to allow for characters such as |#| and |%|. For example, |\EscVerb{\\Verb{\#\%}}| gives \EscVerb{\\Verb{\#\%}}. It behaves exactly the same regardless of whether it is used inside another command. Like the reimplemented \cmd{\Verb}, it works in movable arguments (for example, in \cmd{\section}), and is compatible with \pkg{hyperref} for generating PDF strings (for example, PDF bookmarks).
782 %
783 % \begin{description}
784 % \item[Delimiters] Text must \emph{always} be delimited with a pair of curly braces |{...}|. This ensures that \cmd{\EscVerb} is always used in the same manner regardless of whether it is inside another command.
785 % \item[Escaping rules]\hfill
786 % \begin{itemize}
787 % \item Only printable, non-alphanumeric ASCII characters (symbols, punctuation) can be escaped with backslashes.\footnote{Allowing backslash escapes of letters would lead to ambiguity regarding spaces; see \cmd{\Verb}.}
788 % \item Always escape these characters: |\|, |%|, |#|.
789 % \item Escape spaces when there are more than one in a row.
790 % \item Escape |^| if there are more than one in a row.
791 % \item Escape unpaired curly braces.
792 % \item Additional symbols or punctuation characters may require escaping if they are made \cmd{\active}, depending on their definitions.
793 % \end{itemize}
794 % \item[Using in movable arguments] \cmd{\EscVerb} works automatically in movable arguments, such as in a \cmd{\section}. \cmd{\protect} or similar measures are not needed for \cmd{\EscVerb} itself, or for any of its arguments, and should not be used. \cmd{\EscVerb} performs operations that amount to applying \cmd{\protect} to all of these automatically.
795 % \item[\pkg{hyperref} PDF strings] \cmd{\EscVerb} is compatible with \pkg{hyperref} for generating PDF strings such as PDF bookmarks. Note that the PDF strings are \emph{always} a literal rendering of the verbatim text after backslash escapes have been applied, with all \fancyvrb\ options ignored. For example, things like |showspaces| and |commandchars| have no effect. If you need options to be applied to obtain desired PDF strings, consider a custom approach, perhaps using \cmd{\texorpdfstring}.
796 % \end{description}
797 %
798 %
799 % \subsection{\cmd{VerbEnv}}
800 % \DescribeMacro{\begin\{VerbEnv\}\oarg{options}\\$\langle$\textit{single line}$\rangle$\\\cmd{\end{VerbEnv}}}
801 %
802 % This is an environment variant of \cmd{\Verb}. The environment must contain only a single line of text, and the closing \cmd{\end{VerbEnv}} must be on a line by itself. The $\langle$\textit{options}$\rangle$ and $\langle$\textit{single line}$\rangle$ are read and then passed on to \cmd{\Verb} internally for actual typesetting.
803 %
804 % While \cmd{VerbEnv} can be used by document authors, it is primarily intended for package creators. For example, it is used in \pkg{minted} to implement \cmd{\mintinline}. In that case, highlighted code is always generated within a \cmd{Verbatim} environment. It is possible to process this as inline rather than block verbatim by \cmd{\let}ting \cmd{\Verbatim} to \cmd{\VerbEnv}.
805 %
806 %\begin{example}
807 %BEFORE\begin{VerbEnv}
808 %_inline_
809 %\end{VerbEnv}
810 %AFTER
811 %\end{example}
812 %
813 % \cmd{VerbEnv} is not implemented using the typical \fancyvrb\ environment implementation style, so it is not compatible with |\RecustomVerbatimEnvironment|.
814 %
815 %
816 % \subsection{\cmd{VerbatimWrite}}
817 % \DescribeMacro{\begin\{VerbatimWrite\}\oarg{opt}\\$\langle$\textit{lines}$\rangle$\\\cmd{\end{VerbatimWrite}}}
818 %
819 % This writes environment contents verbatim to an external file. It is similar to \fancyvrb's |VerbatimOut|, except that (1) it allows writing to a file multiple times (multiple environments can write to the same file) and (2) by default it uses |\detokenize| to guarantee truly verbatim output.
820 %
821 % By default, all \fancyvrb\ options except for |VerbatimWrite|-specific options are ignored. This can be customized on a per-environment basis via environment optional arguments.
822 %
823 % ~
824 %
825 % \noindent Options defined specifically for \cmd{VerbatimWrite}:
826 %
827 % \begin{optionlist}
828 % \item[writefilehandle (file handle) (\meta{none})]
829 % File handle for writing. For example,
830 % \begin{Verbatim}[gobble=2]
831 % \newwrite\myfile
832 % \immediate\openout\myfile=myfile.txt\relax
833 %
834 % \begin{VerbatimWrite}[writefilehandle=\myfile]
835 % ...
836 % \end{VerbatimWrite}
837 %
838 % \immediate\closeout\myfile
839 % \end{Verbatim}
840 %
841 % \item[writer (macro) (\cmd{\FancyVerbDefaultWriter})]
842 % This is the macro that processes each line of text in the environment and then writes it to file. This is the default implementation:
843 % \begin{Verbatim}[gobble=2]
844 % \def\FancyVerbDefaultWriter#1{%
845 % \immediate\write\FancyVerbWriteFileHandle{\detokenize{#1}}}
846 % \end{Verbatim}
847 % \end{optionlist}
848 %
849 %
850 %
851 % \subsection{Buffers}
852 %
853 % \subsubsection{\cmd{VerbatimBuffer}}
854 % \DescribeMacro{\begin\{VerbatimBuffer\}\oarg{opt}\\$\langle$\textit{lines}$\rangle$\\\cmd{\end{VerbatimBuffer}}}
855 %
856 % This environment stores its contents verbatim in a ``buffer,'' a sequence of numbered macros each of which contains one line of the environment. The ``buffered'' lines can then be iterated over for further processing or later use. This is similar to \fancyvrb's |SaveVerbatim|, which saves an environment for later use. |VerbatimBuffer| offers additional flexibility by capturing truly verbatim environment contents using |\detokenize| and saving environment contents in a format designed for further processing.
857 %
858 % By default, all \fancyvrb\ options except for |VerbatimBuffer|-specific options are ignored. This can be customized on a per-environment basis via environment optional arguments.
859 %
860 % Below is an extended example that demonstrates what is possible with |VerbatimBuffer| combined with various buffer commands. This uses |\ifstrequal| from the \pkg{etoolbox} package.
861 % \begin{itemize}
862 % \item |\setformatter| defines an empty |\formatter| macro. Then it defines a |\lineprocessor| macro that takes a buffer line as an argument. If this line contains only the text ``red'', then |\lineprocessor| redefines |\formatter| to |\color{red}| and invokes |\IterateBufferBreak| to stop iteration over the buffer. Finally, |\IterateBuffer| is used to apply |\lineprocessor| to each line in the buffer, until the final line is reached or |\IterateBufferBreak| is invoked.
863 % \item |afterbuffer| involves two steps: (1) |\setformatter| loops through the buffer and defines |\formatter| based on the buffer contents, and (2) |\VerbatimInsertBuffer| typesets the buffer, using |formatcom=\formatter| to format the text based on whether any line contains only the text ``red''.
864 % \end{itemize}
865 %
866 %\begin{tcbexample}
867 %\def\setformatter{%
868 % \def\formatter{}%
869 % \def\lineprocessor##1{%
870 % \ifstrequal{##1}{red}{\def\formatter{\color{red}}\IterateBufferBreak}{}}%
871 % \IterateBuffer{\lineprocessor}}
872 %
873 %\begin{VerbatimBuffer}[
874 % afterbuffer={\setformatter\VerbatimInsertBuffer[formatcom=\formatter]}
875 %]
876 %first
877 %second
878 %red
879 %\end{VerbatimBuffer}
880 %\end{tcbexample}
881 %
882 % Here is the same example, but rewritten to use a global buffer with custom buffer name instead.
883 %
884 %\begin{tcbexample}
885 %\begin{VerbatimBuffer}[globalbuffer, buffername=exbuff]
886 %first
887 %second
888 %red
889 %\end{VerbatimBuffer}
890 %
891 %\def\formatter{}
892 %\def\lineprocessor#1{%
893 % \ifstrequal{#1}{red}{\def\formatter{\color{red}}\IterateBufferBreak}{}}
894 %\IterateBuffer[buffername=exbuff]{\lineprocessor}
895 %
896 %\VerbatimInsertBuffer[buffername=exbuff, formatcom=\formatter]
897 %\end{tcbexample}
898 %
899 % ~
900 %
901 % \noindent Options defined specifically for \cmd{VerbatimBuffer}:
902 %
903 % \begin{optionlist}
904 % \item[afterbuffer (macro) (\meta{none})]
905 % Macro or macros invoked at the end of the environment, after all lines of the environment have been buffered. This is outside the |\begingroup...\endgroup| that wraps verbatim processing, so \fancyvrb\ settings are no longer active. However, the buffer line macros and the buffer length macro are still accessible even when |globalbuffer=false|.
906 %
907 % When |afterbuffer| is used to typeset the buffer, the typeset buffer may contain |VerbatimBuffer| or environments based on it. Typically, nested buffering should be avoided for a given buffer; a different buffer should be used at each level of nesting. Otherwise, for nested |VerbatimBuffer| environments, the buffer will contain the contents of the outermost |VerbatimBuffer| environment concatenated with the contents of nested environments.
908 %
909 % The current buffer depth is available in |\FancyVerbBufferDepth|. This has a value of |0| outside of any |VerbatimBuffer| environments, a value of |1| within the outermost |VerbatimBuffer| environment, and so forth. This can be used to automatically generate a unique |buffername| at a given nesting depth.
910 %
911 % \item[bufferer (macro) (\Verb{\FancyVerbDefaultBufferer})]
912 % This is the macro that adds lines to the buffer. The default is designed to create a truly verbatim buffer via |\detokenize|. This can be customized if you wish to use \fancyvrb\ options related to catcodes to create a buffer that is only partially verbatim (that contains macros).
913 % \begin{Verbatim}[gobble=2, fontsize=\small]
914 % \def\FancyVerbDefaultBufferer#1{%
915 % \expandafter\xdef\csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname{%
916 % \detokenize{#1}}}
917 % \end{Verbatim}
918 % A custom |bufferer| must take a single argument |#1| (a line of the environment text) and ultimately store the processed line in a macro called
919 % \begin{Verbatim}[gobble=2]
920 % \csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname
921 % \end{Verbatim}
922 % This macro must be defined globally, so |\xdef| or |\gdef| is necessary (this does not interfere with scoping from |globalbuffer|). Otherwise, there are no restrictions. The |\xdef| and |\detokenize| in the default definition guarantee that the buffer consists only of the literal text from the environment, but this is not required for a custom |bufferer|.
923 %
924 % \item[bufferlengthname (string) (FancyVerbBufferLength)]
925 % Name of the macro storing the length of the buffer. This is the number of lines stored.
926 %
927 % Macros that operate within a |VerbatimBuffer| environment can access the current value of |bufferlengthname| via |\FancyVerbBufferLengthName|.
928 %
929 % \item[bufferlinename (string) (FancyVerbBufferLine)]
930 % The base name of the buffer line macros. The default is |FancyVerbBufferLine|, which will result in buffer macros |\FancyVerbBufferLine<n>| with integer |n| greater than or equal to one and less than or equal to the number of lines (one-based indexing). Since buffer macro names contain a number, they must be accessed differently than typical macros:
931 % \begin{Verbatim}[gobble=2]
932 % \csname FancyVerbBufferLine<n>\endcsname
933 % \@nameuse{FancyVerbBufferLine<n>}
934 % \end{Verbatim}
935 % If the buffer macros are looped over with a counter that is incremented, then |<n>| should be the counter value |\arabic{<counter>}|.
936 %
937 % Macros that operate within a |VerbatimBuffer| environment can access the current value of |bufferlinename| via |\FancyVerbBufferLineName|.
938 %
939 % \item[buffername (string) (\meta{none})]
940 % Shortcut for setting |bufferlengthname| and |bufferlinename| simultaneously, using the same root name. This sets |bufferlengthname| to |<buffername>length| and |bufferlinename| to |<buffername>line|.
941 %
942 % \item[globalbuffer (bool) (false)]
943 % This determines whether buffer line macros are defined globally, that is, whether they are accessible after the end of the |VerbatimBuffer| environment. If the line macros are defined globally, then the buffer length macro is also increased appropriately outside the environment. |globalbuffer| does not affect any |afterbuffer| macro, since that is invoked inside the environment.
944 %
945 % When buffered lines are used immediately, consider using |afterbuffer| instead of |globalbuffer|. When buffered lines must be used later in a document, consider using |globalbuffer| with custom (and perhaps unique) |bufferlinename| and |bufferlengthname|.
946 %
947 % When |globalbuffer=true|, |VerbatimBuffer| environments with the same buffer name will append to a single buffer, so that it ultimately contains the concatenated contents of all environments. A |VerbatimBuffer| environment with |globalbuffer=false| will append to the buffer created by any previous |VerbatimBuffer| that had |globalbuffer=true| and shared the same buffer name. Any |afterbuffer| macro will have access to a buffer containing the concatenated data. At the very end of the environment with |globalbuffer=false|, after any |afterbuffer|, this appended content will be removed. All buffer line macros (from |bufferlinename|) that were created by that environment are ``deleted'' (|\let| to an undefined macro), and the buffer length macro (from |bufferlengthname|) is reduced proportionally.
948 % \end{optionlist}
949 %
950 %
951 % \subsubsection{\cmd{\VerbatimInsertBuffer}}
952 % \DescribeMacro{\VerbatimInsertBuffer\oarg{options}}
953 %
954 % This inserts an existing buffer created by |VerbatimBuffer| as a verbatim environment. The |Verbatim| environment is used by default, but this can be customized by setting |insertenvname|. |\VerbatimInsertBuffer| modifies |Verbatim| and |BVerbatim| internals to function with a buffer in a command context. See the |VerbatimBuffer| documentation for an example of usage.
955 %
956 % Options related to catcodes cause the buffer to be retokenized during typesetting. That is, the \fancyvrb\ options used for |\VerbatimInsertBuffer| are not restricted by those that were in effect when |VerbatimBuffer| originally created the buffer, so long as the buffer contains a complete representation of the original |VerbatimBuffer| environment contents.
957 %
958 % |\VerbatimInsertBuffer| is not implemented using the typical \fancyvrb\ command and environment implementation styles, so it is not compatible with |\RecustomVerbatimCommand| or |\RecustomVerbatimEnvironment|.
959 %
960 % ~\par
961 % \noindent Options defined specifically for \cmd{\VerbatimInsertBuffer}:
962 %
963 % \begin{optionlist}
964 % \item[insertenvname (string) (Verbatim)]
965 % This is the name of the verbatim environment used for inserting the buffer. |insertenvname| can be any |Verbatim|- or |BVerbatim|-based environment. Environments defined with |\CustomVerbatimEnvironment| and |\RecustomVerbatimEnvironment| are supported. User-implemented environments that serve as wrappers around |Verbatim| or |BVerbatim| should typically be compatible so long as they accept \pkg{fancyvrb}/\pkg{fvextra} optional arguments in the same way as |Verbatim| and |BVerbatim|.
966 % \end{optionlist}
967 %
968 %
969 % \subsubsection{\cmd{\VerbatimClearBuffer}}
970 % \DescribeMacro{\VerbatimClearBuffer\oarg{options}}
971 %
972 % Clear an existing buffer created with \cmd{VerbatimBuffer}. |\global\let| all buffer line macros to an undefined macro and set the buffer length macro to zero.
973 %
974 %
975 % \subsubsection{\cmd{\InsertBuffer}}
976 % \DescribeMacro{\InsertBuffer\oarg{options}}
977 % This inserts an existing buffer created with |VerbatimBuffer| so that it is interpreted as \LaTeX. The result is essentially the same as if the buffered text had been included literally at the insertion point. The buffer is processed with |\scantokens|.
978 %
979 % For typesetting verbatim text, |\InsertBuffer| with the |wrapperenv| option set to a verbatim environment can be used as an alternative to |\VerbatimInsertBuffer|. Both typically have similar performance.
980 %
981 % \begin{optionlist}
982 % \item[wrapperenvname (string) (\meta{none})]
983 % Name of environment used to wrap the buffer. This is inserted within the |\scantokens| that is used to process the buffer, so if |wrapperenvname| is set to a verbatim environment, then it will cause the buffer to be typeset verbatim. This is useful for non-\pkg{fancyvrb} verbatim environments that are not supported by |\VerbatimInsertBuffer|.
984 %
985 % \item[wrapperenvopt (string) (\meta{none})]
986 % Optional argument |[|\meta{opt}|]| passed to wrapper environment |wrapperenvname|.
987 %
988 % \item[wrapperenvarg (string) (\meta{none})]
989 % Mandatory argument |{|\meta{arg}|}| passed to wrapper environment |wrapperenvname|.
990 % \end{optionlist}
991 %
992 %\begin{tcblisting}{oversize=5em}
993 %\begin{VerbatimBuffer}[buffername=ins, globalbuffer]
994 %Text. Math $f(x)$. Verb \Verb|~#\{}|.
995 %\begin{Verbatim}
996 %Verbatim. ~#\{}.
997 %\end{Verbatim}
998 %More text.
999 %\end{VerbatimBuffer}
1000 %
1001 %\textbf{BEFORE}\InsertBuffer[buffername=ins]\textbf{AFTER}
1002 %\end{tcblisting}
1003 %
1004 %
1005 % \subsubsection{\cmd{\ClearBuffer}}
1006 % \DescribeMacro{\ClearBuffer\oarg{options}}
1007 % Alias for \cmd{\VerbatimClearBuffer}.
1008 %
1009 %
1010 % \subsubsection{\cmd{\BufferMdfivesum}}
1011 % \DescribeMacro{\BufferMdfivesum}
1012 % Calculate the MD5 sum of the current buffer.
1013 %
1014 % |\BufferMdfivesum| operates on the current buffer. Because it is fully expandable, it cannot take an optional argument to switch buffers. To switch buffers, use something like |\fvset{buffername=...}| before |\BufferMdfivesum|.
1015 %
1016 %\begin{tcblisting}{oversize=5em}
1017 %\begin{VerbatimBuffer}[afterbuffer=\xdef\bufferhash{\BufferMdfivesum}]
1018 %Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
1019 %eiusmod tempor incididunt ut labore et dolore magna aliqua.
1020 %\end{VerbatimBuffer}
1021 %
1022 %\texttt{\bufferhash}
1023 %\end{tcblisting}
1024 %
1025 %
1026 % \subsubsection{\cmd{\IterateBuffer}}
1027 % \DescribeMacro{\IterateBuffer\oarg{options}\marg{macro}}
1028 %
1029 % \noindent Iterate over buffer from beginning to end, invoking \meta{macro} on each line. \meta{macro} must be a macro or sequence of macros that consumes a single brace-delimited argument, which is the current line in the buffer. |\IterateBufferBreak| is available to break iteration immediately after the current \meta{macro} invocation completes. During iteration, |\FancyVerbBufferLengthName|, |\FancyVerbBufferLineName|, and |\FancyVerbBufferIndex| can be used to access the name of the current buffer and index within it.
1030 %
1031 %
1032 % \subsubsection{\cmd{\WriteBuffer}}
1033 % \DescribeMacro{\WriteBuffer\oarg{options}}
1034 % This writes the current buffer to an external file, using the |writefilehandle| and |writer| options from |VerbatimWrite|. It is the buffer equivalent of |VerbatimWrite|.
1035 %
1036 %
1037 %
1038 %
1039 % \section{Line breaking}
1040 % \label{sec:breaklines}
1041 %
1042 % Automatic line breaking may be turned on with |breaklines=true|. By default, breaks only occur at spaces. Breaks may be allowed anywhere with |breakanywhere|, or only before or after specified characters with |breakbefore| and |breakafter|. Many options are provided for customizing breaks. A good place to start is the description of |breaklines|.
1043 %
1044 % When a line is broken, the result must fit on a single page. There is no support for breaking a line across multiple pages.
1045 %
1046 %
1047 % \subsection{Line breaking options}
1048 %
1049 % Options are provided for customizing typical line breaking features. See \cref{sec:breaklines:advanced} for details about low-level customization of break behavior.
1050 %
1051 % \begin{optionlist}
1052 %
1053 % \item[breakafter (string) (\meta{none})]
1054 % Break lines after specified characters, not just at spaces, when |breaklines=true|. For example, |breakafter=-/| would allow breaks after any hyphens or slashes. Special characters given to |breakafter| should be backslash-escaped (usually |#|, |{|, |}|, |%|, |[|, |]|, and the comma |,|; the backslash |\| may be obtained via |\\| and the space via |\space|).\footnote{|breakafter| expands each token it is given once, so when it is given a macro like |\%|, the macro should expand to a literal character that will appear in the text to be typeset. \fvextra\ defines special character escapes that are activated for |breakafter| so that this will work with common escapes. The only exception to token expansion is non-ASCII characters under pdfTeX; these should appear literally. |breakafter| is not catcode-sensitive.}
1055 %
1056 % For an alternative, see |breakbefore|. When |breakbefore| and |breakafter| are used for the same character, |breakbeforeinrun| and |breakafterinrun| must both have the same setting.
1057 %
1058 % Note that when |commandchars| or |codes| are used to include macros within verbatim content, breaks will not occur within mandatory macro arguments by default. Depending on settings, macros that take optional arguments may not work unless the entire macro including arguments is wrapped in a group (curly braces |{}|, or other characters specified with |commandchars|). See \cref{sec:breaklines:advanced} for details, and consider |breaknonspaceingroup| as a solution in simple cases.
1059 %
1060 %\begin{longexample}
1061 %\begin{Verbatim}[breaklines, breakafter=d]
1062 %some_string = 'SomeTextThatGoesOnAndOnForSoLongThatItCouldNeverFitOnOneLine'
1063 %\end{Verbatim}
1064 %\end{longexample}
1065 %
1066 %
1067 % \item[breakafterinrun (boolean) (false)]
1068 % When |breakafter| is used, insert breaks within runs of identical characters. If |false|, treat sequences of identical characters as a unit that cannot contain breaks. When |breakbefore| and |breakafter| are used for the same character, |breakbeforeinrun| and |breakafterinrun| must both have the same setting.
1069 %
1070 %
1071 % \item[breakaftersymbolpre (string) (\string\,\string\footnotesize\string\ensuremath\{\_\string\rfloor\}, \,\footnotesize\ensuremath{_\rfloor})]
1072 % The symbol inserted pre-break for breaks inserted by |breakafter|. This does not apply to breaks inserted next to spaces; see |spacebreak|.
1073 %
1074 %
1075 % \item[breakaftersymbolpost (string) (\meta{none})]
1076 % The symbol inserted post-break for breaks inserted by |breakafter|. This does not apply to breaks inserted next to spaces; see |spacebreak|.
1077 %
1078 %
1079 % \item[breakanywhere (boolean) (false)]
1080 % Break lines anywhere, not just at spaces, when |breaklines=true|.
1081 %
1082 % Note that when |commandchars| or |codes| are used to include macros within verbatim content, breaks will not occur within mandatory macro arguments by default. Depending on settings, macros that take optional arguments may not work unless the entire macro including arguments is wrapped in a group (curly braces |{}|, or other characters specified with |commandchars|). See \cref{sec:breaklines:advanced} for details, and consider |breaknonspaceingroup| as a solution in simple cases.
1083 %
1084 %\begin{longexample}
1085 %\begin{Verbatim}[breaklines, breakanywhere]
1086 %some_string = 'SomeTextThatGoesOnAndOnForSoLongThatItCouldNeverFitOnOneLine'
1087 %\end{Verbatim}
1088 %\end{longexample}
1089 %
1090 %
1091 % \item[breakanywheresymbolpre (string) (\string\,\string\footnotesize\string\ensuremath\{\_\string\rfloor\}, \,\footnotesize\ensuremath{_\rfloor})]
1092 % The symbol inserted pre-break for breaks inserted by |breakanywhere|. This does not apply to breaks inserted next to spaces; see |spacebreak|.
1093 %
1094 %
1095 % \item[breakanywhereinlinestretch (length) (\meta{none})]
1096 % Stretch glue to insert at potential |breakanywhere| break locations in inline contexts, to give better line widths and avoid overfull |\hbox|. This allows the spacing between adjacent non-space characters to stretch, so it should not be used when column alignment is important. For typical line lengths, values between |0.01em| and |0.02em| should be sufficient to provide a cumulative stretch per line that is equal to or greater than the width of one character.
1097 %
1098 % This is typically not needed in cases where an overfull |\hbox| only overflows by tiny amount, perhaps a fraction of a pt. In those cases, the overfull |\hbox| could be ignored, |\hfuzz| could be set to 1pt or 2pt to suppress tiny overfull |\hbox| warnings, or |breakanywheresymbolpre| might be redefined to adjust spacing.
1099 %
1100 % Implementation: Before each |breakanywhere| break location, this inserts the following sequence of macros:
1101 %\begin{verbatim}
1102 %\nobreak\hspace{0pt plus \FV@breaknonspaceinlinestretch}
1103 %\end{verbatim}
1104 %
1105 %
1106 % \item[breakanywheresymbolpost (string) (\meta{none})]
1107 % The symbol inserted post-break for breaks inserted by |breakanywhere|. This does not apply to breaks inserted next to spaces; see |spacebreak|.
1108 %
1109 %
1110 % \item[breakautoindent (boolean) (true)]
1111 % When a line is broken, automatically indent the continuation lines to the indentation level of the first line. When |breakautoindent| and |breakindent| are used together, the indentations add. This indentation is combined with |breaksymbolindentleft| to give the total actual left indentation.
1112 %
1113 %
1114 % \item[breakbefore (string) (\meta{none})]
1115 % Break lines before specified characters, not just at spaces, when |breaklines=true|. For example, |breakbefore=A| would allow breaks before capital A's. Special characters given to |breakbefore| should be backslash-escaped (usually |#|, |{|, |}|, |%|, |[|, |]|, and the comma |,|; the backslash |\| may be obtained via |\\| and the space via |\space|).\footnote{|breakbefore| expands each token it is given once, so when it is given a macro like |\%|, the macro should expand to a literal character that will appear in the text to be typeset. \fvextra\ defines special character escapes that are activated for |breakbefore| so that this will work with common escapes. The only exception to token expansion is non-ASCII characters under pdfTeX; these should appear literally. |breakbefore| is not catcode-sensitive.}
1116 %
1117 % For an alternative, see |breakafter|. When |breakbefore| and |breakafter| are used for the same character, |breakbeforeinrun| and |breakafterinrun| must both have the same setting.
1118 %
1119 % Note that when |commandchars| or |codes| are used to include macros within verbatim content, breaks will not occur within mandatory macro arguments by default. Depending on settings, macros that take optional arguments may not work unless the entire macro including arguments is wrapped in a group (curly braces |{}|, or other characters specified with |commandchars|). See \cref{sec:breaklines:advanced} for details, and consider |breaknonspaceingroup| as a solution in simple cases.
1120 %
1121 %\begin{longexample}
1122 %\begin{Verbatim}[breaklines, breakbefore=A]
1123 %some_string = 'SomeTextThatGoesOnAndOnForSoLongThatItCouldNeverFitOnOneLine'
1124 %\end{Verbatim}
1125 %\end{longexample}
1126 %
1127 %
1128 % \item[breakbeforeinrun (boolean) (false)]
1129 % When |breakbefore| is used, insert breaks within runs of identical characters. If |false|, treat sequences of identical characters as a unit that cannot contain breaks. When |breakbefore| and |breakafter| are used for the same character, |breakbeforeinrun| and |breakafterinrun| must both have the same setting.
1130 %
1131 %
1132 % \item[breakbeforesymbolpre (string) (\string\,\string\footnotesize\string\ensuremath\{\_\string\rfloor\}, \,\footnotesize\ensuremath{_\rfloor})]
1133 % The symbol inserted pre-break for breaks inserted by |breakbefore|. This does not apply to breaks inserted next to spaces; see |spacebreak|.
1134 %
1135 %
1136 % \item[breakbeforesymbolpost (string) (\meta{none})]
1137 % The symbol inserted post-break for breaks inserted by |breakbefore|. This does not apply to breaks inserted next to spaces; see |spacebreak|.
1138 %
1139 %
1140 % \item[breakcollapsespaces (bool) (true)]
1141 % When \Verb{true} (default), a line break within a run of regular spaces (\Verb{showspaces=false}) replaces all spaces with a single break, and the wrapped line after the break starts with a non-space character. When \Verb{false}, a line break within a run of regular spaces preserves all spaces, and the wrapped line after the break may start with one or more spaces. This causes regular spaces to behave exactly like the visible spaces produced with \Verb{showspaces}; both give identical line breaks, with the only difference being the appearance of spaces.
1142 %
1143 %
1144 % \item[breakindent (dimension) (\meta{breakindentnchars})]
1145 % When a line is broken, indent the continuation lines by this amount. When |breakautoindent| and |breakindent| are used together, the indentations add. This indentation is combined with |breaksymbolindentleft| to give the total actual left indentation.
1146 %
1147 % \item[breakindentnchars (integer) (0)]
1148 % This allows |breakindent| to be specified as an integer number of characters rather than as a dimension (assumes a fixed-width font).
1149 %
1150 %
1151 % \item[breaklines (boolean) (false)]
1152 % Automatically break long lines.
1153 %
1154 % \textbf{Limitations for verbatim environments/block text}, such as |Verbatim|: When a line is broken, the result must fit on a single page. There is no support for breaking a line across multiple pages.\footnote{Following the implementation in \pkg{fancyvrb}, each line is typeset within an |\hbox|, so page breaks are not possible.}
1155 %
1156 % \textbf{Limitations for verbatim commands/inline text}, such as |\Verb|: When a line break is inserted, the text may still overflow into the margin or cause an overfull |\hbox|, depending on hyphenation settings and various penalties related to line breaks. It may be possible to avoid this by allowing additional break locations with |breakbefore|, |breakafter|, or |breakanywhere|. Small overfull |\hbox| warnings can be suppressed by setting |\hfuzz| to a larger value, for example setting it to |2pt| instead of the default |0.1pt|. It is also possible to combine |breakanywhere| with |breakanywhereinlinestretch| to allow flexible spacing between adjacent non-space characters. In cases where it is better to break before the margin rather than overflowing into the margin, consider setting |\emergencystretch|.
1157 %
1158 % By default, automatic breaks occur at spaces (even when |showspaces=true|). Use |breakanywhere| to enable breaking anywhere; use |breakbefore| and |breakafter| for more fine-tuned breaking.
1159 %
1160 %\begin{example}
1161 %...text.
1162 %\begin{Verbatim}[breaklines]
1163 %def f(x):
1164 % return 'Some text ' + str(x)
1165 %\end{Verbatim}
1166 %\end{example}
1167 %
1168 % To customize the indentation of broken lines, see |breakindent| and |breakautoindent|. To customize the line continuation symbols, use |breaksymbolleft| and |breaksymbolright|. To customize the separation between the continuation symbols and the text, use |breaksymbolsepleft| and |breaksymbolsepright|. To customize the extra indentation that is supplied to make room for the break symbols, use |breaksymbolindentleft| and |breaksymbolindentright|. Since only the left-hand symbol is used by default, it may also be modified using the alias options |breaksymbol|, |breaksymbolsep|, and |breaksymbolindent|.
1169 %
1170 % An example using these options to customize the |Verbatim| environment is shown below. This uses the |\carriagereturn| symbol from the \pkg{dingbat} package.
1171 %
1172 % \begingroup
1173 % \fvset{breaklines, xleftmargin=1em, xrightmargin=1em}
1174 %\begin{longexample}
1175 %\begin{Verbatim}[breaklines,
1176 % breakautoindent=false,
1177 % breaksymbolleft=\raisebox{0.8ex}{
1178 % \small\reflectbox{\carriagereturn}},
1179 % breaksymbolindentleft=0pt,
1180 % breaksymbolsepleft=0pt,
1181 % breaksymbolright=\small\carriagereturn,
1182 % breaksymbolindentright=0pt,
1183 % breaksymbolsepright=0pt]
1184 %def f(x):
1185 % return 'Some text ' + str(x) + ' some more text ' + str(x) + ' even more text that goes on for a while'
1186 %\end{Verbatim}
1187 %\end{longexample}
1188 % \endgroup
1189 %
1190 % Beginning in version 1.6, automatic line breaks work with |showspaces=true| by default. Defining |breakbefore| or |breakafter| for |\space| is no longer necessary. For example,
1191 %
1192 %\begin{longexample}
1193 %\begin{Verbatim}[breaklines, showspaces]
1194 %some_string = 'Some Text That Goes On And On For So Long That It Could Never Fit'
1195 %\end{Verbatim}
1196 %\end{longexample}
1197 %
1198 %
1199 % \item[breaknonspaceingroup (boolean) (false)]
1200 % By using |commandchars|, it is possible to include \LaTeX\ commands within otherwise verbatim text. In these cases, there can be groups (typically |{...}| but depends on |commandchars|) within verbatim. Spaces within groups are treated as potential line break locations when |breaklines=true|, but by default no other break locations are inserted (|breakbefore|, |breakafter|, |breakanywhere|). This is because inserting non-space break locations can interfere with command functionality. For example, in |\textcolor{red}{text}|, breaks shouldn't be inserted within |red|.
1201 %
1202 % |breaknonspaceingroup| allows non-space breaks to be inserted within groups. This option should only be used when |commandchars| is including \LaTeX\ commands that do not take optional arguments and only take mandatory arguments that are typeset. Something like |\textit{text}| is fine, but |\textcolor{red}{text}| is not because one of the mandatory arguments is not typeset but rather provides a setting. For more complex commands, it is typically better to redefine them to insert breaks in appropriate locations using |\FancyVerbBreakStart...\FancyVerbBreakStop|.
1203 %
1204 %
1205 % \item[breakpreferspaces (boolean) (true)]
1206 % This determines whether line breaks are preferentially inserted at normal spaces (|breakcollapsespaces=true|, |showspaces=false|) rather than at other locations allowed by |breakbefore|, |breakafter|, or |breakanywhere|.
1207 %
1208 % Using |breakpreferspaces=false| with |breakanywhere=true| will typically result in all broken segments of a line going all the way to the right margin.
1209 %
1210 %
1211 % \item[breaksymbol (string) (breaksymbolleft)]
1212 % Alias for |breaksymbolleft|.
1213 %
1214 %
1215 % \item[breaksymbolleft (string) (\string\tiny\string\ensuremath\{\string\hookrightarrow\}, {\tiny\ensuremath{\hookrightarrow}})]
1216 % The symbol used at the beginning (left) of continuation lines when |breaklines=true|. To have no symbol, simply set |breaksymbolleft| to an empty string (``|=,|'' or ``|={}|''). The symbol is wrapped within curly braces |{}| when used, so there is no danger of formatting commands such as |\tiny| ``escaping.''
1217 %
1218 % The |\hookrightarrow| and |\hookleftarrow| may be further customized by the use of the |\rotatebox| command provided by \pkg{graphicx}. Additional arrow-type symbols that may be useful are available in the \pkg{dingbat} (|\carriagereturn|) and \pkg{mnsymbol} (hook and curve arrows) packages, among others.
1219 %
1220 %
1221 % \item[breaksymbolright (string) (\meta{none})]
1222 % The symbol used at breaks (right) when |breaklines=true|. Does not appear at the end of the very last segment of a broken line.
1223 %
1224 %
1225 % \item[breaksymbolindent (dimension) (\meta{breaksymbolindentleftnchars})]
1226 % Alias for |breaksymbolindentleft|.
1227 %
1228 % \item[breaksymbolindentnchars (integer) (\meta{breaksymbolindentleftnchars})]
1229 % Alias for |breaksymbolindentleftnchars|.
1230 %
1231 %
1232 % \item[breaksymbolindentleft (dimension) (\meta{breaksymbolindentleftnchars})]
1233 % The extra left indentation that is provided to make room for |breaksymbolleft|. This indentation is only applied when there is a |breaksymbolleft|.
1234 %
1235 % \item[breaksymbolindentleftnchars (integer) (4)]
1236 % This allows |breaksymbolindentleft| to be specified as an integer number of characters rather than as a dimension (assumes a fixed-width font).
1237 %
1238 % \item[breaksymbolindentright (dimension) (\meta{breaksymbolindentrightnchars})]
1239 % The extra right indentation that is provided to make room for |breaksymbolright|. This indentation is only applied when there is a |breaksymbolright|.
1240 %
1241 % \item[breaksymbolindentrightnchars (integer) (4)]
1242 % This allows |breaksymbolindentright| to be specified as an integer number of characters rather than as a dimension (assumes a fixed-width font).
1243 %
1244 %
1245 % \item[breaksymbolsep (dimension) (\meta{breaksymbolsepleftnchars})]
1246 % Alias for |breaksymbolsepleft|.
1247 %
1248 % \item[breaksymbolsepnchars (integer) (\meta{breaksymbolsepleftnchars})]
1249 % Alias for |breaksymbolsepleftnchars|.
1250 %
1251 %
1252 % \item[breaksymbolsepleft (dimension) (\meta{breaksymbolsepleftnchars})]
1253 % The separation between the |breaksymbolleft| and the adjacent text.
1254 %
1255 % \item[breaksymbolsepleftnchars (integer) (2)]
1256 % Allows |breaksymbolsepleft| to be specified as an integer number of characters rather than as a dimension (assumes a fixed-width font).
1257 %
1258 % \item[breaksymbolsepright (dimension) (\meta{breaksymbolseprightnchars})]
1259 % The \emph{minimum} separation between the |breaksymbolright| and the adjacent text. This is the separation between |breaksymbolright| and the furthest extent to which adjacent text could reach. In practice, |\linewidth| will typically not be an exact integer multiple of the character width (assuming a fixed-width font), so the actual separation between the |breaksymbolright| and adjacent text will generally be larger than |breaksymbolsepright|. This ensures that break symbols have the same spacing from the margins on both left and right. If the same spacing from text is desired instead, |breaksymbolsepright| may be adjusted. (See the definition of |\FV@makeLineNumber| for implementation details.)
1260 %
1261 % \item[breaksymbolseprightnchars (integer) (2)]
1262 % Allows |breaksymbolsepright| to be specified as an integer number of characters rather than as a dimension (assumes a fixed-width font).
1263 %
1264 %
1265 % \item[spacebreak (macro) (\Verb{\discretionary{}{}{}})]
1266 % This determines the break that is inserted around spaces when |breaklines=true| and one or more of the following conditions applies: |breakcollapsespaces=false|, |showspaces=true|, or the space is affected by |breakbefore| or |breakafter|. If it is redefined, it should typically be similar to |\FancyVerbBreakAnywhereBreak|, |\FancyVerbBreakBeforeBreak|, and |\FancyVerbBreakAfterBreak| to obtain consistent breaks.
1267 %
1268 %
1269 % \end{optionlist}
1270 %
1271 %
1272 %
1273 % \subsection{Line breaking and tab expansion}
1274 %
1275 % \fancyvrb\ provides an |obeytabs| option that expands tabs based on tab stops rather than replacing them with a fixed number of spaces (see \fancyvrb's |tabsize|). The \fancyvrb\ implementation of tab expansion is not directly compatible with \fvextra's line-breaking algorithm, but \fvextra\ builds on the \fancyvrb\ approach to obtain identical results.
1276 %
1277 % Tab expansion in the context of line breaking does bring some additional considerations that should be kept in mind. In each line, all tabs are expanded exactly as they would have been had the line not been broken. This means that after a line break, any tabs will not align with tab stops unless the total left indentation of continuation lines is a multiple of the tab stop width. The total indentation of continuation lines is the sum of |breakindent|, |breakautoindent|, and |breaksymbolindentleft| (alias |breaksymbolindent|).
1278 %
1279 % A sample |Verbatim| environment that uses |obeytabs| with |breaklines| is shown below, with numbers beneath the environment indicating tab stops (|tabsize=8| by default). The tab stops in the wrapped and unwrapped lines are identical. However, the continuation line does not match up with the tab stops because by default the width of |breaksymbolindentleft| is equal to four monospace characters. (By default, |breakautoindent=true|, so the continuation line gets a tab plus |breaksymbolindentleft|.) \par~\par
1280 %
1281 % \def\tabnums{\texttt{\textcolor{LightGray}{1234567}\textcolor{Tomato}{8}}}
1282 %
1283 % \noindent\begin{minipage}{\textwidth}
1284 % ~\par
1285 % \hrule
1286 % \begin{VerbatimVerbatim}[gobble=4, showtabs, breaklines, obeytabs]
1287 % \begin{Verbatim}[obeytabs, showtabs, breaklines]
1288 % First Second Third And more text that goes on for a while until wrapping is needed
1289 % First Second Third Forth
1290 % \end{Verbatim}
1291 % \end{VerbatimVerbatim}
1292 % \vspace{-0.1in}
1293 % \noindent\tabnums\tabnums\tabnums\tabnums\tabnums\tabnums\tabnums\tabnums\par~\par
1294 % \hrule~\\
1295 % \end{minipage}
1296 %
1297 % We can set the symbol indentation to eight characters by creating a dimen,
1298 %\begin{verbatim}
1299 %\newdimen\temporarydimen
1300 %\end{verbatim}
1301 %setting its width to eight characters,
1302 %\begin{verbatim}
1303 %\settowidth{\temporarydimen}{\ttfamily AaAaAaAa}
1304 %\end{verbatim}
1305 % and finally adding the option |breaksymbolindentleft=\temporarydimen| to the |Verbatim| environment to obtain the following:\par~\par
1306 %
1307 % \newdimen\temporarydimen
1308 % \settowidth{\temporarydimen}{\ttfamily AaAaAaAa}
1309 % \noindent\begin{minipage}{\textwidth}
1310 % ~\par
1311 % \hrule
1312 % \begin{Verbatim}[obeytabs, showtabs, breaklines, breaksymbolindentleft=\temporarydimen, gobble=4]
1313 % First Second Third And more text that goes on for a while until wrapping is needed
1314 % First Second Third Forth
1315 % \end{Verbatim}
1316 % \vspace{-0.1in}
1317 % \noindent\tabnums\tabnums\tabnums\tabnums\tabnums\tabnums\tabnums\tabnums\par~\par
1318 % \hrule~\\
1319 % \end{minipage}
1320 %
1321 %
1322 %
1323 % \subsection{Advanced line breaking}
1324 % \label{sec:breaklines:advanced}
1325 %
1326 % \subsubsection{A few notes on algorithms}
1327 %
1328 % |breakanywhere|, |breakbefore|, and |breakafter| work by scanning through the tokens in each line and inserting line breaking commands wherever a break should be allowed. By default, they skip over all groups (|{...}|) and all math (|$...$|). Note that this refers to curly braces and dollar signs with their normal \LaTeX\ meaning (catcodes), not verbatim curly braces and dollar signs; such non-verbatim content may be enabled with |commandchars| or |codes|. This means that math and macros that only take mandatory arguments (|{...}|) will function normally within otherwise verbatim text. However, macros that take optional arguments may not work because |[...]| is not treated specially, and thus break commands may be inserted within |[...]| depending on settings. Wrapping an entire macro, including its arguments, in a group will protect the optional argument: |{\|\meta{macro}|[|\meta{oarg}|]{|\meta{marg}|}}|.
1329 %
1330 % |breakbefore| and |breakafter| insert line breaking commands around specified characters. This process is catcode-independent; tokens are |\detokenize|d before they are checked against characters specified via |breakbefore| and |breakafter|.
1331 %
1332 %
1333 % \subsubsection{Breaks within macro arguments}
1334 %
1335 % \DescribeMacro{\FancyVerbBreakStart}
1336 %
1337 % \DescribeMacro{\FancyVerbBreakStop}
1338 %
1339 % When |commandchars| or |codes| are used to include macros within verbatim content, the options |breakanywhere|, |breakbefore|, and |breakafter| will not generate breaks within mandatory macro arguments. Macros with optional arguments may not work, depending on settings, unless they are wrapped in a group (curly braces |{}|, or other characters specified via |commandchars|).
1340 %
1341 % If you want to allow breaks within macro arguments (optional or mandatory), then you should (re)define your macros so that the relevant arguments are wrapped in the commands
1342 %\begin{verbatim}
1343 %\FancyVerbBreakStart ... \FancyVerbBreakStop
1344 %\end{verbatim}
1345 % For example, suppose you have the macro
1346 %\begin{verbatim}
1347 %\newcommand{\mycmd}[1]{\_before:#1:after\_}
1348 %\end{verbatim}
1349 % Then you would discover that line breaking does not occur:
1350 %
1351 % \newcommand{\mycmd}[1]{\_before:#1:after\_}
1352 %\begin{longexample}
1353 %\begin{Verbatim}[commandchars=\\\{\}, breaklines, breakafter=a]
1354 %\mycmd{1}\mycmd{2}\mycmd{3}\mycmd{4}\mycmd{5}
1355 %\end{Verbatim}
1356 %\end{longexample}
1357 %
1358 % Now redefine the macro:
1359 %\begin{verbatim}
1360 %\renewcommand{\mycmd}[1]{\FancyVerbBreakStart\_before:#1:after\_\FancyVerbBreakStop}
1361 %\end{verbatim}
1362 %
1363 % This is the result:
1364 %
1365 % \renewcommand{\mycmd}[1]{\FancyVerbBreakStart\_before:#1:after\_\FancyVerbBreakStop}
1366 %\begin{longexample}
1367 %\begin{Verbatim}[commandchars=\\\{\}, breaklines, breakafter=a]
1368 %\mycmd{1}\mycmd{2}\mycmd{3}\mycmd{4}\mycmd{5}
1369 %\end{Verbatim}
1370 %\end{longexample}
1371 %
1372 % Instead of completely redefining macros, it may be more convenient to use |\let|. For example,
1373 %\begin{verbatim}
1374 %\let\originalmycmd\mycmd
1375 %\renewcommand{\mycmd}[1]{%
1376 % \expandafter\FancyVerbBreakStart\originalmycmd{#1}\FancyVerbBreakStop}
1377 %\end{verbatim}
1378 % Notice that in this case |\expandafter| is required, because |\FancyVerbBreakStart| does not perform any expansion and thus will skip over |\originalmycmd{#1}| unless it is already expanded. The \pkg{etoolbox} package provides commands that may be useful for patching macros to insert line breaks.
1379 %
1380 % When working with |\FancyVerbBreakStart ... \FancyVerbBreakStop|, keep in mind that any groups |{...}| or math |$...$| between the two commands will be skipped as far as line breaks are concerned, and breaks may be inserted within any optional arguments |[...]| depending on settings. Inserting breaks within groups requires another level of |\FancyVerbBreakStart| and |\FancyVerbBreakStop|, and protecting optional arguments requires wrapping the entire macro in a group |{...}|. Also, keep in mind that |\FancyVerbBreakStart| cannot introduce line breaks in a context in which they are never allowed, such as in an |\hbox|.
1381 %
1382 %
1383 % \subsubsection{Customizing break behavior}
1384 %
1385 % \DescribeMacro{\FancyVerbBreakAnywhereBreak}
1386 % \DescribeMacro{\FancyVerbBreakBeforeBreak}
1387 % \DescribeMacro{\FancyVerbBreakAfterBreak}
1388 %
1389 % These macros govern the behavior of breaks introduced by |breakanywhere|, |breakbefore|, and |breakafter|. These do not apply to breaks inserted next to spaces; see |spacebreak|.
1390 %
1391 % By default, these macros use |\discretionary|. |\discretionary| takes three arguments: commands to insert before the break, commands to insert after the break, and commands to insert if there is no break. For example, the default definition of |\FancyVerbBreakAnywhereBreak|:
1392 %
1393 %\begin{verbatim}
1394 %\newcommand{\FancyVerbBreakAnywhereBreak}{%
1395 % \discretionary{\FancyVerbBreakAnywhereSymbolPre}%
1396 % {\FancyVerbBreakAnywhereSymbolPost}{}}
1397 %\end{verbatim}
1398 % The other macros are equivalent, except that ``|Anywhere|'' is swapped for ``|Before|'' or ``|After|''.
1399 %
1400 % |\discretionary| will generally only insert breaks when breaking at spaces simply cannot make lines short enough (this may be tweaked to some extent with hyphenation settings). This can produce a somewhat ragged appearance in some cases. If you want breaks exactly at the margin (or as close as possible) regardless of whether a break at a space is an option, you may want to use |\allowbreak| instead. Another option is |\linebreak[|\meta{n}|]|, where \meta{n} is between 0 to 4, with 0 allowing a break and 4 forcing a break.
1401 %
1402 %
1403 %
1404 %
1405 % \section{Pygments support}
1406 % \label{sec:pygments}
1407 %
1408 %
1409 % \subsection{Options for users}
1410 % \label{sec:pygments:users}
1411 %
1412 % \fvextra\ defines additional options for working code that has been highlighted with \href{pygments.org}{Pygments}. These options work with the \pkg{minted} and \pkg{pythontex} packages, and may be enabled for other packages that work with Pygments output (\cref{sec:pygments:package-authors}).
1413 %
1414 %
1415 % \begin{optionlist}
1416 % \item[breakbytoken (boolean) (false)]
1417 % When |breaklines=true|, do not allow breaks within \href{http://pygments.org/docs/tokens/}{Pygments tokens}. This would prevent, for example, line breaking within strings.
1418 %
1419 % \item[breakbytokenanywhere (boolean) (false)]
1420 % When |breaklines=true|, do not allow breaks within Pygments tokens, but always allow breaks between tokens even when they are immediately adjacent (not separated by spaces). \textbf{This option should be used with care.} Due to the details of how each Pygments lexer works, and due to the tokens defined in each lexer, this may result in breaks in locations that might not be anticipated. Also keep in mind that this will not allow breaks between tokens if those tokens are actually ``subtokens'' within another token.
1421 % \end{optionlist}
1422 %
1423 % \DescribeMacro{\FancyVerbBreakByTokenAnywhereBreak}
1424 %
1425 % This defines the break inserted when |breakbytokenanywhere=true|. By default, it is |\allowbreak|.
1426 %
1427 %
1428 % \subsection{For package authors}
1429 % \label{sec:pygments:package-authors}
1430 %
1431 % By default, line breaking will only partially work with Pygments output; |breakbefore| and |breakafter| will not work with any characters that do not appear literally in Pygments output but rather are replaced with a character macro. Also, |breakbytoken| and |breakbytokenanywhere| will not function at all.
1432 %
1433 % \DescribeMacro{\VerbatimPygments\marg{literal\_macro}\marg{actual\_macro}}
1434 %
1435 % To enable full Pygments support, use this macro before \verb|\begin{Verbatim}|, etc. This macro must be used within |\begingroup...\endgroup| to prevent settings from escaping into the rest of the document. It may be used safely at the beginning of a |\newenvironment| definition. When used with |\newcommand|, though, the |\begingroup...\endgroup| will need to be inserted explicitly.
1436 %
1437 % \meta{literal\_macro} is the Pygments macro that literally appears in Pygments output; it corresponds to the Pygments |commandprefix|. For \pkg{minted} and \pkg{pythontex}, this is |\PYG|. \meta{actual\_macro} is the Pygments macro that should actually be used. For \pkg{minted} this is |\PYG|; for \pkg{pythontex} this is |\PYG|\meta{style}. In the \pkg{minted} and \pkg{pythontex} approach, code is only highlighted once (|\PYG|), and then the style is changed by redefining the macro that literally appears to use the appropriate style macro.
1438 %
1439 % |\VerbatimPygments| takes the two Pygments macros and redefines \meta{literal\_macro} so that it will invoke \meta{actual\_macro} while fully supporting line breaks, |breakbytoken|, and |breakbytokenanywhere|. No further modification of either \meta{literal\_macro} or \meta{actual\_macro} is possible after |\VerbatimPygments| is used.
1440 %
1441 % In packages that do not make a distinction between \meta{literal\_macro} and \meta{actual\_macro}, simply use |\VerbatimPygments| with two identical arguments; |\VerbatimPygments| is defined to handle this case.
1442 %
1443 % |\VerbatimPygments| also improves other features of Pygments token macros, including improving support for Pygments options |escapeinside| and |texcomments|. See the implementation for details.
1444 %
1445 %
1446 %
1447 %
1448 % \section{Patches}
1449 % \label{sec:patch}
1450 %
1451 % \fvextra\ modifies some \fancyvrb\ behavior that is the result of bugs or omissions.
1452 %
1453 % \subsection{Visible spaces}
1454 % \label{sec:patch:visible-space}
1455 %
1456 % The command |\FancyVerbSpace| defines the visible space when |showspaces=true|. The default \fancyvrb\ definition allows a font command to escape under some circumstances, so that all following text is forced to be teletype font. The command is redefined following \url{https://tex.stackexchange.com/a/120231/10742}.
1457 %
1458 %
1459 % \subsection{\texttt{obeytabs} with visible tabs and with tabs inside macro arguments}
1460 %
1461 % The original \fancyvrb\ treatment of visible tabs when |showtabs=true| and |obeytabs=true| did not allow variable-width tab symbols such as |\rightarrowfill| to function correctly. This is fixed through a redefinition of |\FV@TrueTab|.
1462 %
1463 % Various macros associated with |obeytabs=true| are also redefined so that tabs may be expanded regardless of whether they are within a group (within |{...}| with the normal \LaTeX\ meaning due to |commandchars|, etc.). In the \fancyvrb\ implementation, using |obeytabs=true| when a tab is inside a group typically causes the entire line to vanish. \fvextra\ patches this so that the tab is expanded and will be visible if |showtabs=true|. Note, though, that the tab expansion in these cases is only guaranteed to be correct for leading whitespace that is inside a group. The start of each run of whitespace that is inside a group is treated as a tab stop, whether or not it actually is, due to limitations of the tab expansion algorithm. A more detailed discussion is provided in the implementation.
1464 %
1465 % The example below shows correct tab expansion of leading whitespace within a macro argument. With \fancyvrb, the line of text would simply vanish in this case.
1466 %
1467 % \begingroup
1468 % \fvset{frame=single, rulecolor=DarkGreen, gobble=4}
1469 % \begin{VerbatimVerbatim}[showtabs]
1470 % \begin{Verbatim}[obeytabs, showtabs, showspaces, tabsize=4,
1471 % commandchars=\\\{\}, tab=\textcolor{orange}{\rightarrowfill}]
1472 % \textcolor{blue}{ Text after 1 space + 2 tabs}
1473 % \end{Verbatim}
1474 % \end{VerbatimVerbatim}
1475 %
1476 % \begin{Verbatim}[obeytabs, showtabs, showspaces, tabsize=4,
1477 % commandchars=\\\{\}, tab=\textcolor{orange}{\rightarrowfill}]
1478 % \textcolor{blue}{ Text after 1 space + 2 tabs}
1479 % \end{Verbatim}
1480 % \endgroup
1481 %
1482 % The next example shows that tab expansion inside macros in the midst of text typically does not match up with the correct tab stops, since in such circumstances the beginning of the run of whitespace must be treated as a tab stop.
1483 %
1484 % \begingroup
1485 % \fvset{frame=single, rulecolor=DarkGreen, gobble=4, obeytabs}
1486 % \begin{VerbatimVerbatim}[showtabs]
1487 % \begin{Verbatim}[obeytabs, showtabs, commandchars=\\\{\},
1488 % tab=\textcolor{orange}{\rightarrowfill}]
1489 % \textcolor{blue}{ 2 leading tabs}
1490 % \textcolor{blue}{Text then 2 tabs}
1491 % \end{Verbatim}
1492 % \end{VerbatimVerbatim}
1493 %
1494 % \begin{Verbatim}[obeytabs, showtabs, commandchars=\\\{\},
1495 % tab=\textcolor{orange}{\rightarrowfill}]
1496 % \textcolor{blue}{ 2 leading tabs}
1497 % \textcolor{blue}{Text then 2 tabs}
1498 % \end{Verbatim}
1499 % \endgroup
1500 %
1501 %
1502 % \subsection{Math mode}
1503 % \label{sec:patch:math}
1504 %
1505 % \subsubsection{Spaces}
1506 %
1507 % When typeset math is included within verbatim material, \fancyvrb\ makes spaces within the math appear literally.
1508 %
1509 % \begingroup
1510 % \makeatletter
1511 % \let\FV@SetupMathActive\relax
1512 % \let\FV@ApplyMathActive\relax
1513 % \def\FV@SpaceCatTen{\ }
1514 % \makeatother
1515 %\begin{longexample}
1516 %\begin{Verbatim}[commandchars=\\\{\}, mathescape]
1517 %Verbatim $\displaystyle\frac{1}{ x^2 + y^2 }$ verbatim
1518 %\end{Verbatim}
1519 %\end{longexample}
1520 % \endgroup
1521 %
1522 % \fvextra\ patches this by redefining \fancyvrb's space character within math mode so that it behaves as expected:
1523 %
1524 % \begin{Verbatim}[commandchars=\\\{\},
1525 % codes={\catcode`$=3\catcode`^=7}, gobble=4]
1526 % Verbatim $\displaystyle\frac{1}{ x^2 + y^2 }$ verbatim
1527 % \end{Verbatim}
1528 %
1529 %
1530 % \subsubsection{Symbols and fonts}
1531 %
1532 % With \fancyvrb, ASCII symbols may give errors within math or may not appear as expected. \fvextra\ redefines all ASCII symbols so that they will behave as literal characters or (if |\active| outside the verbatim context) as macros within math. This behavior is overridden for specific characters by the |codes|, |commandchars|, and |mathescape| options.
1533 %
1534 % With \fancyvrb, using a single quotation mark (\texttt{\textquotesingle}) in typeset math within verbatim material results in an error rather than a prime symbol ($'$).\footnote{The single quotation mark is made active within verbatim material to prevent ligatures, via |\@noligs|. The default definition is incompatible with math mode.} \fvextra\ redefines the behavior of the single quotation mark within math mode to fix this, so that it will become a proper prime.
1535 %
1536 % The \pkg{amsmath} package provides a |\text| command for including normal text within math. With \fancyvrb, |\text| does not behave normally when used in typeset math within verbatim material. \fvextra\ redefines the backtick (\texttt{\textasciigrave}) and the single quotation mark so that they function normally within |\text|, becoming left and right quotation marks. It redefines the greater-than sign, less-than sign, comma, and hyphen so that they function normally as well. \fvextra\ also switches back to the default document font within |\text|, rather than using the verbatim font, which is typically a monospace or typewriter font.
1537 %
1538 % The result of these modifications is a math mode that very closely mimics the behavior of normal math mode outside of verbatim material.
1539 %
1540 %\begin{longexample}
1541 %\begin{Verbatim}[commandchars=\\\{\}, mathescape]
1542 %Verbatim $\displaystyle f'''(x) = \text{``Some quoted text---''}$
1543 %\end{Verbatim}
1544 %\end{longexample}
1545 %
1546 %
1547 %
1548 % \subsection{Orphaned labels}
1549 %
1550 % When |frame=lines| is used with a |label|, \fancyvrb\ does not prevent the label from being orphaned under some circumstances. |\FV@BeginListFrame@Lines| is patched to prevent this.
1551 %
1552 %
1553 %
1554 % \subsection{\texttt{rulecolor} and \texttt{fillcolor}}
1555 %
1556 % The |rulecolor| and |fillcolor| options are redefined so that they accept color names directly, rather than requiring |\color{|\meta{color\_name}|}|. The definitions still allow the old usage.
1557 %
1558 %
1559 %
1560 % \subsection{Command lookahead tokenization}
1561 %
1562 % \cmd{\FV@Command} is used internally by commands like \cmd{\Verb} to read stars (|*|) and optional arguments (|[...]|) before invoking the core of the command. This is redefined so that lookahead tokenizes under a verbatim catcode regime. The original definition could prevent commands like \cmd{\Verb} from using characters like |%| as delimiters, because the lookahead for a star and optional argument could read the |%| and give it its normal meaning of comment character. The new definition fixes this, so that commands like \cmd{\Verb} behave as closely to \cmd{\verb} as possible.
1563 %
1564 %
1565 %
1566 %
1567 % \section{Additional modifications to \fancyvrb}
1568 % \label{sec:modifications}
1569 %
1570 % \fvextra\ modifies some \fancyvrb\ behavior with the intention of improving logical consistency or providing better defaults.
1571 %
1572 %
1573 % \subsection{Backtick and single quotation mark}
1574 %
1575 % With \fancyvrb, the backtick \texttt{\textasciigrave} and typewriter single quotation mark \texttt{\textquotesingle} are typeset as the left and right curly single quotation marks \texttt{`'}. \pkg{fvextra} loads the \pkg{upquote} package so that these characters will appear literally by default. The original \fancyvrb\ behavior can be restored with the \fvextra\ option |curlyquotes| (\cref{sec:general-options}).
1576 %
1577 %
1578 % \subsection{Line numbering}
1579 % \label{sec:modifications:line-numbering}
1580 %
1581 % With \fancyvrb, using |firstnumber| to offset line numbering in conjunction with |stepnumber| changes which line numbers appear. Lines are numbered if their original line numbers, without the |firstnumber| offset, are a multiple of |stepnumber|. But the actual numbers that appear are the offset values that include |firstnumber|. Thus, using |firstnumber=2| with |stepnumber=5| would cause the original lines $5, 10, 15, ...$ to be numbered, but with the values $6, 11, 16, ...$.
1582 %
1583 % \fvextra\ changes line numbering so that when |stepnumber| is used, the actual line numbers that appear are always multiples of |stepnumber| by default, regardless of any |firstnumber| offset. The original \fancyvrb\ behavior may be turned on by setting |stepnumberoffsetvalues=true| (\cref{sec:general-options}).
1584 %
1585 %
1586 %
1587 % \section{Undocumented features of \fancyvrb}
1588 %
1589 % \fancyvrb\ defines some potentially useful but undocumented features.
1590 %
1591 % \subsection{Undocumented option}
1592 %
1593 % \begin{optionlist}
1594 %
1595 % \item[listparameters (macro) (\meta{empty})]
1596 % Set list-related lengths to modify spacing around lines of code. For example, |listparameters=\setlength{\topsep}{0pt}| will remove space before and after a |Verbatim| environment.
1597 %
1598 % \end{optionlist}
1599 %
1600 %
1601 % \subsection{Undocumented macros}
1602 %
1603 % \DescribeMacro{\FancyVerbTab}
1604 %
1605 % This defines the visible tab character (\FancyVerbTab) that is used when |showtabs=true|. The default definition is
1606 %\begin{verbatim}
1607 %\def\FancyVerbTab{%
1608 % \valign{%
1609 % \vfil##\vfil\cr
1610 % \hbox{$\scriptscriptstyle-$}\cr
1611 % \hbox to 0pt{\hss$\scriptscriptstyle\rangle\mskip -.8mu$}\cr
1612 % \hbox{$\scriptstyle\mskip -3mu\mid\mskip -1.4mu$}\cr}}
1613 %\end{verbatim}
1614 % While this may be redefined directly, \fvextra\ also defines a new option |tab|
1615 %
1616 %
1617 % \DescribeMacro{\FancyVerbSpace}
1618 %
1619 % This defines the visible space character (\texttt{\FancyVerbSpace}) that is used when |showspaces=true|. The default definition (as patched by \fvextra, \cref{sec:patch:visible-space}) follows \url{https://tex.stackexchange.com/a/120231/10742}. While this may be redefined directly, \fvextra\ also defines a new option |space|.
1620 %
1621 %
1622 %
1623 % \StopEventually{\PrintIndex}
1624 %
1625 % \section{Implementation}
1626 %
1627 % \iffalse
1628 %<*package>
1629 % \fi
1630 %
1631 %
1632 %
1633 % \subsection{Required packages}
1634 % The \pkg{upquote} package performs some font checks when it is loaded to determine whether \pkg{textcomp} is needed, but errors can result if the font is changed later in the preamble, so duplicate the package's font check at the end of the preamble. Also check for a package order issue with \pkg{lineno} and \pkg{csquotes}.
1635 % \begin{macrocode}
1636 \RequirePackage{etoolbox}
1637 \RequirePackage{fancyvrb}
1638 \RequirePackage{upquote}
1639 \AtEndPreamble{%
1640 \ifx\encodingdefault\upquote@OTone
1641 \ifx\ttdefault\upquote@cmtt\else\RequirePackage{textcomp}\fi
1642 \else
1643 \RequirePackage{textcomp}
1644 \fi}
1645 \RequirePackage{lineno}
1646 \@ifpackageloaded{csquotes}%
1647 {\PackageWarning{fvextra}{csquotes should be loaded after fvextra, %
1648 to avoid a warning from the lineno package}}{}
1649 % \end{macrocode}
1650 %
1651 %
1652 %
1653 %
1654 % \subsection{Utility macros}
1655 %
1656 % \subsubsection{\fancyvrb\ space and tab tokens}
1657 %
1658 % \begin{macro}{\FV@ActiveSpaceToken}
1659 % Active space for |\ifx| token comparisons.
1660 % \begin{macrocode}
1661 \begingroup
1662 \catcode`\ =\active%
1663 \gdef\FV@ActiveSpaceToken{ }%
1664 \endgroup%
1665 % \end{macrocode}
1666 % \end{macro}
1667 %
1668 % \begin{macro}{\FV@SpaceCatTen}
1669 % Space with catcode 10. Used instead of \Verb[showspaces]{\ } and |\space| in some contexts to avoid issues in the event that these are redefined.
1670 % \begin{macrocode}
1671 \edef\FV@SpaceCatTen{{\detokenize{ }}}
1672 % \end{macrocode}
1673 % \end{macro}
1674 %
1675 %
1676 % \begin{macro}{\FV@FVSpaceToken}
1677 % Macro with the same definition as \fancyvrb's active space. Useful for |\ifx| comparisons, such as |\@ifnextchar| lookaheads.
1678 % \begin{macrocode}
1679 \def\FV@FVSpaceToken{\FV@Space}
1680 % \end{macrocode}
1681 % \end{macro}
1682 %
1683 %
1684 % \begin{macro}{\FV@FVTabToken}
1685 % Macro with the same definition as \fancyvrb's active tab. Useful for |\ifx| comparisons, such as |\@ifnextchar| lookaheads.
1686 % \begin{macrocode}
1687 \def\FV@FVTabToken{\FV@Tab}
1688 % \end{macrocode}
1689 % \end{macro}
1690 %
1691 %
1692 %
1693 % \subsubsection{ASCII processing}
1694 %
1695 % \begin{macro}{\FVExtraDoSpecials}
1696 % Apply \cmd{\do} to all printable, non-alphanumeric ASCII characters (codepoints |0x20| through |0x7E| except for alphanumeric characters).
1697 %
1698 % These punctuation marks and symbols are the most likely characters to be made \cmd{\active}, so it is convenient to be able to change the catcodes for all of them, not just for those in the \cmd{\dospecials} defined in |latex.ltx|:
1699 % \begin{quote}
1700 %\begin{verbatim}
1701 %\def\dospecials{\do\ \do\\\do\{\do\}\do\$\do\&%
1702 % \do\#\do\^\do\_\do\%\do\~}
1703 %\end{verbatim}
1704 % \end{quote}
1705 % If a command takes an argument delimited by a given symbol, but that symbol has been made \cmd{\active} and defined as \cmd{\outer} (perhaps it is being used as a short \cmd{\verb}), then changing the symbol's catcode is the only way to use it as a delimiter.
1706 % \begin{macrocode}
1707 \def\FVExtraDoSpecials{%
1708 \do\ \do\!\do\"\do\#\do\$\do\%\do\&\do\'\do\(\do\)\do\*\do\+\do\,\do\-%
1709 \do\.\do\/\do\:\do\;\do\<\do\=\do\>\do\?\do\@\do\[\do\\\do\]\do\^\do\_%
1710 \do\`\do\{\do\|\do\}\do\~}
1711 % \end{macrocode}
1712 % \end{macro}
1713 %
1714 %
1715 % \begin{macro}{\FV@Char@Special:<char>}
1716 % Create macros for all printable, non-alphanumeric ASCII characters. This is used in creating backslash escapes that can only be applied to ASCII symbols and punctuation; these macros serve as \cmd{\ifcsname} lookups for valid escapes.
1717 % \begin{macrocode}
1718 \begingroup
1719 \def\do#1{%
1720 \expandafter\global\expandafter
1721 \let\csname FV@Char@Special:\number`#1\endcsname\relax}
1722 \FVExtraDoSpecials
1723 \endgroup
1724 % \end{macrocode}
1725 % \end{macro}
1726 %
1727 %
1728 % \begin{macro}{\FV@Char@CatTwelve:<char>, \FV@Char@CatDetok:<char>, \FV@Char@CatActive:<char>}
1729 % Macros for accessing printable, non-alphanumeric ASCII characters with various catcodes.
1730 % \begin{macrocode}
1731 \begingroup
1732 \catcode`\!=0
1733 \catcode`\<=1
1734 \catcode`\>=2
1735 \catcode`\&=14!relax&
1736 !edef!FVCharNum<!number`!\>&
1737 !catcode`!\=12!relax&
1738 !expandafter!gdef!csname!detokenize<FV@Char@CatTwelve:>!FVCharNum!endcsname<\>&
1739 !expandafter!xdef!csname!detokenize<FV@Char@CatDetok:>!FVCharNum!endcsname<!detokenize<\>>&
1740 !catcode`!\=!active&
1741 !expandafter!gdef!csname!detokenize<FV@Char@CatActive:>!FVCharNum!endcsname<\>&
1742 !edef!FVCharNum<!number`!{>&
1743 !catcode`!{=12!relax&
1744 !expandafter!gdef!csname!detokenize<FV@Char@CatTwelve:>!FVCharNum!endcsname<{>&
1745 !expandafter!xdef!csname!detokenize<FV@Char@CatDetok:>!FVCharNum!endcsname<!detokenize<{>>&
1746 !catcode`!{=!active&
1747 !expandafter!gdef!csname!detokenize<FV@Char@CatActive:>!FVCharNum!endcsname<{>&
1748 !edef!FVCharNum<!number`!}>&
1749 !catcode`!}=12!relax&
1750 !expandafter!gdef!csname!detokenize<FV@Char@CatTwelve:>!FVCharNum!endcsname<}>&
1751 !expandafter!xdef!csname!detokenize<FV@Char@CatDetok:>!FVCharNum!endcsname<!detokenize<}>>&
1752 !catcode`!}=!active&
1753 !expandafter!gdef!csname!detokenize<FV@Char@CatActive:>!FVCharNum!endcsname<}>&
1754 !edef!FVCharNum<!number`! >&
1755 !catcode`! =12!relax&
1756 !expandafter!gdef!csname!detokenize<FV@Char@CatTwelve:>!FVCharNum!endcsname< >&
1757 !expandafter!xdef!csname!detokenize<FV@Char@CatDetok:>!FVCharNum!endcsname<!detokenize< >>&
1758 !catcode`! =!active&
1759 !expandafter!gdef!csname!detokenize<FV@Char@CatActive:>!FVCharNum!endcsname< >&
1760 !edef!FVCharNum<!number`!%>&
1761 !catcode`!%=12!relax&
1762 !expandafter!gdef!csname!detokenize<FV@Char@CatTwelve:>!FVCharNum!endcsname<%>&
1763 !expandafter!xdef!csname!detokenize<FV@Char@CatDetok:>!FVCharNum!endcsname<!detokenize<%>>&
1764 !catcode`!%=!active&
1765 !expandafter!gdef!csname!detokenize<FV@Char@CatActive:>!FVCharNum!endcsname<%>&
1766 !endgroup
1767 \begingroup
1768 \edef\FVCharNum{\number`\!}
1769 \catcode`\!=12
1770 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{!}
1771 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{!}}
1772 \catcode`\!=\active
1773 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{!}
1774 \endgroup
1775 \begingroup
1776 \edef\FVCharNum{\number`\"}
1777 \catcode`\"=12
1778 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{"}
1779 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{"}}
1780 \catcode`\"=\active
1781 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{"}
1782 \endgroup
1783 \begingroup
1784 \edef\FVCharNum{\number`\#}
1785 \catcode`\#=12
1786 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{#}
1787 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{#}}
1788 \catcode`\#=\active
1789 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{#}
1790 \endgroup
1791 \begingroup
1792 \edef\FVCharNum{\number`\$}
1793 \catcode`\$=12
1794 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{$}
1795 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{$}}
1796 \catcode`\$=\active
1797 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{$}
1798 \endgroup
1799 \begingroup
1800 \edef\FVCharNum{\number`\&}
1801 \catcode`\&=12
1802 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{&}
1803 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{&}}
1804 \catcode`\&=\active
1805 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{&}
1806 \endgroup
1807 \begingroup
1808 \edef\FVCharNum{\number`\'}
1809 \catcode`\'=12
1810 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{'}
1811 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{'}}
1812 \catcode`\'=\active
1813 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{'}
1814 \endgroup
1815 \begingroup
1816 \edef\FVCharNum{\number`\(}
1817 \catcode`\(=12
1818 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{(}
1819 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{(}}
1820 \catcode`\(=\active
1821 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{(}
1822 \endgroup
1823 \begingroup
1824 \edef\FVCharNum{\number`\)}
1825 \catcode`\)=12
1826 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{)}
1827 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{)}}
1828 \catcode`\)=\active
1829 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{)}
1830 \endgroup
1831 \begingroup
1832 \edef\FVCharNum{\number`\*}
1833 \catcode`\*=12
1834 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{*}
1835 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{*}}
1836 \catcode`\*=\active
1837 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{*}
1838 \endgroup
1839 \begingroup
1840 \edef\FVCharNum{\number`\+}
1841 \catcode`\+=12
1842 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{+}
1843 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{+}}
1844 \catcode`\+=\active
1845 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{+}
1846 \endgroup
1847 \begingroup
1848 \edef\FVCharNum{\number`\,}
1849 \catcode`\,=12
1850 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{,}
1851 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{,}}
1852 \catcode`\,=\active
1853 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{,}
1854 \endgroup
1855 \begingroup
1856 \edef\FVCharNum{\number`\-}
1857 \catcode`\-=12
1858 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{-}
1859 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{-}}
1860 \catcode`\-=\active
1861 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{-}
1862 \endgroup
1863 \begingroup
1864 \edef\FVCharNum{\number`\.}
1865 \catcode`\.=12
1866 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{.}
1867 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{.}}
1868 \catcode`\.=\active
1869 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{.}
1870 \endgroup
1871 \begingroup
1872 \edef\FVCharNum{\number`\/}
1873 \catcode`\/=12
1874 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{/}
1875 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{/}}
1876 \catcode`\/=\active
1877 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{/}
1878 \endgroup
1879 \begingroup
1880 \edef\FVCharNum{\number`\:}
1881 \catcode`\:=12
1882 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{:}
1883 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{:}}
1884 \catcode`\:=\active
1885 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{:}
1886 \endgroup
1887 \begingroup
1888 \edef\FVCharNum{\number`\;}
1889 \catcode`\;=12
1890 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{;}
1891 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{;}}
1892 \catcode`\;=\active
1893 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{;}
1894 \endgroup
1895 \begingroup
1896 \edef\FVCharNum{\number`\<}
1897 \catcode`\<=12
1898 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{<}
1899 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{<}}
1900 \catcode`\<=\active
1901 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{<}
1902 \endgroup
1903 \begingroup
1904 \edef\FVCharNum{\number`\=}
1905 \catcode`\==12
1906 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{=}
1907 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{=}}
1908 \catcode`\==\active
1909 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{=}
1910 \endgroup
1911 \begingroup
1912 \edef\FVCharNum{\number`\>}
1913 \catcode`\>=12
1914 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{>}
1915 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{>}}
1916 \catcode`\>=\active
1917 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{>}
1918 \endgroup
1919 \begingroup
1920 \edef\FVCharNum{\number`\?}
1921 \catcode`\?=12
1922 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{?}
1923 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{?}}
1924 \catcode`\?=\active
1925 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{?}
1926 \endgroup
1927 \begingroup
1928 \edef\FVCharNum{\number`\@}
1929 \catcode`\@=12
1930 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{@}
1931 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{@}}
1932 \catcode`\@=\active
1933 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{@}
1934 \endgroup
1935 \begingroup
1936 \edef\FVCharNum{\number`\[}
1937 \catcode`\[=12
1938 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{[}
1939 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{[}}
1940 \catcode`\[=\active
1941 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{[}
1942 \endgroup
1943 \begingroup
1944 \edef\FVCharNum{\number`\]}
1945 \catcode`\]=12
1946 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{]}
1947 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{]}}
1948 \catcode`\]=\active
1949 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{]}
1950 \endgroup
1951 \begingroup
1952 \edef\FVCharNum{\number`\^}
1953 \catcode`\^=12
1954 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{^}
1955 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{^}}
1956 \catcode`\^=\active
1957 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{^}
1958 \endgroup
1959 \begingroup
1960 \edef\FVCharNum{\number`\_}
1961 \catcode`\_=12
1962 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{_}
1963 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{_}}
1964 \catcode`\_=\active
1965 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{_}
1966 \endgroup
1967 \begingroup
1968 \edef\FVCharNum{\number`\`}
1969 \catcode`\`=12
1970 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{`}
1971 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{`}}
1972 \catcode`\`=\active
1973 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{`}
1974 \endgroup
1975 \begingroup
1976 \edef\FVCharNum{\number`\|}
1977 \catcode`\|=12
1978 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{|}
1979 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{|}}
1980 \catcode`\|=\active
1981 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{|}
1982 \endgroup
1983 \begingroup
1984 \edef\FVCharNum{\number`\~}
1985 \catcode`\~=12
1986 \expandafter\gdef\csname\detokenize{FV@Char@CatTwelve:}\FVCharNum\endcsname{~}
1987 \expandafter\xdef\csname\detokenize{FV@Char@CatDetok:}\FVCharNum\endcsname{\detokenize{~}}
1988 \catcode`\~=\active
1989 \expandafter\gdef\csname\detokenize{FV@Char@CatActive:}\FVCharNum\endcsname{~}
1990 \endgroup
1991 % \end{macrocode}
1992 % \end{macro}
1993 %
1994 %
1995 % \begin{macro}{\FVExtraSaveCodes,\FVExtraUseCodes,\FancyVerbRestoreCodes}
1996 % Save catcodes of all characters in |\FVExtraDoSpecials|, and then restore them. The |\FVExtra*| commands take arguments of the form \oarg{pkg}\marg{name}. |\FancyVerbRestoreCodes| is a shortcut for |\FVExtraUseCodes[fancyvrb]{BeforeVerbatim}|.
1997 % \begin{macrocode}
1998 \newcommand{\FVExtraSaveCodes}[2][]{%
1999 \ifcsname do\endcsname
2000 \let\FV@dotmp\do
2001 \fi
2002 \if\relax\detokenize{#1}\relax
2003 \def\do##1{%
2004 \expandafter
2005 \chardef\csname FV@SVCode:\number`##1@#2\endcsname=%
2006 \catcode`##1\relax}%
2007 \expandafter\let\csname FV@SVCodes@#2\endcsname\relax
2008 \else
2009 \def\do##1{%
2010 \expandafter
2011 \chardef\csname FV@PkgSVCode:\number`##1@#1:#2\endcsname=%
2012 \catcode`##1\relax}%
2013 \expandafter\let\csname FV@PkgSVCodes@#1:#2\endcsname\relax
2014 \fi
2015 \FVExtraDoSpecials
2016 \ifcsname FV@dotmp\endcsname
2017 \let\do\FV@dotmp
2018 \let\FV@dotmp\FV@Undefined
2019 \else
2020 \let\do\FV@Undefined
2021 \fi}
2022 \newcommand{\FVExtraUseCodes}[2][]{%
2023 \if\relax\detokenize{#1}\relax
2024 \expandafter\FVExtraUseCodes@NoPkg
2025 \else
2026 \expandafter\FVExtraUseCodes@Pkg
2027 \fi
2028 {#1}{#2}}
2029 \def\FVExtraUseCodes@NoPkg#1#2{%
2030 \ifcsname FV@SVCodes@#2\endcsname
2031 \expandafter\FVExtraUseCodes@NoPkg@i
2032 \else
2033 \PackageError{fvextra}%
2034 {Unknown name "#2" for saved catcodes}%
2035 {Check name of saved catcodes}%
2036 \expandafter\@gobble
2037 \fi
2038 {#2}}
2039 \def\FVExtraUseCodes@NoPkg@i#1{%
2040 \FVExtraUseCodes@end{FV@SVCode}{#1}}
2041 \def\FVExtraUseCodes@Pkg#1#2{%
2042 \ifcsname FV@PkgSVCodes@#1:#2\endcsname
2043 \expandafter\FVExtraUseCodes@Pkg@i
2044 \else
2045 \PackageError{fvextra}%
2046 {Unknown name "#2" for saved catcodes for package "#1"}%
2047 {Check name of saved catcodes}%
2048 \expandafter\@gobble
2049 \fi
2050 {#1:#2}}
2051 \def\FVExtraUseCodes@Pkg@i#1{%
2052 \FVExtraUseCodes@end{FV@PkgSVCode}{#1}}
2053 \def\FVExtraUseCodes@end#1#2{%
2054 \ifcsname do\endcsname
2055 \let\FV@dotmp\do
2056 \fi
2057 \def\do##1{%
2058 \catcode`##1=%
2059 \csname #1:\number`##1@#2\endcsname\relax}%
2060 \FVExtraDoSpecials
2061 \ifcsname FV@dotmp\endcsname
2062 \let\do\FV@dotmp
2063 \let\FV@dotmp\FV@Undefined
2064 \else
2065 \let\do\FV@Undefined
2066 \fi}
2067 \AtEndOfPackage{%
2068 \FV@AddToHook{\FV@UseKeyValues@Hook}{\FVExtraSaveCodes[fancyvrb]{BeforeVerbatim}}}
2069 \def\FancyVerbRestoreCodes{\FVExtraUseCodes[fancyvrb]{BeforeVerbatim}}
2070 % \end{macrocode}
2071 % \end{macro}
2072 %
2073 %
2074 %
2075 % \subsubsection{Sentinels}
2076 %
2077 % Sentinel macros are needed for scanning tokens.
2078 %
2079 % There are two contexts in which sentinels may be needed. In delimited macro arguments, such as |\def\macro#1\sentinel{...}|, a sentinel is needed as the delimiter. Because the delimiting macro need not be defined, special delimiting macros need not be created for this case. The important thing is to ensure that the macro name is sufficiently unique to avoid collisions. Typically, using \cmd{\makeatletter} to allow something like \cmd{\@sentinel} will be sufficient. For added security, additional characters can be given catcode 11, to allow things like \cmd{\@sent!nel}.
2080 %
2081 % The other context for sentinels is in scanning through a sequence of tokens that is delimited by a sentinel, and using \cmd{\ifx} comparisons to identify the sentinel and stop scanning. In this case, using an undefined macro is risky. Under normal conditions, the sequence of tokens could contain an undefined macro due to mistyping. In some \fvextra\ applications, the tokens will have been incorrectly tokenized under a normal catcode regime, and need to be retokenized as verbatim, in which case undefined macros must be expected. Thus, a sentinel macro whose expansion is resistant to collisions is needed.
2082 %
2083 % \begin{macro}{\FV@<Sentinel>}
2084 % This is the standard default \fvextra\ delimited-macro sentinel. It is used with \cmd{\makeatletter} by changing |<| and |>| to catcode 11. The |<| and |>| add an extra level of collision resistance. Because it is undefined, it is \emph{only} appropriate for use in delimited macro arguments.
2085 % \end{macro}
2086 %
2087 % \begin{macro}{\FV@Sentinel}
2088 % This is the standard \fvextra\ \cmd{\ifx} comparison sentinel. It expands to the control word \cmd{\FV@<Sentinel>}, which is very unlikely to be in any other macro since it requires that |@|, |<|, and |>| all have catcode 11 and appear in the correct sequence. Because its definition is itself undefined, this sentinel will result in an error if it escapes.
2089 % \begin{macrocode}
2090 \begingroup
2091 \catcode`\<=11
2092 \catcode`\>=11
2093 \gdef\FV@Sentinel{\FV@<Sentinel>}
2094 \endgroup
2095 % \end{macrocode}
2096 % \end{macro}
2097 %
2098 %
2099 % \subsubsection{Active character definitions}
2100 %
2101 % \begin{macro}{\FV@OuterDefEOLEmpty}
2102 % Macro for defining the active end-of-line character |^^M| (|\r|), which \fancyvrb\ uses to prevent runaway command arguments. \fancyvrb\ uses macro definitions of the form
2103 %\begin{verbatim}
2104 %\begingroup
2105 %\catcode`\^^M=\active%
2106 %\gdef\macro{%
2107 % ...
2108 % \outer\def^^M{}%
2109 % ...
2110 %}%
2111 %\endgroup
2112 %\end{verbatim}
2113 % While this works, it is nice to avoid the |\begingroup...\endgroup| and especially the requirement that all lines now end with |%| to discard the |^^M| that would otherwise be inserted.
2114 % \begin{macrocode}
2115 \begingroup
2116 \catcode`\^^M=\active%
2117 \gdef\FV@OuterDefEOLEmpty{\outer\def^^M{}}%
2118 \endgroup
2119 % \end{macrocode}
2120 % \end{macro}
2121 %
2122 % \begin{macro}{\FV@DefEOLEmpty}
2123 % The same thing, without the \cmd{\outer}. This is used to ensure that |^^M| is not \cmd{\outer} when it should be read.
2124 % \begin{macrocode}
2125 \begingroup
2126 \catcode`\^^M=\active%
2127 \gdef\FV@DefEOLEmpty{\def^^M{}}%
2128 \endgroup
2129 % \end{macrocode}
2130 % \end{macro}
2131 %
2132 %
2133 % \begin{macro}{\FV@OuterDefSTXEmpty}
2134 % Define start-of-text (STX) |^^B| so that it cannot be used inside other macros. This makes it possible to guarantee that |^^B| is not part of a verbatim argument, so that it can be used later as a sentinel in retokenizing the argument.
2135 % \begin{macrocode}
2136 \begingroup
2137 \catcode`\^^B=\active
2138 \gdef\FV@OuterDefSTXEmpty{\outer\def^^B{}}
2139 \endgroup
2140 % \end{macrocode}
2141 % \end{macro}
2142 %
2143 %
2144 % \begin{macro}{\FV@OuterDefETXEmpty}
2145 % Define end-of-text (ETX) |^^C| so that it cannot be used inside other macros. This makes it possible to guarantee that |^^C| is not part of a verbatim argument, so that it can be used later as a sentinel in retokenizing the argument.
2146 % \begin{macrocode}
2147 \begingroup
2148 \catcode`\^^C=\active
2149 \gdef\FV@OuterDefETXEmpty{\outer\def^^C{}}
2150 \endgroup
2151 % \end{macrocode}
2152 % \end{macro}
2153 %
2154 %
2155 %
2156 %
2157 % \subsection{pdfTeX with \pkg{inputenc} using UTF-8}
2158 %
2159 % Working with verbatim text often involves handling individual code points. While these are treated as single entities under LuaTeX and XeTeX, under pdfTeX code points must be handled at the byte level instead. This means that reading a single code point encoded in UTF-8 may involve a macro that reads up to four arguments.
2160 %
2161 % Macros are defined for working with non-ASCII code points under pdfTeX. These are only for use with the \pkg{inputenc} package set to |utf8| encoding.
2162 %
2163 % \begin{macro}{\ifFV@pdfTeXinputenc}
2164 % All of the |UTF| macros are only needed with pdfTeX when \pkg{inputenc} is loaded, so they are created conditionally, inspired by the approach of the \pkg{iftex} package. The tests deal with the possibility that a previous test using |\ifx| rather than the cleaner |\ifcsname| has already been performed. These assume that \pkg{inputenc} will be loaded before \fvextra. The \cmd{\inputencodingname} tests should be redundant after the \cmd{\@ifpackageloaded} test, but do provide some additional safety if another package is faking \pkg{inputenc} being loaded but not providing an equivalent encoding interface.
2165 %
2166 % Note that an encoding test of the form
2167 % \begin{quote}
2168 %\begin{verbatim}
2169 %\ifdefstring{\inputencodingname}{utf8}{<true>}{<false>}
2170 %\end{verbatim}
2171 % \end{quote}
2172 % is still required before switching to the |UTF| variants in any given situation. A document using \pkg{inputenc} can switch encodings (for example, around an \cmd{\input}), so simply checking encoding when \fvextra\ is loaded is \emph{not} sufficient.
2173 % \begin{macrocode}
2174 \newif\ifFV@pdfTeXinputenc
2175 \FV@pdfTeXinputencfalse
2176 \ifcsname pdfmatch\endcsname
2177 \ifx\pdfmatch\relax
2178 \else
2179 \@ifpackageloaded{inputenc}%
2180 {\ifcsname inputencodingname\endcsname
2181 \ifx\inputencodingname\relax
2182 \else
2183 \FV@pdfTeXinputenctrue
2184 \fi\fi}
2185 {}%
2186 \fi\fi
2187 % \end{macrocode}
2188 % \end{macro}
2189 %
2190 % Define |UTF| macros conditionally:
2191 % \begin{macrocode}
2192 \ifFV@pdfTeXinputenc
2193 % \end{macrocode}
2194 %
2195 %
2196 % \begin{macro}{\FV@U8:<byte>}
2197 % Define macros of the form |\FV@U8:<byte>| for each active byte. These are used for determining whether a token is the first byte in a multi-byte sequence, and if so, invoking the necessary macro to capture the remaining bytes. The code is adapted from the beginning of |utf8.def|. Completely capitalized macro names are used to avoid having to worry about |\uppercase|.
2198 % \begin{macrocode}
2199 \begingroup
2200 \catcode`\~=13
2201 \catcode`\"=12
2202 \def\FV@UTFviii@loop{%
2203 \uccode`\~\count@
2204 \uppercase\expandafter{\FV@UTFviii@Tmp}%
2205 \advance\count@\@ne
2206 \ifnum\count@<\@tempcnta
2207 \expandafter\FV@UTFviii@loop
2208 \fi}
2209 % \end{macrocode}
2210 % Setting up 2-byte UTF-8:
2211 % \begin{macrocode}
2212 \count@"C2
2213 \@tempcnta"E0
2214 \def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{%
2215 \FV@UTF@two@octets}}
2216 \FV@UTFviii@loop
2217 % \end{macrocode}
2218 % Setting up 3-byte UTF-8:
2219 % \begin{macrocode}
2220 \count@"E0
2221 \@tempcnta"F0
2222 \def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{%
2223 \FV@UTF@three@octets}}
2224 \FV@UTFviii@loop
2225 % \end{macrocode}
2226 % Setting up 4-byte UTF-8:
2227 % \begin{macrocode}
2228 \count@"F0
2229 \@tempcnta"F4
2230 \def\FV@UTFviii@Tmp{\expandafter\gdef\csname FV@U8:\string~\endcsname{%
2231 \FV@UTF@four@octets}}
2232 \FV@UTFviii@loop
2233 \endgroup
2234 % \end{macrocode}
2235 % \end{macro}
2236 %
2237 %
2238 % \begin{macro}{\FV@UTF@two@octets}%
2239 % \begin{macro}{\FV@UTF@three@octets}%
2240 % \begin{macro}{\FV@UTF@four@octets}
2241 % These are variants of the |utf8.def| macros that capture all bytes of a multi-byte code point and then pass them on to \cmd{\FV@UTF@octets@after} as a single argument for further processing. The invoking macro should \cmd{\let} or \cmd{\def}'ed \cmd{\FV@UTF@octets@after} to an appropriate macro that performs further processing.
2242 %
2243 % Typical use will involve the following steps:
2244 % \begin{enumerate}
2245 % \item Read a token, say |#1|.
2246 % \item Use |\ifcsname FV@U8:\detokenize{#1}\endcsname| to determine that the token is the first byte of a multi-byte code point.
2247 % \item Ensure that |\FV@UTF@octets@after| has an appropriate value, if this has not already been done.
2248 % \item Use |\csname FV@U8:\detokenize{#1}\endcsname#1| at the end of the original reading macro to read the full multi-byte code point and then pass it on as a single argument to |\FV@UTF@octets@after|.
2249 % \end{enumerate}
2250 %
2251 % All code points are checked for validity here so as to raise errors as early as possible. Otherwise an invalid terminal byte sequence might gobble a sentinel macro in a scanning context, potentially making debugging much more difficult. It would be possible to use |\UTFviii@defined{|\meta{bytes}|}| to trigger an error directly, but the current approach is to attempt to typeset invalid code points, which should trigger errors without relying on the details of the |utf8.def| implementation.
2252 % \begin{macrocode}
2253 \def\FV@UTF@two@octets#1#2{%
2254 \ifcsname u8:\detokenize{#1#2}\endcsname
2255 \else
2256 #1#2%
2257 \fi
2258 \FV@UTF@octets@after{#1#2}}
2259 \def\FV@UTF@three@octets#1#2#3{%
2260 \ifcsname u8:\detokenize{#1#2#3}\endcsname
2261 \else
2262 #1#2#3%
2263 \fi
2264 \FV@UTF@octets@after{#1#2#3}}
2265 \def\FV@UTF@four@octets#1#2#3#4{%
2266 \ifcsname u8:\detokenize{#1#2#3#4}\endcsname
2267 \else
2268 #1#2#3#4%
2269 \fi
2270 \FV@UTF@octets@after{#1#2#3#4}}
2271 % \end{macrocode}
2272 % \end{macro}\end{macro}\end{macro}
2273 %
2274 %
2275 % End conditional creation of |UTF| macros:
2276 % \begin{macrocode}
2277 \fi
2278 % \end{macrocode}
2279 %
2280 %
2281 %
2282 %
2283 % \subsection{Reading and processing command arguments}
2284 %
2285 % \fvextra\ provides macros for reading and processing verbatim arguments. These are primarily intended for creating commands that take verbatim arguments but can still be used within other commands (with some limitations). These macros are used in reimplementing \fancyvrb\ commands like \cmd{\Verb}. They may also be used in other packages; \pkg{minted} and \pkg{pythontex} use them for handling inline code.
2286 %
2287 % All macros meant for internal use have names of the form \cmd{\FV@<Name>}, while all macros meant for use in other packages have names of the form \cmd{\FVExtra<Name>}. Only the latter are intended to have a stable interface.
2288 %
2289 %
2290 % \subsubsection{Tokenization and lookahead}
2291 %
2292 %
2293 % \begin{macro}{\FVExtra@ifnextcharAny}
2294 % A version of |\@ifnextchar| that can detect any character, including catcode 10 spaces. This is an exact copy of the definition from |latex.ltx|, modified with the ``|\let\reserved@d= #1%|'' (note space!) trick from \pkg{amsgen}.
2295 % \begin{macrocode}
2296 \long\def\FVExtra@ifnextcharAny#1#2#3{%
2297 \let\reserved@d= #1%
2298 \def\reserved@a{#2}%
2299 \def\reserved@b{#3}%
2300 \futurelet\@let@token\FVExtra@ifnchAny}
2301 \def\FVExtra@ifnchAny{%
2302 \ifx\@let@token\reserved@d
2303 \expandafter\reserved@a
2304 \else
2305 \expandafter\reserved@b
2306 \fi}
2307 % \end{macrocode}
2308 % \end{macro}
2309
2310 %
2311 % \begin{macro}{\FVExtra@ifnextcharVArg}
2312 % This is a wrapper for \cmd{\@ifnextchar} from |latex.ltx| (|ltdefns.dtx|) that tokenizes lookaheads under a mostly verbatim catcode regime rather than the current catcode regime. This is important when looking ahead for stars |*| and optional argument delimiters~|[|, because if these are not present when looking ahead for a verbatim argument, then the first thing tokenized will be the verbatim argument's delimiting character. Ideally, the delimiter should be tokenized under a verbatim catcode regime. This is necessary for instance if the delimiter is \cmd{\active} and \cmd{\outer}.
2313 %
2314 % The catcode of the space is preserved (in the unlikely event it is \cmd{\active}) and curly braces are given their normal catcodes for the lookahead. This simplifies space handling in an untokenized context, and allows paired curly braces to be used as verbatim delimiters.
2315 % \begin{macrocode}
2316 \long\def\FVExtra@ifnextcharVArg#1#2#3{%
2317 \begingroup
2318 \edef\FV@TmpSpaceCat{\the\catcode` }%
2319 \let\do\@makeother\FVExtraDoSpecials
2320 \catcode`\ =\FV@TmpSpaceCat\relax
2321 \catcode`\{=1
2322 \catcode`\}=2
2323 \@ifnextchar#1{\endgroup#2}{\endgroup#3}}
2324 % \end{macrocode}
2325 % \end{macro}
2326 %
2327 %
2328 % \begin{macro}{\FVExtra@ifstarVArg}
2329 % A starred command behaves differently depending on whether it is followed by an optional star or asterisk |*|. \cmd{\@ifstar} from |latex.ltx| is typically used to check for the |*|. In the process, it discards following spaces (catcode 10) and tokenizes the next non-space character under the current catcode regime. While this is fine for normal commands, it is undesirable if the next character turns out to be not a |*| but rather a verbatim argument's delimiter. This reimplementation prevents such issues for all printable ASCII symbols via \cmd{\FVExtra@ifnextcharVArg}.
2330 % \begin{macrocode}
2331 \begingroup
2332 \catcode`\*=12
2333 \gdef\FVExtra@ifstarVArg#1{\FVExtra@ifnextcharVArg*{\@firstoftwo{#1}}}
2334 \endgroup
2335 % \end{macrocode}
2336 % \end{macro}
2337 %
2338 %
2339 % \subsubsection{Reading arguments}
2340 %
2341 % \begin{macro}{\FV@ReadOArgContinue}
2342 % Read a macro followed by an optional argument, then pass the optional argument to the macro for processing and to continue.
2343 % \begin{macrocode}
2344 \def\FV@ReadOArgContinue#1[#2]{#1{#2}}
2345 % \end{macrocode}
2346 % \end{macro}
2347 %
2348 %
2349 % \begin{macro}{\FVExtraReadOArgBeforeVArg}
2350 % Read an optional argument that comes before a verbatim argument. The lookahead for the optional argument tokenizes with a verbatim catcode regime in case it encounters the delimiter for the verbatim argument rather than |[|. If the lookahead doesn't find |[|, the optional argument for \cmd{\FVExtraReadOArgBeforeVArg} can be used to supply a default optional argument other than \meta{empty}.
2351 % \begin{macrocode}
2352 \newcommand{\FVExtraReadOArgBeforeVArg}[2][]{%
2353 \FVExtra@ifnextcharVArg[%
2354 {\FV@ReadOArgContinue{#2}}%
2355 {\FV@ReadOArgContinue{#2}[#1]}}
2356 % \end{macrocode}
2357 % \end{macro}
2358 %
2359 %
2360 % \begin{macro}{\FVExtraReadOArgBeforeVEnv}
2361 % Read an optional argument at the start of a verbatim environment, after the |\begin{|\meta{environment}|}| but before the start of the next line where the verbatim content begins. Check for extraneous content after the optional argument and discard the following newline. Note that this is not needed when an environment takes a mandatory argument that follows the optional argument.
2362 %
2363 % The case with only an optional argument is tricky because the default behavior of \cmd{\@ifnextchar} is to read into the next line looking for the optional argument. Setting |^^M| as \cmd{\active} prevents this. That does mean, though, that the end-of-line token will have to be read and removed later as an \cmd{\active} |^^M|.
2364 %
2365 % \cmd{\@ifnextchar} is used instead of \cmd{\FVExtra@ifnextcharVArg} because the latter is not needed since there is an explicit, required delimiter (|^^M|) before the actual start of verbatim content. Lookahead can never tokenize verbatim content under an incorrect catcode regime.
2366 % \begin{macrocode}
2367 \newcommand{\FVExtraReadOArgBeforeVEnv}[2][]{%
2368 \begingroup
2369 \catcode`\^^M=\active
2370 \@ifnextchar[%
2371 {\endgroup\FVExtraReadOArgBeforeVEnv@i{#2}}%
2372 {\endgroup\FVExtraReadOArgBeforeVEnv@i{#2}[#1]}}
2373 \def\FVExtraReadOArgBeforeVEnv@i#1[#2]{%
2374 \begingroup
2375 \catcode`\^^M=\active
2376 \FVExtraReadOArgBeforeVEnv@ii{#1}{#2}}
2377 \begingroup
2378 \catcode`\^^M=\active%
2379 \gdef\FVExtraReadOArgBeforeVEnv@ii#1#2#3^^M{%
2380 \endgroup%
2381 \FVExtraReadOArgBeforeVEnv@iii{#1}{#2}{#3}}%
2382 \endgroup%
2383 \def\FVExtraReadOArgBeforeVEnv@iii#1#2#3{%
2384 \if\relax\detokenize{#3}\relax
2385 \else
2386 \PackageError{fvextra}%
2387 {Discarded invalid text while checking for optional argument of verbatim environment}%
2388 {Discarded invalid text while checking for optional argument of verbatim environment}%
2389 \fi
2390 #1{#2}}
2391 % \end{macrocode}
2392 % \end{macro}
2393 %
2394 %
2395 % \begin{macro}{\FVExtraReadVArg,\FVExtraReadVArgSingleLine}
2396 % Read a verbatim argument that is bounded by two identical characters or by paired curly braces. There are two variants: one reads a multi-line (but not multi-paragraph) argument, while the other restricts the argument to a single line via the \cmd{\outer} |^^M| trick from \fancyvrb. An \cmd{\outer} |^^C| is used to prevent |^^C| from being part of arguments, so that it can be used later as a sentinel if retokenization is needed. |^^B| is handled in the same manner for symmetry with later usage, though technically it is not used as a sentinel so this is not strictly necessary. Alternate |UTF| macros, defined later, are invoked when under pdfTeX with \pkg{inputenc} using UTF-8.
2397 %
2398 % The lookahead for the type of delimiting character is done under a verbatim catcode regime, except that the space catcode is preserved and curly braces are given their normal catcodes. This provides consistency with any \cmd{\FVExtra@ifnextcharVArg} or \cmd{\FVExtra@ifstarVArg} that may have been used previously, allows characters like |#| and |%| to be used as delimiters when the verbatim argument is read outside any other commands (untokenized), and allows paired curly braces to serve as delimiters. Any additional command-specific catcode modifications should only be applied to the argument after it has been read, since they do not apply to the delimiters.
2399 %
2400 % Once the delimiter lookahead is complete, catcodes revert to full verbatim, and are then modified appropriately given the type of delimiter. The space and tab must be \cmd{\active} to be preserved correctly when the verbatim argument is not inside any other commands (otherwise, they collapse into single spaces).
2401 %
2402 % Note that |\FVExtraReadVArg| will interpret a line break as a catcode 10 space, not as an |\active| space. Depending on usage, the argument may need to be processed with |\FVExtraDetokenizeVArg| and |\FVExtraRetokenizeVArg| to fix this.
2403 % \begin{macrocode}
2404 \def\FVExtraReadVArg#1{%
2405 \begingroup
2406 \ifFV@pdfTeXinputenc
2407 \ifdefstring{\inputencodingname}{utf8}%
2408 {\let\FV@ReadVArg@Char\FV@ReadVArg@Char@UTF}%
2409 {}%
2410 \fi
2411 \edef\FV@TmpSpaceCat{\the\catcode` }%
2412 \let\do\@makeother\FVExtraDoSpecials
2413 \catcode`\^^B=\active
2414 \FV@OuterDefSTXEmpty
2415 \catcode`\^^C=\active
2416 \FV@OuterDefETXEmpty
2417 \begingroup
2418 \catcode`\ =\FV@TmpSpaceCat\relax
2419 \catcode`\{=1
2420 \catcode`\}=2
2421 \@ifnextchar\bgroup
2422 {\endgroup
2423 \catcode`\{=1
2424 \catcode`\}=2
2425 \catcode`\ =\active
2426 \catcode`\^^I=\active
2427 \FV@ReadVArg@Group{#1}}%
2428 {\endgroup
2429 \catcode`\ =\active
2430 \catcode`\^^I=\active
2431 \FV@ReadVArg@Char{#1}}}
2432 \def\FVExtraReadVArgSingleLine#1{%
2433 \begingroup
2434 \ifFV@pdfTeXinputenc
2435 \ifdefstring{\inputencodingname}{utf8}%
2436 {\let\FV@ReadVArg@Char\FV@ReadVArg@Char@UTF}%
2437 {}%
2438 \fi
2439 \edef\FV@TmpSpaceCat{\the\catcode` }%
2440 \let\do\@makeother\FVExtraDoSpecials
2441 \catcode`\^^B=\active
2442 \FV@OuterDefSTXEmpty
2443 \catcode`\^^C=\active
2444 \FV@OuterDefETXEmpty
2445 \catcode`\^^M=\active
2446 \FV@OuterDefEOLEmpty
2447 \begingroup
2448 \catcode`\ =\FV@TmpSpaceCat\relax
2449 \catcode`\{=1
2450 \catcode`\}=2
2451 \@ifnextchar\bgroup
2452 {\endgroup
2453 \catcode`\{=1
2454 \catcode`\}=2
2455 \catcode`\ =\active
2456 \catcode`\^^I=\active
2457 \FV@ReadVArg@Group{#1}}%
2458 {\endgroup
2459 \catcode`\ =\active
2460 \catcode`\^^I=\active
2461 \FV@ReadVArg@Char{#1}}}
2462 % \end{macrocode}
2463 % \end{macro}
2464 %
2465 % \begin{macro}{\FV@ReadVArg@Group}
2466 % The argument is read under the verbatim catcode regime already in place from \cmd{\FVExtraReadVArg}. The \cmd{\endgroup} returns to prior catcodes. Any command-specific catcodes can be applied later via \cmd{\scantokens}. Using them here in reading the argument would have no effect as far as later processing with \cmd{\scantokens} is concerned, unless the argument were read outside any other commands and additional characters were given catcodes 1 or 2 (like the curly braces). That scenario is not allowed because it makes reading the argument overly dependent on the argument content. (Technically, reading the argument is already dependent on the argument content in the sense that the argument cannot contain unescaped unpaired curly braces, given that it is delimited by curly braces.)
2467 % \begin{macrocode}
2468 \def\FV@ReadVArg@Group#1#2{%
2469 \endgroup
2470 #1{#2}}
2471 % \end{macrocode}
2472 % \end{macro}
2473 %
2474 % \begin{macro}{\FV@ReadVArg@Char}
2475 % The delimiting character is read under the verbatim catcode regime in place from \cmd{\FVExtraReadVArg}. If the command is not inside a normal command, then this means the delimiting character will typically have catcode 12 and that characters like |#| and |%| can be used as delimiters; otherwise, the delimiter may have any catcode that is possible for a single character captured by a macro. If the argument is read inside another command (already tokenized), then it is possible for the delimiter to be a control sequence rather than a singler character. An error is raised in this case. The \cmd{\endgroup} in \cmd{\FV@ReadVArg@Char@i} returns to prior catcodes after the argument is captured.
2476 %
2477 % It would be possible to read the argument using any command-specific catcode settings, but that would result in different behavior depending on whether the argument is already tokenized, and would make reading the argument overly dependent on the argument content.
2478 % \begin{macrocode}
2479 \def\FV@ReadVArg@Char#1#2{%
2480 \expandafter\expandafter\expandafter
2481 \if\expandafter\expandafter\expandafter\relax\expandafter\@gobble\detokenize{#2}\relax
2482 \expandafter\@gobble
2483 \else
2484 \expandafter\@firstofone
2485 \fi
2486 {\PackageError{fvextra}%
2487 {Verbatim delimiters must be single characters, not commands}%
2488 {Try a different delimiter}}%
2489 \def\FV@ReadVArg@Char@i##1##2#2{%
2490 \endgroup
2491 ##1{##2}}%
2492 \FV@ReadVArg@Char@i{#1}}%
2493 % \end{macrocode}
2494 % \end{macro}
2495 %
2496 %
2497 % \paragraph{Alternate implementation for pdfTeX with \pkg{inputenc} using UTF-8}\hfill\\
2498 %
2499 % Start conditional creation of macros:
2500 % \begin{macrocode}
2501 \ifFV@pdfTeXinputenc
2502 % \end{macrocode}
2503 %
2504 % \begin{macro}{\FV@ReadVArg@Char@UTF}
2505 % This is a variant of \cmd{\FV@ReadVArg@Char} that allows non-ASCII codepoints as delimiters under the pdfTeX engine with \pkg{inputenc} using UTF-8. Under pdfTeX, non-ASCII codepoints must be handled as a sequence of bytes rather than as a single entity. \cmd{\FV@ReadVArg@Char} is automatically \cmd{\let} to this version when appropriate. This uses the \cmd{\FV@U8:<byte>} macros for working with \pkg{inputenc}'s UTF-8.
2506 % \begin{macrocode}
2507 \def\FV@ReadVArg@Char@UTF#1#2{%
2508 \expandafter\expandafter\expandafter
2509 \if\expandafter\expandafter\expandafter\relax\expandafter\@gobble\detokenize{#2}\relax
2510 \expandafter\@gobble
2511 \else
2512 \expandafter\@firstofone
2513 \fi
2514 {\PackageError{fvextra}%
2515 {Verbatim delimiters must be single characters, not commands}%
2516 {Try a different delimiter}}%
2517 \ifcsname FV@U8:\detokenize{#2}\endcsname
2518 \expandafter\@firstoftwo
2519 \else
2520 \expandafter\@secondoftwo
2521 \fi
2522 {\def\FV@UTF@octets@after##1{\FV@ReadVArg@Char@UTF@i{#1}{##1}}%
2523 \csname FV@U8:\detokenize{#2}\endcsname#2}%
2524 {\FV@ReadVArg@Char@UTF@i{#1}{#2}}}
2525 % \end{macrocode}
2526 %
2527 % \begin{macro}{\FV@ReadVArg@Char@UTF@i}
2528 % \begin{macrocode}
2529 \def\FV@ReadVArg@Char@UTF@i#1#2{%
2530 \def\FV@ReadVArg@Char@i##1##2#2{%
2531 \endgroup
2532 ##1{##2}}%
2533 \FV@ReadVArg@Char@i{#1}}%
2534 % \end{macrocode}
2535 % \end{macro}
2536 %
2537 % End conditional creation of |UTF| macros:
2538 % \begin{macrocode}
2539 \fi
2540 % \end{macrocode}
2541 % \end{macro}
2542 %
2543 %
2544 % \begin{macro}{vargsingleline}
2545 % This determines whether |\Verb| and |\SaveVerb| use |\FVExtraReadVArg| or |\FVExtraReadVArgSingleLine| to read their arguments. It has no effect on |\EscVerb|, since that does not use special tokenization.
2546 % \begin{macrocode}
2547 \newbool{FV@vargsingleline}
2548 \define@booleankey{FV}{vargsingleline}%
2549 {\booltrue{FV@vargsingleline}}
2550 {\boolfalse{FV@vargsingleline}}
2551 \fvset{vargsingleline=false}
2552 % \end{macrocode}
2553 % \end{macro}
2554 %
2555 %
2556 %
2557 %
2558 % \subsubsection{Reading and protecting arguments in expansion-only contexts}
2559 %
2560 % The objective here is to make possible commands that can function correctly after being in expansion-only contexts like \cmd{\edef}. The general strategy is to allow commands to be defined like this:
2561 %\begin{verbatim}
2562 %\def\cmd{\FVExtraRobustCommand\robustcmd\reader}
2563 %\end{verbatim}
2564 % |\robustcmd| is the actual command, including argument reading and processing, and is \cmd{\protected}. |\reader| is an expandable macro that reads all of |\robustcmd|'s arguments, then wraps them in \cmd{\FVExtraAlwaysUnexpanded}. When |\FVExtraAlwaysUnexpanded{|\meta{args}|}| is expanded, the result is always |\FVExtraAlwaysUnexpanded{|\meta{args}|}|. |\FVExtraRobustCommand| is \cmd{\protected} and manages everything in a context-sensitive manner.
2565 % \begin{itemize}
2566 % \item In a normal context, \cmd{\FVExtraRobustCommand} reads two arguments, which will be |\robustcmd| and |\reader|. It detects that |\reader| has not expanded to |\FVExtraAlwaysUnexpanded{|\meta{args}|}|, so it discards |\reader| and reinserts |\robustcmd| so that it can operate normally.
2567 % \item In an expansion-only context, neither \cmd{\FVExtraRobustCommand} nor |\robustcmd| will expand, because both are \cmd{\protected}. |\reader| will read |\robustcmd|'s arguments and protect them with \cmd{\FVExtraAlwaysUnexpanded}. When this is used later in a normal context, \cmd{\FVExtraRobustCommand} reads two arguments, which will be |\robustcmd| and \cmd{\FVExtraAlwaysUnexpanded}. It detects that |\reader| did expand, so it discards \cmd{\FVExtraAlwaysUnexpanded} and reads its argument to discard the wrapping braces. Then it reinserts |\robustcmd|\meta{args} so that everything can proceed as if expansion had not occurred.
2568 % \end{itemize}
2569 %
2570 % \begin{macro}{\FVExtrapdfstringdef}
2571 % \begin{macro}{\FVExtrapdfstringdefDisableCommands}
2572 % Conditionally allow alternate definitions for PDF bookmarks when \pkg{hyperref} is in use. This is helpful for working with \cmd{\protected} or otherwise unexpandable commands.
2573 % \begin{macrocode}
2574 \def\FVExtrapdfstringdef#1#2{%
2575 \AfterPreamble{%
2576 \ifcsname pdfstringdef\endcsname
2577 \ifx\pdfstringdef\relax
2578 \else
2579 \pdfstringdef#1{#2}%
2580 \fi\fi}}
2581 \def\FVExtrapdfstringdefDisableCommands#1{%
2582 \AfterPreamble{%
2583 \ifcsname pdfstringdefDisableCommands\endcsname
2584 \ifx\pdfstringdefDisableCommands\relax
2585 \else
2586 \pdfstringdefDisableCommands{#1}%
2587 \fi\fi}}
2588 % \end{macrocode}
2589 % \end{macro}
2590 % \end{macro}
2591 %
2592 % \begin{macro}{\FVExtraAlwaysUnexpanded}
2593 % Always expands to itself, thanks to \cmd{\unexpanded}.
2594 % \begin{macrocode}
2595 \long\def\FVExtraAlwaysUnexpanded#1{%
2596 \unexpanded{\FVExtraAlwaysUnexpanded{#1}}}
2597 \FVExtrapdfstringdefDisableCommands{%
2598 \long\def\FVExtraAlwaysUnexpanded#1{#1}}
2599 % \end{macrocode}
2600 % \end{macro}
2601 %
2602 %
2603 % \begin{macro}{FVExtraRobustCommandExpanded}
2604 % Boolean to track whether expansion occurred. Set in \cmd{\FVExtraRobustCommand}. Useful in creating commands that behave differently depending on whether expansion occurred.
2605 % \begin{macrocode}
2606 \newbool{FVExtraRobustCommandExpanded}
2607 % \end{macrocode}
2608 % \end{macro}
2609 %
2610 % \begin{macro}{\FVExtraRobustCommand}
2611 % \begin{macrocode}
2612 \protected\def\FVExtraRobustCommand#1#2{%
2613 \ifx#2\FVExtraAlwaysUnexpanded
2614 \expandafter\@firstoftwo
2615 \else
2616 \expandafter\@secondoftwo
2617 \fi
2618 {\booltrue{FVExtraRobustCommandExpanded}\FV@RobustCommand@i{#1}}%
2619 {\boolfalse{FVExtraRobustCommandExpanded}#1}}
2620 \FVExtrapdfstringdefDisableCommands{%
2621 \def\FVExtraRobustCommand{}}
2622 % \end{macrocode}
2623 % \end{macro}
2624 %
2625 % \begin{macro}{\FV@RobustCommand@i}
2626 % |#2| will be the argument of \cmd{\FVExtraAlwaysUnexpanded}. Reading this strips the braces. At the beginning of |#2| will be the reader macro, which must be \cmd{\@gobble}'d.
2627 % \begin{macrocode}
2628 \def\FV@RobustCommand@i#1#2{\expandafter#1\@gobble#2}
2629 % \end{macrocode}
2630 % \end{macro}
2631 %
2632 % \begin{macro}{\FVExtraUnexpandedReadStarOArgMArg}
2633 % Read the arguments for a command that may be starred, may have an optional argument, and has a single brace-delimited mandatory argument. Then protect them with \cmd{\FVExtraAlwaysUnexpanded}. The reader macro is itself maintained in the protected result, so that it can be redefined to provide a simple default value for \pkg{hyperref}.
2634 %
2635 % Note the argument signature |#1#{|. This reads everything up to, but not including, the next brace group.
2636 % \begin{macrocode}
2637 \def\FVExtraUnexpandedReadStarOArgMArg#1#{%
2638 \FV@UnexpandedReadStarOArgMArg@i{#1}}
2639 % \end{macrocode}
2640 % \end{macro}
2641 %
2642 % \begin{macro}{\FV@UnexpandedReadStarOArgMArg@i}
2643 % \begin{macrocode}
2644 \def\FV@UnexpandedReadStarOArgMArg@i#1#2{%
2645 \FVExtraAlwaysUnexpanded{\FVExtraUnexpandedReadStarOArgMArg#1{#2}}}
2646 \FVExtrapdfstringdefDisableCommands{%
2647 \makeatletter
2648 \def\FV@UnexpandedReadStarOArgMArg@i#1#2{#2}%
2649 \makeatother}
2650 % \end{macrocode}
2651 % \end{macro}
2652 %
2653 %
2654 % \begin{macro}{\FVExtraUseVerbUnexpandedReadStarOArgMArg}
2655 % This is a variant of \cmd{\FVExtraUnexpandedReadStarOArgMArg} customized for \cmd{\UseVerb}. It would be tempting to use \cmd{\pdfstringdef} to define a PDF string based on the final tokenization in \cmd{\UseVerb}, rather than applying \cmd{\FVExtraPDFStringVerbatimDetokenize} to the original raw (read) tokenization. Unfortunately, \cmd{\pdfstringdef} apparently can't handle catcode 12 |\| and |%|. Since the final tokenization could contain arbitrary catcodes, that approach might fail even if the |\| and |%| issue were resolved. It may be worth considering more sophisticated approaches in the future.
2656 % \begin{macrocode}
2657 \def\FVExtraUseVerbUnexpandedReadStarOArgMArg#1#{%
2658 \FV@UseVerbUnexpandedReadStarOArgMArg@i{#1}}
2659 % \end{macrocode}
2660 % \end{macro}
2661 %
2662 % \begin{macro}{\FV@UseVerbUnexpandedReadStarOArgMArg@i}
2663 % \begin{macrocode}
2664 \def\FV@UseVerbUnexpandedReadStarOArgMArg@i#1#2{%
2665 \FVExtraAlwaysUnexpanded{\FVExtraUseVerbUnexpandedReadStarOArgMArg#1{#2}}}
2666 \FVExtrapdfstringdefDisableCommands{%
2667 \makeatletter
2668 \def\FV@UseVerbUnexpandedReadStarOArgMArg@i#1#2{%
2669 \ifcsname FV@SVRaw@#2\endcsname
2670 \expandafter\expandafter\expandafter\FVExtraPDFStringVerbatimDetokenize
2671 \expandafter\expandafter\expandafter{\csname FV@SVRaw@#2\endcsname}%
2672 \fi}%
2673 \makeatother}
2674 % \end{macrocode}
2675 % \end{macro}
2676 %
2677 %
2678 % \begin{macro}{\FVExtraUnexpandedReadStarOArgBVArg}
2679 % Same as \cmd{\FVExtraUnexpandedReadStarOArgMArg}, except |BVArg|, brace-delimited verbatim argument.
2680 % \begin{macrocode}
2681 \def\FVExtraUnexpandedReadStarOArgBVArg#1#{%
2682 \FV@UnexpandedReadStarOArgBVArg@i{#1}}
2683 % \end{macrocode}
2684 % \end{macro}
2685 %
2686 % \begin{macro}{\FV@UnexpandedReadStarOArgBVArg@i}
2687 % \begin{macrocode}
2688 \def\FV@UnexpandedReadStarOArgBVArg@i#1#2{%
2689 \FVExtraAlwaysUnexpanded{\FVExtraUnexpandedReadStarOArgBVArg#1{#2}}}
2690 \FVExtrapdfstringdefDisableCommands{%
2691 \makeatletter
2692 \def\FV@UnexpandedReadStarOArgBVArg@i#1#2{%
2693 \FVExtraPDFStringVerbatimDetokenize{#2}}%
2694 \makeatother}
2695 % \end{macrocode}
2696 % \end{macro}
2697 %
2698 %
2699 % \begin{macro}{\FVExtraUnexpandedReadStarOArgBEscVArg}
2700 % Same as \cmd{\FVExtraUnexpandedReadStarOArgMArg}, except |BEscVArg|, brace-delimited escaped verbatim argument.
2701 % \begin{macrocode}
2702 \def\FVExtraUnexpandedReadStarOArgBEscVArg#1#{%
2703 \FV@UnexpandedReadStarOArgBEscVArg@i{#1}}
2704 % \end{macrocode}
2705 % \end{macro}
2706 %
2707 % \begin{macro}{\FV@UnexpandedReadStarOArgBEscVArg@i}
2708 % \begin{macrocode}
2709 \def\FV@UnexpandedReadStarOArgBEscVArg@i#1#2{%
2710 \FVExtraAlwaysUnexpanded{\FVExtraUnexpandedReadStarOArgBEscVArg#1{#2}}}
2711 \FVExtrapdfstringdefDisableCommands{%
2712 \makeatletter
2713 \def\FV@UnexpandedReadStarOArgBEscVArg@i#1#2{%
2714 \FVExtraPDFStringEscapedVerbatimDetokenize{#2}}%
2715 \makeatother}
2716 % \end{macrocode}
2717 % \end{macro}
2718 %
2719 %
2720 % \begin{macro}{\FVExtraUnexpandedReadStarOArgMArgBVArg}
2721 % Read arguments for a command that has a mandatory argument before a verbatim argument, such as \pkg{minted}'s \cmd{\mintinline}.
2722 % \begin{macrocode}
2723 \def\FVExtraUnexpandedReadStarOArgMArgBVArg#1#{%
2724 \FV@UnexpandedReadStarOArgMArgBVArg@i{#1}}
2725 \def\FV@UnexpandedReadStarOArgMArgBVArg@i#1#2{%
2726 \FV@UnexpandedReadStarOArgMArgBVArg@ii{#1}{#2}}
2727 \def\FV@UnexpandedReadStarOArgMArgBVArg@ii#1#2#3#{%
2728 \FV@UnexpandedReadStarOArgMArgBVArg@iii{#1}{#2}{#3}}
2729 \def\FV@UnexpandedReadStarOArgMArgBVArg@iii#1#2#3#4{%
2730 \FVExtraAlwaysUnexpanded{\FVExtraUnexpandedReadStarOArgMArgBVArg#1{#2}#3{#4}}}
2731 \FVExtrapdfstringdefDisableCommands{%
2732 \makeatletter
2733 \def\FV@UnexpandedReadStarOArgMArgBVArg@iii#1#2#3#4{%
2734 \FVExtraPDFStringVerbatimDetokenize{#4}}%
2735 \makeatother}
2736 % \end{macrocode}
2737 % \end{macro}
2738 %
2739 %
2740 %
2741 %
2742 % \subsubsection{Converting detokenized tokens into PDF strings}
2743 %
2744 % At times it will be convenient to convert detokenized tokens into PDF strings, such as bookmarks. Define macros to escape such detokenized content so that it is in a suitable form.
2745 %
2746 % \begin{macro}{\FVExtraPDFStringEscapeChar}
2747 % Note that this does not apply any special treatment to spaces. If there are multiple adjacent spaces, then the octal escape \cmd{\040} is needed to prevent them from being merged. In the detokenization macros where \cmd{\FVExtraPDFStringEscapeChar} is currently used, spaces are processed separately without \cmd{\FVExtraPDFStringEscapeChar}, and literal spaces or \cmd{\040} are inserted in a context-dependent manner.
2748 % \begin{macrocode}
2749 \def\FVExtraPDFStringEscapeChar#1{%
2750 \ifcsname FV@PDFStringEscapeChar@#1\endcsname
2751 \csname FV@PDFStringEscapeChar@#1\endcsname
2752 \else
2753 #1%
2754 \fi}
2755 \begingroup
2756 \catcode`\&=14
2757 \catcode`\%=12&
2758 \catcode`\(=12&
2759 \catcode`\)=12&
2760 \catcode`\^^J=12&
2761 \catcode`\^^M=12&
2762 \catcode`\^^I=12&
2763 \catcode`\^^H=12&
2764 \catcode`\^^L=12&
2765 \catcode`\!=0\relax&
2766 !catcode`!\=12!relax&
2767 !expandafter!gdef!csname FV@PDFStringEscapeChar@\!endcsname{\\}&
2768 !expandafter!gdef!csname FV@PDFStringEscapeChar@%!endcsname{\%}&
2769 !expandafter!gdef!csname FV@PDFStringEscapeChar@(!endcsname{\(}&
2770 !expandafter!gdef!csname FV@PDFStringEscapeChar@)!endcsname{\)}&
2771 !expandafter!gdef!csname FV@PDFStringEscapeChar@^^J!endcsname{\n}&
2772 !expandafter!gdef!csname FV@PDFStringEscapeChar@^^M!endcsname{\r}&
2773 !expandafter!gdef!csname FV@PDFStringEscapeChar@^^I!endcsname{\t}&
2774 !expandafter!gdef!csname FV@PDFStringEscapeChar@^^H!endcsname{\b}&
2775 !expandafter!gdef!csname FV@PDFStringEscapeChar@^^L!endcsname{\f}&
2776 !catcode`!\=0!relax&
2777 \endgroup
2778 % \end{macrocode}
2779 % \end{macro}
2780 %
2781 %
2782 % \begin{macro}{\FVExtraPDFStringEscapeChars}
2783 % \begin{macrocode}
2784 \def\FVExtraPDFStringEscapeChars#1{%
2785 \FV@PDFStringEscapeChars#1\FV@Sentinel}
2786 % \end{macrocode}
2787 % \end{macro}
2788 %
2789 % \begin{macro}{\FV@PDFStringEscapeChars}
2790 % \begin{macrocode}
2791 \def\FV@PDFStringEscapeChars#1{%
2792 \ifx#1\FV@Sentinel
2793 \else
2794 \FVExtraPDFStringEscapeChar{#1}%
2795 \expandafter\FV@PDFStringEscapeChars
2796 \fi}%
2797 % \end{macrocode}
2798 % \end{macro}
2799 %
2800 %
2801 %
2802 %
2803 % \subsubsection{Detokenizing verbatim arguments}
2804 %
2805 % Ensure correct catcodes for this subsection (note |<| and |>| for \cmd{\FV@<Sentinel>}):
2806 % \begin{macrocode}
2807 \begingroup
2808 \catcode`\ =10
2809 \catcode`\a=11
2810 \catcode`\<=11
2811 \catcode`\>=11
2812 \catcode`\^^C=\active
2813 % \end{macrocode}
2814 %
2815 %
2816 % \paragraph{Detokenize as if the original source were tokenized verbatim}\hfill\\
2817 %
2818 % \begin{macro}{\FVExtraVerbatimDetokenize}
2819 % Detokenize tokens as if their original source was tokenized verbatim, rather than under any other catcode regime that may actually have been in place. This recovers the original source when tokenization was verbatim. Otherwise, it recovers the closest approximation of the source that is possible given information loss during tokenization (for example, adjacent space characters may be merged into a single space token). This is useful in constructing nearly verbatim commands that can be used inside other commands. It functions in an expansion-only context (``fully expandable,'' works in \cmd{\edef}).
2820 %
2821 % This yields spaces with catcode 12, \emph{not} spaces with catcode 10 like \cmd{\detokenize}. Spaces with catcode 10 require special handling when being read by macros, so detokenizing them to catcode 10 makes further processing difficult. Spaces with catcode 12 may be used just like any other catcode 12 token.
2822 %
2823 % This requires that the \cmd{\active} end-of-text (ETX) |^^C| (U+0003) not be defined as \cmd{\outer}, since |^^C| is used as a sentinel. Usually, it should not be defined at all, or defined to an error sequence. When in doubt, it may be worth explicitly defining |^^C| before using \cmd{\FVExtraVerbatimDetokenize}:
2824 % \begin{quote}
2825 %\begin{verbatim}
2826 %\begingroup
2827 %\catcode`\^^C=\active
2828 %\def^^C{}
2829 %...
2830 %\FVExtraVerbatimDetokenize{...}
2831 %...
2832 %\endgroup
2833 %\end{verbatim}
2834 % \end{quote}
2835 %
2836 % \cmd{\detokenize} inserts a space after each control word (control sequence with a name composed of catcode 11 tokens, ASCII letters |[a-zA-Z]|). For example,
2837 % \begin{quote}
2838 % \Verb{\detokenize{\macroA\macroB{}\csname name\endcsname123}}
2839 % \end{quote}
2840 % yields
2841 % \begin{quote}
2842 % \expandafter\Verb\expandafter{\detokenize{\macroA\macroB{}\csname name\endcsname123}}
2843 % \end{quote}
2844 % That is the correct behavior when detokenizing text that will later be retokenized for normal use. The space prevents the control word from accidentally merging with any letters that follow it immediately, and will be gobbled by the macro when retokenized. However, the inserted spaces are unwanted in the current context, because
2845 % \begin{quote}
2846 % \Verb{\FVExtraVerbatimDetokenize{\macroA\macroB{}\csname name\endcsname123}}
2847 % \end{quote}
2848 % should yield
2849 % \begin{quote}
2850 % \ttfamily
2851 % \FVExtraVerbatimDetokenize{\macroA\macroB{}\csname name\endcsname123}
2852 % \end{quote}
2853 % Note that the space is visible since it is catcode 12.
2854 %
2855 % Thus, \cmd{\FVExtraVerbatimDetokenize} is essentially a context-sensitive wrapper around \cmd{\detokenize} that removes extraneous space introduced by \cmd{\detokenize}. It iterates through the tokens, detokenizing them individually and then removing any trailing space inserted by \cmd{\detokenize}.
2856 % \begin{macrocode}
2857 \gdef\FVExtraVerbatimDetokenize#1{%
2858 \FV@VDetok@Scan{}#1^^C \FV@<Sentinel>}
2859 % \end{macrocode}
2860 % \end{macro}
2861 %
2862 % \begin{macro}{\FV@VDetok@Scan}
2863 % This scans through a token sequence while performing two tasks:
2864 % \begin{enumerate}
2865 % \item Replace all catcode 10 spaces with catcode 12 spaces.
2866 % \item Insert macros that will process groups, after which they will insert yet other macros to process individual tokens.
2867 % \end{enumerate}
2868 % Usage must \emph{always} have the form
2869 % \begin{quote}
2870 % |\FV@VDetok@Scan{}|\meta{tokens}\Verb*|^^C \FV@<Sentinel>|
2871 % \end{quote}
2872 % where |^^C| is \cmd{\active}, the catcode 10 space after |^^C| is mandatory, and \cmd{\FV@<Sentinel>} is a \emph{single}, undefined control word (this is accomplished via catcodes).
2873 % \begin{itemize}
2874 % \item \cmd{\FV@VDetok@Scan} searches for spaces to replace. After any spaces in \meta{tokens} have been handled, the space in \Verb*|^^C \FV@<Sentinel>| triggers space processing. When \cmd{\FV@VDetok@Scan} detects the sentinel macro \cmd{\FV@<Sentinel>}, scanning stops.
2875 % \item The |{}| protects the beginning of \meta{tokens}, so that if \meta{tokens} is a group, its braces won't be gobbled. Later, the inserted |{}| must be stripped so that it does not become part the processed \meta{tokens}.
2876 % \item |^^C| is a convenient separator between \meta{tokens} and the rest of the sentinel sequence.
2877 % \begin{itemize}
2878 % \item Since \cmd{\FV@VDetok@Scan} has delimited arguments, a leading catcode 10 space in \meta{tokens} will be preserved automatically. Preserving a trailing catcode 10 space is much easier if it is immediately adjacent to a non-space character in the sentinel sequence; two adjacent catcode 10 spaces would be difficult to handle with macro pattern matching. However, the sentinel sequence must contain a catcode 10 space, so the sentinel sequence must contain at least 3 tokens.
2879 % \item Since |^^C| is not a control word, it does not gobble following spaces. That makes it much easier to assemble macro arguments that contain a catcode 10 space. This is useful because the sentinel sequence \Verb*|^^C \FV@<Sentinel>| may have to be inserted into processing multiple times (for example, in recursive handling of groups).
2880 % \item \cmd{\FVExtraReadVArg} defines |^^C| as \cmd{\outer}, so any verbatim argument read by it is guaranteed not to contain |^^C|. This is in contrast to \cmd{\active} ASCII symbols and to two-character sequences |<backslash><symbol>| that should be expected in arbitrary verbatim content. It is a safe sentinel from that perspective.
2881 % \item A search of a complete TeX Live 2018 installation revealed no other uses of |^^C| that would clash (thanks, |ripgrep|!). As a control character, it should not be in common use except as a sentinel or for similar special purposes.
2882 % \end{itemize}
2883 % \end{itemize}
2884 %
2885 % If \meta{tokens} is empty or contains no spaces, then |#1| will contain |{}|\meta{tokens}|^^C| and |#2| will be empty. Otherwise, |#1| will contain |{}|\meta{tokens\_to\_space} and |#2| will contain \meta{tokens\_after\_space}\Verb*|^^C |.
2886 %
2887 % This uses the |\if\relax\detokenize{|\meta{argument}|}\relax| approach to check for an empty argument. If |#2| is empty, then the space that was just removed by \cmd{\FV@VDetok@Scan} reading its arguments was the space in the sentinel sequence, in which case scanning should end. |#1| is passed on raw so that \cmd{\FV@VDetok@ScanEnd} can strip the |^^C| from the end, which is the only remaining token from the sentinel sequence \Verb*|^^C \FV@<Sentinel>|. Otherwise, if |#2| is not empty, continue. In that case, the braces in |{#1}{#2}| ensure arguments remain intact.
2888 %
2889 % Note that \cmd{\FV@<Sentinel>} is removed during each space search, and thus must be reinserted in \cmd{\FV@VDetok@ScanCont}. It would be possible to use the macro signature |#1 #2| instead of |#1 #2\FV@<Sentinel>|, and then do an \cmd{\ifx} test on |#2| for \cmd{\FV@<Sentinel>}. However, that is problematic, because |#2| may contain an arbitrary sequence of arbitrary tokens, so it cannot be used safely without \cmd{\detokenize}.
2890 % \begin{macrocode}
2891 \gdef\FV@VDetok@Scan#1 #2\FV@<Sentinel>{%
2892 \if\relax\detokenize{#2}\relax
2893 \expandafter\@firstoftwo
2894 \else
2895 \expandafter\@secondoftwo
2896 \fi
2897 {\FV@VDetok@ScanEnd#1}%
2898 {\FV@VDetok@ScanCont{#1}{#2}}}
2899 % \end{macrocode}
2900 % \end{macro}
2901 %
2902 % \begin{macro}{\FV@VDetok@ScanEnd}
2903 % This removes the |^^C| from the sentinel sequence \Verb*|^^C \FV@<Sentinel>|, so the sentinel sequence is now completely gone. If |#1| is empty, there is nothing to do (|#1| being empty means that |#1| consumed the |{}| that was inserted to protect anything following, because there was nothing after it). Otherwise, \cmd{\@gobble} the inserted |{}| before starting a different scan to deal with groups. The group scanner \cmd{\FV@VDetok@ScanGroup} has its own sentinel sequence |{\FV@<Sentinel>}|.
2904 % \begin{macrocode}
2905 \gdef\FV@VDetok@ScanEnd#1^^C{%
2906 \if\relax\detokenize{#1}\relax
2907 \expandafter\@gobble
2908 \else
2909 \expandafter\@firstofone
2910 \fi
2911 {\expandafter\FV@VDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}}
2912 % \end{macrocode}
2913 % \end{macro}
2914 %
2915 % \begin{macro}{\FV@VDetok@ScanCont}
2916 % Continue scanning after removing a space in \cmd{\FV@VDetok@Scan}.
2917 %
2918 % |#1| is everything before the space. If |#1| is empty, there is nothing to do related to it; |#1| simply consumed an inserted |{}| that preceded nothing (that would be a leading space). Otherwise, start a different scan on |#1| to deal with groups. A non-empty |#1| will start with the |{}| that was inserted to protect groups, hence the \cmd{\@gobble} before group scanning.
2919 %
2920 % Then insert a literal catcode 12 space to account for the space removed in \cmd{\FV@VDetok@Scan}. Note the catcode, and thus the lack of indentation and the |%| to avoid unwanted catcode 12 spaces.
2921 %
2922 % |#2| is everything after the space, ending with \Verb*|^^C | from the sentinel sequence \Verb*|^^C \FV@<Sentinel>|. This needs continued scanning to deal with spaces, with |{}| inserted in front to protect a leading group and \cmd{\FV@<Sentinel>} after to complete the sentinel sequence.
2923 % \begin{macrocode}
2924 \begingroup
2925 \catcode`\ =12%
2926 \gdef\FV@VDetok@ScanCont#1#2{%
2927 \if\relax\detokenize{#1}\relax%
2928 \expandafter\@gobble%
2929 \else%
2930 \expandafter\@firstofone%
2931 \fi%
2932 {\expandafter\FV@VDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}%
2933 %<-catcode 12 space
2934 \FV@VDetok@Scan{}#2\FV@<Sentinel>}%
2935 \endgroup
2936 % \end{macrocode}
2937 % \end{macro}
2938 %
2939 % \begin{macro}{\FV@VDetok@ScanGroup}
2940 % The macro argument |#1#| reads up to the next group. When this macro is invoked, the sentinel sequence |{\FV@<Sentinel>}| is inserted, so there is guaranteed to be at least one group.
2941 %
2942 % Everything in |#1| contains no spaces and no groups, and thus is ready for token scanning, with the sentinel \cmd{\FV@Sentinel}. Note that \cmd{\FV@Sentinel}, which is defined as |\def\FV@Sentinel{\FV@<Sentinel>}|, is used here, \emph{not} \cmd{\FV@<Sentinel>}. \cmd{\FV@<Sentinel>} is not defined and is thus unsuitable for \cmd{\ifx} comparisons with tokens that may have been tokenized under an incorrect catcode regime and thus are undefined. \cmd{\FV@Sentinel} \emph{is} defined, and its definition is resistant against accidental collisions.
2943 % \begin{macrocode}
2944 \gdef\FV@VDetok@ScanGroup#1#{%
2945 \FV@VDetok@ScanToken#1\FV@Sentinel
2946 \FV@VDetok@ScanGroup@i}
2947 % \end{macrocode}
2948 % \end{macro}
2949 %
2950 % \begin{macro}{\FV@VDetok@ScanGroup@i}
2951 % The braces from the group are stripped during reading |#1|. Proceed based on whether the group is empty. If the group is not empty, |{}| must be inserted to protect |#1| in case it is a group, and the new sentinel sequence |\FV@<Sentinel>^^C| is added for the group contents. \cmd{\FV@<Sentinel>} cannot be used as a sentinel for the group contents, because if this is the sentinel group |{\FV@<Sentinel>}|, then |#1| is \cmd{\FV@<Sentinel>}.
2952 % \begin{macrocode}
2953 \gdef\FV@VDetok@ScanGroup@i#1{%
2954 \if\relax\detokenize{#1}\relax
2955 \expandafter\@firstoftwo
2956 \else
2957 \expandafter\@secondoftwo
2958 \fi
2959 {\FV@VDetok@ScanEmptyGroup}%
2960 {\FV@VDetok@ScanGroup@ii{}#1\FV@<Sentinel>^^C}}
2961 % \end{macrocode}
2962 % \end{macro}
2963 %
2964 % \begin{macro}{\FV@VDetok@ScanEmptyGroup}
2965 % Insert |{}| to handle the empty group, then continue group scanning.
2966 % \begin{macrocode}
2967 \begingroup
2968 \catcode`\(=1
2969 \catcode`\)=2
2970 \catcode`\{=12
2971 \catcode`\}=12
2972 \gdef\FV@VDetok@ScanEmptyGroup({}\FV@VDetok@ScanGroup)
2973 \endgroup
2974 % \end{macrocode}
2975 % \end{macro}
2976 %
2977 % \begin{macro}{\FV@VDetok@ScanGroup@ii}
2978 % The group is not empty, so determine whether it contains \cmd{\FV@<Sentinel>} and thus is the sentinel group. The group contents are followed by the sentinel sequence |\FV@<Sentinel>^^C| inserted in \cmd{\FV@VDetok@ScanGroup@i}. This means that if |#2| is empty, the group did not contain \cmd{\FV@<Sentinel>} and thus is not the sentinel group. Otherwise, |#2| will be \cmd{\FV@<Sentinel>}.
2979 %
2980 % If this is not the sentinel group, then the group contents must be scanned, with surrounding literal braces inserted. |#1| already contains an inserted leading |{}| to protect groups; see \cmd{\FV@VDetok@ScanGroup@i}. A sentinel sequence \Verb*|^^C \FV@<Sentinel>| is needed, though. Then group scanning must continue.
2981 % \begin{macrocode}
2982 \begingroup
2983 \catcode`\(=1
2984 \catcode`\)=2
2985 \catcode`\{=12
2986 \catcode`\}=12
2987 \gdef\FV@VDetok@ScanGroup@ii#1\FV@<Sentinel>#2^^C(%
2988 \if\relax\detokenize(#2)\relax
2989 \expandafter\@firstofone
2990 \else
2991 \expandafter\@gobble
2992 \fi
2993 ({\FV@VDetok@Scan#1^^C \FV@<Sentinel>}\FV@VDetok@ScanGroup))
2994 \endgroup
2995 % \end{macrocode}
2996 % \end{macro}
2997 %
2998 % \begin{macro}{\FV@VDetok@ScanToken}
2999 % Scan individual tokens. At this point, all spaces and groups have been handled, so this will only ever encounter individual tokens that can be iterated with a |#1| argument. The sentinel for token scanning is \cmd{\FV@Sentinel}. This is the appropriate sentinel because \cmd{\ifx} comparisons are now safe (individual tokens) and \cmd{\FV@Sentinel} is defined. Processing individual detokenized tokens requires the same sentinel sequence as handling spaces, since it can produce them.
3000 % \begin{macrocode}
3001 \gdef\FV@VDetok@ScanToken#1{%
3002 \ifx\FV@Sentinel#1%
3003 \expandafter\@gobble
3004 \else
3005 \expandafter\@firstofone
3006 \fi
3007 {\expandafter\FV@VDetok@ScanToken@i\detokenize{#1}^^C \FV@<Sentinel>}}
3008 % \end{macrocode}
3009 % \end{macro}
3010 %
3011 % \begin{macro}{\FV@VDetok@ScanToken@i}
3012 % If |#2| is empty, then there are no spaces in the detokenized token, so it is either an \cmd{\active} character other than the space, or a two-character sequence of the form |<backslash><symbol>| where the second character is not a space. Thus, |#1| contains \meta{detokenized}|^^C|. Otherwise, |#1| contains \meta{detokenized\_without\_space}, and |#2| may be discarded since it contains \Verb*|^^C \FV@<Sentinel>|. (If the detokenized token contains a space, it is always at the end.)
3013 % \begin{macrocode}
3014 \gdef\FV@VDetok@ScanToken@i#1 #2\FV@<Sentinel>{%
3015 \if\relax\detokenize{#2}\relax
3016 \expandafter\@firstoftwo
3017 \else
3018 \expandafter\@secondoftwo
3019 \fi
3020 {\FV@VDetok@ScanTokenNoSpace#1}%
3021 {\FV@VDetok@ScanTokenWithSpace{#1}}}
3022 % \end{macrocode}
3023 % \end{macro}
3024 %
3025 % \begin{macro}{\FV@VDetok@ScanTokenNoSpace}
3026 % Strip |^^C| sentinel in reading, then insert character(s) and continue scanning.
3027 % \begin{macrocode}
3028 \gdef\FV@VDetok@ScanTokenNoSpace#1^^C{#1\FV@VDetok@ScanToken}
3029 % \end{macrocode}
3030 % \end{macro}
3031 %
3032 % \begin{macro}{\FV@VDetok@ScanTokenWithSpace}
3033 % Handle a token that when detokenized produces a space. If there is nothing left once the space is removed, this is the \cmd{\active} space. Otherwise, process further.
3034 % \begin{macrocode}
3035 \gdef\FV@VDetok@ScanTokenWithSpace#1{%
3036 \if\relax\detokenize{#1}\relax
3037 \expandafter\@firstoftwo
3038 \else
3039 \expandafter\@secondoftwo
3040 \fi
3041 {\FV@VDetok@ScanTokenActiveSpace}%
3042 {\FV@VDetok@ScanTokenWithSpace@i#1\FV@<Sentinel>}}
3043 % \end{macrocode}
3044 % \end{macro}
3045 %
3046 % \begin{macro}{\FV@VDetok@ScanTokenActiveSpace}
3047 % \begin{macrocode}
3048 \begingroup
3049 \catcode`\ =12%
3050 \gdef\FV@VDetok@ScanTokenActiveSpace{ \FV@VDetok@ScanToken}%
3051 \endgroup
3052 % \end{macrocode}
3053 % \end{macro}
3054 %
3055 % \begin{macro}{\FV@VDetok@ScanTokenWithSpace@i}
3056 % If there is only one character left once the space is removed, this is the escaped space \Verb*|\ |. Otherwise, this is a command word that needs further processing.
3057 % \begin{macrocode}
3058 \gdef\FV@VDetok@ScanTokenWithSpace@i#1#2\FV@<Sentinel>{%
3059 \if\relax\detokenize{#2}\relax
3060 \expandafter\@firstoftwo
3061 \else
3062 \expandafter\@secondoftwo
3063 \fi
3064 {\FV@VDetok@ScanTokenEscSpace{#1}}%
3065 {\FV@VDetok@ScanTokenCW{#1#2}}}
3066 % \end{macrocode}
3067 % \end{macro}
3068 %
3069 % \begin{macro}{\FV@VDetok@ScanTokenEscSpace}
3070 % \begin{macrocode}
3071 \begingroup
3072 \catcode`\ =12%
3073 \gdef\FV@VDetok@ScanTokenEscSpace#1{#1 \FV@VDetok@ScanToken}%
3074 \endgroup
3075 % \end{macrocode}
3076 % \end{macro}
3077 %
3078 % \begin{macro}{\FV@VDetok@ScanTokenCW}
3079 % Process control words in a context-sensitive manner by looking ahead to the next token (|#2|). The lookahead must be reinserted into processing, hence the |\FV@VDetok@ScanToken#2|.
3080 %
3081 % A control word will detokenize to a sequence of characters followed by a space. If the following token has catcode 11, then this space represents one or more space characters that must have been present in the original source, because otherwise the catcode 11 token would have become part of the control word's name. If the following token has another catcode, then it is impossible to determine whether a space was present, so assume that one was not.
3082 % \begin{macrocode}
3083 \begingroup
3084 \catcode`\ =12%
3085 \gdef\FV@VDetok@ScanTokenCW#1#2{%
3086 \ifcat\noexpand#2a%
3087 \expandafter\@firstoftwo%
3088 \else%
3089 \expandafter\@secondoftwo%
3090 \fi%
3091 {#1 \FV@VDetok@ScanToken#2}%
3092 {#1\FV@VDetok@ScanToken#2}}%
3093 \endgroup
3094 % \end{macrocode}
3095 % \end{macro}
3096 %
3097 %
3098 % \paragraph{Detokenize as if the original source were tokenized verbatim, then convert to PDF string}\hfill\\
3099 %
3100 % \begin{macro}{\FVExtraPDFStringVerbatimDetokenize}
3101 % This is identical to \cmd{\FVExtraVerbatimDetokenize}, except that the output is converted to a valid PDF string. Some spaces are represented with the octal escape \cmd{\040} to prevent adjacent spaces from being merged.
3102 % \begin{macrocode}
3103 \gdef\FVExtraPDFStringVerbatimDetokenize#1{%
3104 \FV@PDFStrVDetok@Scan{}#1^^C \FV@<Sentinel>}
3105 % \end{macrocode}
3106 % \end{macro}
3107 %
3108 % \begin{macro}{\FV@PDFStrVDetok@Scan}
3109 % \begin{macrocode}
3110 \gdef\FV@PDFStrVDetok@Scan#1 #2\FV@<Sentinel>{%
3111 \if\relax\detokenize{#2}\relax
3112 \expandafter\@firstoftwo
3113 \else
3114 \expandafter\@secondoftwo
3115 \fi
3116 {\FV@PDFStrVDetok@ScanEnd#1}%
3117 {\FV@PDFStrVDetok@ScanCont{#1}{#2}}}
3118 % \end{macrocode}
3119 % \end{macro}
3120 %
3121 % \begin{macro}{\FV@PDFStrVDetok@ScanEnd}
3122 % \begin{macrocode}
3123 \gdef\FV@PDFStrVDetok@ScanEnd#1^^C{%
3124 \if\relax\detokenize{#1}\relax
3125 \expandafter\@gobble
3126 \else
3127 \expandafter\@firstofone
3128 \fi
3129 {\expandafter\FV@PDFStrVDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}}
3130 % \end{macrocode}
3131 % \end{macro}
3132 %
3133 % \begin{macro}{\FV@PDFStrVDetok@ScanCont}
3134 % \begin{macrocode}
3135 \begingroup
3136 \catcode`\ =12%
3137 \gdef\FV@PDFStrVDetok@ScanCont#1#2{%
3138 \if\relax\detokenize{#1}\relax%
3139 \expandafter\@gobble%
3140 \else%
3141 \expandafter\@firstofone%
3142 \fi%
3143 {\expandafter\FV@PDFStrVDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}%
3144 %<-catcode 12 space
3145 \FV@PDFStrVDetok@Scan{}#2\FV@<Sentinel>}%
3146 \endgroup
3147 % \end{macrocode}
3148 % \end{macro}
3149 %
3150 % \begin{macro}{\FV@PDFStrVDetok@ScanGroup}
3151 % \begin{macrocode}
3152 \gdef\FV@PDFStrVDetok@ScanGroup#1#{%
3153 \FV@PDFStrVDetok@ScanToken#1\FV@Sentinel
3154 \FV@PDFStrVDetok@ScanGroup@i}
3155 % \end{macrocode}
3156 % \end{macro}
3157 %
3158 % \begin{macro}{\FV@PDFStrVDetok@ScanGroup@i}
3159 % \begin{macrocode}
3160 \gdef\FV@PDFStrVDetok@ScanGroup@i#1{%
3161 \if\relax\detokenize{#1}\relax
3162 \expandafter\@firstoftwo
3163 \else
3164 \expandafter\@secondoftwo
3165 \fi
3166 {\FV@PDFStrVDetok@ScanEmptyGroup}%
3167 {\FV@PDFStrVDetok@ScanGroup@ii{}#1\FV@<Sentinel>^^C}}
3168 % \end{macrocode}
3169 % \end{macro}
3170 %
3171 % \begin{macro}{\FV@PDFStrVDetok@ScanEmptyGroup}
3172 % \begin{macrocode}
3173 \begingroup
3174 \catcode`\(=1
3175 \catcode`\)=2
3176 \catcode`\{=12
3177 \catcode`\}=12
3178 \gdef\FV@PDFStrVDetok@ScanEmptyGroup({}\FV@PDFStrVDetok@ScanGroup)
3179 \endgroup
3180 % \end{macrocode}
3181 % \end{macro}
3182 %
3183 % \begin{macro}{\FV@PDFStrVDetok@ScanGroup@ii}
3184 % \begin{macrocode}
3185 \begingroup
3186 \catcode`\(=1
3187 \catcode`\)=2
3188 \catcode`\{=12
3189 \catcode`\}=12
3190 \gdef\FV@PDFStrVDetok@ScanGroup@ii#1\FV@<Sentinel>#2^^C(%
3191 \if\relax\detokenize(#2)\relax
3192 \expandafter\@firstofone
3193 \else
3194 \expandafter\@gobble
3195 \fi
3196 ({\FV@PDFStrVDetok@Scan#1^^C \FV@<Sentinel>}\FV@PDFStrVDetok@ScanGroup))
3197 \endgroup
3198 % \end{macrocode}
3199 % \end{macro}
3200 %
3201 % \begin{macro}{\FV@PDFStrVDetok@ScanToken}
3202 % \begin{macrocode}
3203 \gdef\FV@PDFStrVDetok@ScanToken#1{%
3204 \ifx\FV@Sentinel#1%
3205 \expandafter\@gobble
3206 \else
3207 \expandafter\@firstofone
3208 \fi
3209 {\expandafter\FV@PDFStrVDetok@ScanToken@i\detokenize{#1}^^C \FV@<Sentinel>}}
3210 % \end{macrocode}
3211 % \end{macro}
3212 %
3213 % \begin{macro}{\FV@PDFStrVDetok@ScanToken@i}
3214 % \begin{macrocode}
3215 \gdef\FV@PDFStrVDetok@ScanToken@i#1 #2\FV@<Sentinel>{%
3216 \if\relax\detokenize{#2}\relax
3217 \expandafter\@firstoftwo
3218 \else
3219 \expandafter\@secondoftwo
3220 \fi
3221 {\FV@PDFStrVDetok@ScanTokenNoSpace#1}%
3222 {\FV@PDFStrVDetok@ScanTokenWithSpace{#1}}}
3223 % \end{macrocode}
3224 % \end{macro}
3225 %
3226 % \begin{macro}{\FV@PDFStrVDetok@ScanTokenNoSpace}
3227 % This is modified to use \cmd{\FVExtraPDFStringEscapeChars}.
3228 % \begin{macrocode}
3229 \gdef\FV@PDFStrVDetok@ScanTokenNoSpace#1^^C{%
3230 \FVExtraPDFStringEscapeChars{#1}\FV@PDFStrVDetok@ScanToken}
3231 % \end{macrocode}
3232 % \end{macro}
3233 %
3234 % \begin{macro}{\FV@PDFStrVDetok@ScanTokenWithSpace}
3235 % \begin{macrocode}
3236 \gdef\FV@PDFStrVDetok@ScanTokenWithSpace#1{%
3237 \if\relax\detokenize{#1}\relax
3238 \expandafter\@firstoftwo
3239 \else
3240 \expandafter\@secondoftwo
3241 \fi
3242 {\FV@PDFStrVDetok@ScanTokenActiveSpace}%
3243 {\FV@PDFStrVDetok@ScanTokenWithSpace@i#1\FV@<Sentinel>}}
3244 % \end{macrocode}
3245 % \end{macro}
3246 %
3247 % \begin{macro}{\FV@PDFStrVDetok@ScanTokenActiveSpace}
3248 % This is modified to use \cmd{\040} rather than a catcode 12 space.
3249 % \begin{macrocode}
3250 \begingroup
3251 \catcode`\!=0\relax
3252 \catcode`\\=12!relax
3253 !gdef!FV@PDFStrVDetok@ScanTokenActiveSpace{\040!FV@PDFStrVDetok@ScanToken}%
3254 !catcode`!\=0!relax
3255 \endgroup
3256 % \end{macrocode}
3257 % \end{macro}
3258 %
3259 % \begin{macro}{\FV@PDFStrVDetok@ScanTokenWithSpace@i}
3260 % If there is only one character left once the space is removed, this is the escaped space \Verb*|\ |. Otherwise, this is a command word that needs further processing.
3261 % \begin{macrocode}
3262 \gdef\FV@PDFStrVDetok@ScanTokenWithSpace@i#1#2\FV@<Sentinel>{%
3263 \if\relax\detokenize{#2}\relax
3264 \expandafter\@firstoftwo
3265 \else
3266 \expandafter\@secondoftwo
3267 \fi
3268 {\FV@PDFStrVDetok@ScanTokenEscSpace{#1}}%
3269 {\FV@PDFStrVDetok@ScanTokenCW{#1#2}}}
3270 % \end{macrocode}
3271 % \end{macro}
3272 %
3273 % \begin{macro}{\FV@PDFStrVDetok@ScanTokenEscSpace}
3274 % This is modified to add \cmd{\FVExtraPDFStringEscapeChar} and use |\040| for the space, since a space could follow.
3275 % \begin{macrocode}
3276 \begingroup
3277 \catcode`\!=0\relax
3278 \catcode`\\=12!relax
3279 !gdef!FV@PDFStrVDetok@ScanTokenEscSpace#1{%
3280 !FVExtraPDFStringEscapeChar{#1}\040!FV@PDFStrVDetok@ScanToken}%
3281 !catcode`!\=0!relax
3282 \endgroup
3283 % \end{macrocode}
3284 % \end{macro}
3285 %
3286 % \begin{macro}{\FV@PDFStrVDetok@ScanTokenCW}
3287 % This is modified to add \cmd{\FVExtraPDFStringEscapeChars}.
3288 % \begin{macrocode}
3289 \begingroup
3290 \catcode`\ =12%
3291 \gdef\FV@PDFStrVDetok@ScanTokenCW#1#2{%
3292 \ifcat\noexpand#2a%
3293 \expandafter\@firstoftwo%
3294 \else%
3295 \expandafter\@secondoftwo%
3296 \fi%
3297 {\FVExtraPDFStringEscapeChars{#1} \FV@PDFStrVDetok@ScanToken#2}%
3298 {\FVExtraPDFStringEscapeChars{#1}\FV@PDFStrVDetok@ScanToken#2}}
3299 \endgroup
3300 % \end{macrocode}
3301 % \end{macro}
3302 %
3303 %
3304 % \paragraph{Detokenize as if the original source were tokenized verbatim, except for backslash escapes of non-catcode 11 characters}\hfill
3305 %
3306 % \begin{macro}{\FVExtraEscapedVerbatimDetokenize}
3307 % This is a variant of \cmd{\FVExtraVerbatimDetokenize} that treats character sequences of the form |\<char>| as escapes for |<char>|. It is primarily intended for making |\<symbol>| escapes for |<symbol>|, but allowing arbitrary escapes simplifies the default behavior and implementation. This is useful in constructing nearly verbatim commands that can be used inside other commands, because the backslash escapes allow for characters like |#| and |%|, as well as making possible multiple adjacent spaces via \Verb*|\ |. It should be applied to arguments that are read verbatim insofar as is possible, except that the backslash |\| should have its normal meaning (catcode 0). Most of the implementation is identical to that for \cmd{\FVExtraVerbatimDetokenize}. Only the token processing requires modification to handle backslash escapes.
3308 %
3309 % It is possible to restrict escapes to ASCII symbols and punctuation. See \cmd{\FVExtraDetokenizeREscVArg}. The disadvantage of restricting escapes is that it prevents functioning in an expansion-only context (unless you want to use undefined macros as a means of raising errors). The advantage is that it eliminates ambiguity introduced by allowing arbitrary escapes. Backslash escapes of characters with catcode 11 (ASCII letters, |[A-Za-z]|) are typically not necessary, and introduce ambiguity because something like |\x| will gobble following spaces since it will be tokenized originally as a control word.
3310 % \begin{macrocode}
3311 \gdef\FVExtraEscapedVerbatimDetokenize#1{%
3312 \FV@EscVDetok@Scan{}#1^^C \FV@<Sentinel>}
3313 % \end{macrocode}
3314 % \end{macro}
3315 %
3316 % \begin{macro}{\FV@EscVDetok@Scan}
3317 % \begin{macrocode}
3318 \gdef\FV@EscVDetok@Scan#1 #2\FV@<Sentinel>{%
3319 \if\relax\detokenize{#2}\relax
3320 \expandafter\@firstoftwo
3321 \else
3322 \expandafter\@secondoftwo
3323 \fi
3324 {\FV@EscVDetok@ScanEnd#1}%
3325 {\FV@EscVDetok@ScanCont{#1}{#2}}}
3326 % \end{macrocode}
3327 % \end{macro}
3328 %
3329 % \begin{macro}{\FV@EscVDetok@ScanEnd}
3330 % \begin{macrocode}
3331 \gdef\FV@EscVDetok@ScanEnd#1^^C{%
3332 \if\relax\detokenize{#1}\relax
3333 \expandafter\@gobble
3334 \else
3335 \expandafter\@firstofone
3336 \fi
3337 {\expandafter\FV@EscVDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}}
3338 % \end{macrocode}
3339 % \end{macro}
3340 %
3341 % \begin{macro}{\FV@EscVDetok@ScanCont}
3342 % \begin{macrocode}
3343 \begingroup
3344 \catcode`\ =12%
3345 \gdef\FV@EscVDetok@ScanCont#1#2{%
3346 \if\relax\detokenize{#1}\relax%
3347 \expandafter\@gobble%
3348 \else%
3349 \expandafter\@firstofone%
3350 \fi%
3351 {\expandafter\FV@EscVDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}%
3352 %<-catcode 12 space
3353 \FV@EscVDetok@Scan{}#2\FV@<Sentinel>}%
3354 \endgroup
3355 % \end{macrocode}
3356 % \end{macro}
3357 %
3358 % \begin{macro}{\FV@EscVDetok@ScanGroup}
3359 % \begin{macrocode}
3360 \gdef\FV@EscVDetok@ScanGroup#1#{%
3361 \FV@EscVDetok@ScanToken#1\FV@Sentinel
3362 \FV@EscVDetok@ScanGroup@i}
3363 % \end{macrocode}
3364 % \end{macro}
3365 %
3366 % \begin{macro}{\FV@EscVDetok@ScanGroup@i}
3367 % \begin{macrocode}
3368 \gdef\FV@EscVDetok@ScanGroup@i#1{%
3369 \if\relax\detokenize{#1}\relax
3370 \expandafter\@firstoftwo
3371 \else
3372 \expandafter\@secondoftwo
3373 \fi
3374 {\FV@EscVDetok@ScanEmptyGroup}%
3375 {\FV@EscVDetok@ScanGroup@ii{}#1\FV@<Sentinel>^^C}}
3376 % \end{macrocode}
3377 % \end{macro}
3378 %
3379 % \begin{macro}{\FV@EscVDetok@ScanEmptyGroup}
3380 % \begin{macrocode}
3381 \begingroup
3382 \catcode`\(=1
3383 \catcode`\)=2
3384 \catcode`\{=12
3385 \catcode`\}=12
3386 \gdef\FV@EscVDetok@ScanEmptyGroup({}\FV@EscVDetok@ScanGroup)
3387 \endgroup
3388 % \end{macrocode}
3389 % \end{macro}
3390 %
3391 % \begin{macro}{\FV@EscVDetok@ScanGroup@ii}
3392 % \begin{macrocode}
3393 \begingroup
3394 \catcode`\(=1
3395 \catcode`\)=2
3396 \catcode`\{=12
3397 \catcode`\}=12
3398 \gdef\FV@EscVDetok@ScanGroup@ii#1\FV@<Sentinel>#2^^C(%
3399 \if\relax\detokenize(#2)\relax
3400 \expandafter\@firstofone
3401 \else
3402 \expandafter\@gobble
3403 \fi
3404 ({\FV@EscVDetok@Scan#1^^C \FV@<Sentinel>}\FV@EscVDetok@ScanGroup))
3405 \endgroup
3406 % \end{macrocode}
3407 % \end{macro}
3408 %
3409 % \begin{macro}{\FV@EscVDetok@ScanToken}
3410 % \begin{macrocode}
3411 \gdef\FV@EscVDetok@ScanToken#1{%
3412 \ifx\FV@Sentinel#1%
3413 \expandafter\@gobble
3414 \else
3415 \expandafter\@firstofone
3416 \fi
3417 {\expandafter\FV@EscVDetok@ScanToken@i\detokenize{#1}^^C \FV@<Sentinel>}}
3418 % \end{macrocode}
3419 % \end{macro}
3420 %
3421 % \begin{macro}{\FV@EscVDetok@ScanToken@i}
3422 % \begin{macrocode}
3423 \gdef\FV@EscVDetok@ScanToken@i#1 #2\FV@<Sentinel>{%
3424 \if\relax\detokenize{#2}\relax
3425 \expandafter\@firstoftwo
3426 \else
3427 \expandafter\@secondoftwo
3428 \fi
3429 {\FV@EscVDetok@ScanTokenNoSpace#1}%
3430 {\FV@EscVDetok@ScanTokenWithSpace{#1}}}
3431 % \end{macrocode}
3432 % \end{macro}
3433 %
3434 % \subparagraph{Parallel implementations, with a restricted option}
3435 % Starting here, there are alternate macros for restricting escapes to ASCII punctuation and symbols. These alternates have names of the form \cmd{\FV@REscVDetok@<name>}. They are used in \cmd{\FVExtraDetokenizeREscVArg}. The alternate \cmd{\FV@REscVDetok@<name>} macros replace invalid escape sequences with the undefined \cmd{\FV@<InvalidEscape>}, which is later scanned for with a delimited macro.
3436 %
3437 % \begin{macro}{\FV@EscVDetok@ScanTokenNoSpace}
3438 % This was modified from \cmd{\FV@VDetok@ScanTokenNoSpace} to discard the first character of multi-character sequences (that would be the backslash |\|).
3439 % \begin{macrocode}
3440 \gdef\FV@EscVDetok@ScanTokenNoSpace#1#2^^C{%
3441 \if\relax\detokenize{#2}\relax
3442 \expandafter\@firstoftwo
3443 \else
3444 \expandafter\@secondoftwo
3445 \fi
3446 {#1\FV@EscVDetok@ScanToken}%
3447 {#2\FV@EscVDetok@ScanToken}}
3448 % \end{macrocode}
3449 % \end{macro}
3450 %
3451 % \begin{macro}{\FV@REscVDetok@ScanTokenNoSpace}
3452 % \begin{macrocode}
3453 \gdef\FV@REscVDetok@ScanTokenNoSpace#1#2^^C{%
3454 \if\relax\detokenize{#2}\relax
3455 \expandafter\@firstoftwo
3456 \else
3457 \expandafter\@secondoftwo
3458 \fi
3459 {#1\FV@EscVDetok@ScanToken}%
3460 {\ifcsname FV@Char@Special:\number`#2\endcsname#2\else\noexpand\FV@<InvalidEscape>\fi
3461 \FV@EscVDetok@ScanToken}}
3462 % \end{macrocode}
3463 % \end{macro}
3464 %
3465 %
3466 % \begin{macro}{\FV@EscVDetok@ScanTokenWithSpace}
3467 % \begin{macrocode}
3468 \gdef\FV@EscVDetok@ScanTokenWithSpace#1{%
3469 \if\relax\detokenize{#1}\relax
3470 \expandafter\@firstoftwo
3471 \else
3472 \expandafter\@secondoftwo
3473 \fi
3474 {\FV@EscVDetok@ScanTokenActiveSpace}%
3475 {\FV@EscVDetok@ScanTokenWithSpace@i#1\FV@<Sentinel>}}
3476 % \end{macrocode}
3477 % \end{macro}
3478 %
3479 % \begin{macro}{\FV@EscVDetok@ScanTokenActiveSpace}
3480 % \begin{macrocode}
3481 \begingroup
3482 \catcode`\ =12%
3483 \gdef\FV@EscVDetok@ScanTokenActiveSpace{ \FV@EscVDetok@ScanToken}%
3484 \endgroup
3485 % \end{macrocode}
3486 % \end{macro}
3487 %
3488 % \begin{macro}{\FV@EscVDetok@ScanTokenWithSpace@i}
3489 % If there is only one character left once the space is removed, this is the escaped space \Verb*|\ |. Otherwise, this is a command word. A command word is passed on so as to keep the backslash and letters separate.
3490 % \begin{macrocode}
3491 \gdef\FV@EscVDetok@ScanTokenWithSpace@i#1#2\FV@<Sentinel>{%
3492 \if\relax\detokenize{#2}\relax
3493 \expandafter\@firstoftwo
3494 \else
3495 \expandafter\@secondoftwo
3496 \fi
3497 {\FV@EscVDetok@ScanTokenEscSpace{#1}}%
3498 {\FV@EscVDetok@ScanTokenCW{#1}{#2}}}
3499 % \end{macrocode}
3500 % \end{macro}
3501 %
3502 % \begin{macro}{\FV@REscVDetok@ScanTokenWithSpace@i}
3503 % \begin{macrocode}
3504 \gdef\FV@REscVDetok@ScanTokenWithSpace@i#1#2\FV@<Sentinel>{%
3505 \if\relax\detokenize{#2}\relax
3506 \expandafter\@firstoftwo
3507 \else
3508 \expandafter\@secondoftwo
3509 \fi
3510 {\FV@EscVDetok@ScanTokenEscSpace{#1}}%
3511 {\noexpand\FV@<InvalidEscape>\FV@EscVDetok@ScanToken}}
3512 % \end{macrocode}
3513 % \end{macro}
3514 %
3515 % \begin{macro}{\FV@EscVDetok@ScanTokenEscSpace}
3516 % This is modified to drop |#1|, which will be the backslash.
3517 % \begin{macrocode}
3518 \begingroup
3519 \catcode`\ =12%
3520 \gdef\FV@EscVDetok@ScanTokenEscSpace#1{ \FV@EscVDetok@ScanToken}%
3521 \endgroup
3522 % \end{macrocode}
3523 % \end{macro}
3524 %
3525 % \begin{macro}{\FV@EscVDetok@ScanTokenCW}
3526 % This is modified to accept an additional argument, since the control word is now split into backslash plus letters.
3527 % \begin{macrocode}
3528 \begingroup
3529 \catcode`\ =12%
3530 \gdef\FV@EscVDetok@ScanTokenCW#1#2#3{%
3531 \ifcat\noexpand#2a%
3532 \expandafter\@firstoftwo%
3533 \else%
3534 \expandafter\@secondoftwo%
3535 \fi%
3536 {#2 \FV@EscVDetok@ScanToken#3}%
3537 {#2\FV@EscVDetok@ScanToken#3}}
3538 \endgroup
3539 % \end{macrocode}
3540 % \end{macro}
3541 %
3542 %
3543 % \paragraph{Detokenize as if the original source were tokenized verbatim, except for backslash escapes of non-catcode 11 characters, then convert to PDF string}\hfill\\
3544 %
3545 % \begin{macro}{\FVExtraPDFStringEscapedVerbatimDetokenize}
3546 % This is identical to \cmd{\FVExtraEscapedVerbatimDetokenize}, except that the output is converted to a valid PDF string. All spaces are represented with the octal escape \cmd{\040} to prevent adjacent spaces from being merged. There is no alternate implementation for restricting escapes to ASCII symbols and punctuation. Typically, this would be used in an expansion-only context to create something like bookmarks, while \cmd{\FVExtraEscapedVerbatimDetokenize} (potentially with escape restrictions) would be used in parallel to generate whatever is actually typeset. Escape errors can be handled in generating what is typeset.
3547 % \begin{macrocode}
3548 \gdef\FVExtraPDFStringEscapedVerbatimDetokenize#1{%
3549 \FV@PDFStrEscVDetok@Scan{}#1^^C \FV@<Sentinel>}
3550 % \end{macrocode}
3551 % \end{macro}
3552 %
3553 % \begin{macro}{\FV@PDFStrEscVDetok@Scan}
3554 % \begin{macrocode}
3555 \gdef\FV@PDFStrEscVDetok@Scan#1 #2\FV@<Sentinel>{%
3556 \if\relax\detokenize{#2}\relax
3557 \expandafter\@firstoftwo
3558 \else
3559 \expandafter\@secondoftwo
3560 \fi
3561 {\FV@PDFStrEscVDetok@ScanEnd#1}%
3562 {\FV@PDFStrEscVDetok@ScanCont{#1}{#2}}}
3563 % \end{macrocode}
3564 % \end{macro}
3565 %
3566 % \begin{macro}{\FV@PDFStrEscVDetok@ScanEnd}
3567 % \begin{macrocode}
3568 \gdef\FV@PDFStrEscVDetok@ScanEnd#1^^C{%
3569 \if\relax\detokenize{#1}\relax
3570 \expandafter\@gobble
3571 \else
3572 \expandafter\@firstofone
3573 \fi
3574 {\expandafter\FV@PDFStrEscVDetok@ScanGroup\@gobble#1{\FV@<Sentinel>}}}
3575 % \end{macrocode}
3576 % \end{macro}
3577 %
3578 % \begin{macro}{\FV@PDFStrEscVDetok@ScanCont}
3579 % This is modified to use |\040| for the space. In the unescaped case, using a normal space here is fine, but in the escaped case, the preceding or following token could be an escaped space.
3580 % \begin{macrocode}
3581 \begingroup
3582 \catcode`\!=0\relax
3583 \catcode`\\=12!relax
3584 !gdef!FV@PDFStrEscVDetok@ScanCont#1#2{%
3585 !if!relax!detokenize{#1}!relax
3586 !expandafter!@gobble
3587 !else
3588 !expandafter!@firstofone
3589 !fi
3590 {!expandafter!FV@PDFStrEscVDetok@ScanGroup!@gobble#1{!FV@<Sentinel>}}%
3591 \040%<-space
3592 !FV@PDFStrEscVDetok@Scan{}#2!FV@<Sentinel>}%
3593 !catcode`!\=0!relax
3594 \endgroup
3595 % \end{macrocode}
3596 % \end{macro}
3597 %
3598 % \begin{macro}{\FV@PDFStrEscVDetok@ScanGroup}
3599 % \begin{macrocode}
3600 \gdef\FV@PDFStrEscVDetok@ScanGroup#1#{%
3601 \FV@PDFStrEscVDetok@ScanToken#1\FV@Sentinel
3602 \FV@PDFStrEscVDetok@ScanGroup@i}
3603 % \end{macrocode}
3604 % \end{macro}
3605 %
3606 % \begin{macro}{\FV@PDFStrEscVDetok@ScanGroup@i}
3607 % \begin{macrocode}
3608 \gdef\FV@PDFStrEscVDetok@ScanGroup@i#1{%
3609 \if\relax\detokenize{#1}\relax
3610 \expandafter\@firstoftwo
3611 \else
3612 \expandafter\@secondoftwo
3613 \fi
3614 {\FV@PDFStrEscVDetok@ScanEmptyGroup}%
3615 {\FV@PDFStrEscVDetok@ScanGroup@ii{}#1\FV@<Sentinel>^^C}}
3616 % \end{macrocode}
3617 % \end{macro}
3618 %
3619 % \begin{macro}{\FV@PDFStrEscVDetok@ScanEmptyGroup}
3620 % \begin{macrocode}
3621 \begingroup
3622 \catcode`\(=1
3623 \catcode`\)=2
3624 \catcode`\{=12
3625 \catcode`\}=12
3626 \gdef\FV@PDFStrEscVDetok@ScanEmptyGroup({}\FV@PDFStrEscVDetok@ScanGroup)
3627 \endgroup
3628 % \end{macrocode}
3629 % \end{macro}
3630 %
3631 % \begin{macro}{\FV@PDFStrEscVDetok@ScanGroup@ii}
3632 % \begin{macrocode}
3633 \begingroup
3634 \catcode`\(=1
3635 \catcode`\)=2
3636 \catcode`\{=12
3637 \catcode`\}=12
3638 \gdef\FV@PDFStrEscVDetok@ScanGroup@ii#1\FV@<Sentinel>#2^^C(%
3639 \if\relax\detokenize(#2)\relax
3640 \expandafter\@firstofone
3641 \else
3642 \expandafter\@gobble
3643 \fi
3644 ({\FV@PDFStrEscVDetok@Scan#1^^C \FV@<Sentinel>}\FV@PDFStrEscVDetok@ScanGroup))
3645 \endgroup
3646 % \end{macrocode}
3647 % \end{macro}
3648 %
3649 % \begin{macro}{\FV@PDFStrEscVDetok@ScanToken}
3650 % \begin{macrocode}
3651 \gdef\FV@PDFStrEscVDetok@ScanToken#1{%
3652 \ifx\FV@Sentinel#1%
3653 \expandafter\@gobble
3654 \else
3655 \expandafter\@firstofone
3656 \fi
3657 {\expandafter\FV@PDFStrEscVDetok@ScanToken@i\detokenize{#1}^^C \FV@<Sentinel>}}
3658 % \end{macrocode}
3659 % \end{macro}
3660 %
3661 % \begin{macro}{\FV@PDFStrEscVDetok@ScanToken@i}
3662 % \begin{macrocode}
3663 \gdef\FV@PDFStrEscVDetok@ScanToken@i#1 #2\FV@<Sentinel>{%
3664 \if\relax\detokenize{#2}\relax
3665 \expandafter\@firstoftwo
3666 \else
3667 \expandafter\@secondoftwo
3668 \fi
3669 {\FV@PDFStrEscVDetok@ScanTokenNoSpace#1}%
3670 {\FV@PDFStrEscVDetok@ScanTokenWithSpace{#1}}}
3671 % \end{macrocode}
3672 % \end{macro}
3673 %
3674 % \begin{macro}{\FV@PDFStrEscVDetok@ScanTokenNoSpace}
3675 % This was modified to add \cmd{\FVExtraPDFStringEscapeChar}
3676 % \begin{macrocode}
3677 \gdef\FV@PDFStrEscVDetok@ScanTokenNoSpace#1#2^^C{%
3678 \if\relax\detokenize{#2}\relax
3679 \expandafter\@firstoftwo
3680 \else
3681 \expandafter\@secondoftwo
3682 \fi
3683 {\FVExtraPDFStringEscapeChar{#1}\FV@PDFStrEscVDetok@ScanToken}%
3684 {\FVExtraPDFStringEscapeChar{#2}\FV@PDFStrEscVDetok@ScanToken}}
3685 % \end{macrocode}
3686 % \end{macro}
3687 %
3688 %
3689 % \begin{macro}{\FV@PDFStrEscVDetok@ScanTokenWithSpace}
3690 % \begin{macrocode}
3691 \gdef\FV@PDFStrEscVDetok@ScanTokenWithSpace#1{%
3692 \if\relax\detokenize{#1}\relax
3693 \expandafter\@firstoftwo
3694 \else
3695 \expandafter\@secondoftwo
3696 \fi
3697 {\FV@PDFStrEscVDetok@ScanTokenActiveSpace}%
3698 {\FV@PDFStrEscVDetok@ScanTokenWithSpace@i#1\FV@<Sentinel>}}
3699 % \end{macrocode}
3700 % \end{macro}
3701 %
3702 % \begin{macro}{\FV@PDFStrEscVDetok@ScanTokenActiveSpace}
3703 % This is modified to use |\040| for the space.
3704 % \begin{macrocode}
3705 \begingroup
3706 \catcode`\!=0\relax
3707 \catcode`\\=12!relax
3708 !gdef!FV@PDFStrEscVDetok@ScanTokenActiveSpace{\040!FV@PDFStrEscVDetok@ScanToken}%
3709 !catcode`!\=0!relax
3710 \endgroup
3711 % \end{macrocode}
3712 % \end{macro}
3713 %
3714 % \begin{macro}{\FV@PDFStrEscVDetok@ScanTokenWithSpace@i}
3715 % \begin{macrocode}
3716 \gdef\FV@PDFStrEscVDetok@ScanTokenWithSpace@i#1#2\FV@<Sentinel>{%
3717 \if\relax\detokenize{#2}\relax
3718 \expandafter\@firstoftwo
3719 \else
3720 \expandafter\@secondoftwo
3721 \fi
3722 {\FV@PDFStrEscVDetok@ScanTokenEscSpace{#1}}%
3723 {\FV@PDFStrEscVDetok@ScanTokenCW{#1}{#2}}}
3724 % \end{macrocode}
3725 % \end{macro}
3726 %
3727 % \begin{macro}{\FV@PDFStrEscVDetok@ScanTokenEscSpace}
3728 % This is modified to drop |#1|, which will be the backslash, and use |\040| for the space.
3729 % \begin{macrocode}
3730 \begingroup
3731 \catcode`\!=0\relax
3732 \catcode`\\=12!relax
3733 !gdef!FV@PDFStrEscVDetok@ScanTokenEscSpace#1{\040!FV@PDFStrEscVDetok@ScanToken}
3734 !catcode`!\=0!relax
3735 \endgroup
3736 % \end{macrocode}
3737 % \end{macro}
3738 %
3739 % \begin{macro}{\FV@PDFStrEscVDetok@ScanTokenCW}
3740 % This is modified to use \cmd{\FVExtraPDFStringEscapeChars}.
3741 % \begin{macrocode}
3742 \begingroup
3743 \catcode`\ =12%
3744 \gdef\FV@PDFStrEscVDetok@ScanTokenCW#1#2#3{%
3745 \ifcat\noexpand#2a%
3746 \expandafter\@firstoftwo%
3747 \else%
3748 \expandafter\@secondoftwo%
3749 \fi%
3750 {\FVExtraPDFStringEscapeChars{#2} \FV@PDFStrEscVDetok@ScanToken#3}%
3751 {\FVExtraPDFStringEscapeChars{#2}\FV@PDFStrEscVDetok@ScanToken#3}}
3752 \endgroup
3753 % \end{macrocode}
3754 % \end{macro}
3755 %
3756 %
3757 %
3758 %
3759 % \paragraph{Detokenization wrappers}\hfill\\
3760 %
3761 % \begin{macro}{\FVExtraDetokenizeVArg}
3762 % Detokenize a verbatim argument read by \cmd{\FVExtraReadVArg}. This is a wrapper around \cmd{\FVExtraVerbatimDetokenize} that adds some additional safety by ensuring |^^C| is \cmd{\active} with an appropriate definition, at the cost of not working in an expansion-only context. This tradeoff isn't an issue when working with \cmd{\FVExtraReadVArg}, because it has the same expansion limitations.
3763 % \begin{macrocode}
3764 \gdef\FVExtraDetokenizeVArg#1#2{%
3765 \begingroup
3766 \catcode`\^^C=\active
3767 \let^^C\FV@Sentinel
3768 \edef\FV@Tmp{\FVExtraVerbatimDetokenize{#2}}%
3769 \expandafter\FV@DetokenizeVArg@i\expandafter{\FV@Tmp}{#1}}
3770 \gdef\FV@DetokenizeVArg@i#1#2{%
3771 \endgroup
3772 #2{#1}}
3773 % \end{macrocode}
3774 % \end{macro}
3775 %
3776 % \begin{macro}{\FVExtraDetokenizeEscVArg}
3777 % This is the same as \cmd{\FVExtraDetokenizeVArg}, except it is intended to work with \cmd{\FVExtraReadEscVArg} by using \cmd{\FVExtraEscapedVerbatimDetokenize}.
3778 % \begin{macrocode}
3779 \gdef\FVExtraDetokenizeEscVArg#1#2{%
3780 \begingroup
3781 \catcode`\^^C=\active
3782 \let^^C\FV@Sentinel
3783 \edef\FV@Tmp{\FVExtraEscapedVerbatimDetokenize{#2}}%
3784 \expandafter\FV@DetokenizeVArg@i\expandafter{\FV@Tmp}{#1}}
3785 % \end{macrocode}
3786 % \end{macro}
3787 %
3788 % \begin{macro}{\FVExtraDetokenizeREscVArg}
3789 % \begin{macrocode}
3790 \gdef\FVExtraDetokenizeREscVArg#1#2{%
3791 \begingroup
3792 \catcode`\^^C=\active
3793 \let^^C\FV@Sentinel
3794 \let\FV@EscVDetok@ScanTokenNoSpace\FV@REscVDetok@ScanTokenNoSpace
3795 \let\FV@EscVDetok@ScanTokenWithSpace@i\FV@REscVDetok@ScanTokenWithSpace@i
3796 \edef\FV@Tmp{\FVExtraEscapedVerbatimDetokenize{#2}}%
3797 \expandafter\FV@DetokenizeREscVArg@InvalidEscapeCheck\FV@Tmp\FV@<InvalidEscape>\FV@<Sentinel>
3798 \expandafter\FV@DetokenizeVArg@i\expandafter{\FV@Tmp}{#1}}
3799 \gdef\FV@DetokenizeREscVArg@InvalidEscapeCheck#1\FV@<InvalidEscape>#2\FV@<Sentinel>{%
3800 \if\relax\detokenize{#2}\relax
3801 \expandafter\@gobble
3802 \else
3803 \expandafter\@firstofone
3804 \fi
3805 {\PackageError{fvextra}%
3806 {Invalid backslash escape; only escape ASCII symbols and punctuation}%
3807 {Only use \@backslashchar <char> for ASCII symbols and punctuation}}}
3808 % \end{macrocode}
3809 % \end{macro}
3810 %
3811 % End catcodes for this subsection:
3812 % \begin{macrocode}
3813 \endgroup
3814 % \end{macrocode}
3815 %
3816 %
3817 %
3818 %
3819 % \subsubsection{Retokenizing detokenized arguments}
3820 %
3821 % \begin{macro}{\FV@RetokVArg@Read}
3822 % Read all tokens up to \cmd{\active} |^^C^^M|, then save them in a macro for further use. This is used to read tokens inside \cmd{\scantokens} during retokenization. The \cmd{\endgroup} disables catcode modifications that will have been put in place for the reading process, including making |^^C| and |^^M| \cmd{\active}.
3823 % \begin{macrocode}
3824 \begingroup
3825 \catcode`\^^C=\active%
3826 \catcode`\^^M=\active%
3827 \gdef\FV@RetokVArg@Read#1^^C^^M{%
3828 \endgroup%
3829 \def\FV@TmpRetoked{#1}}%
3830 \endgroup
3831 % \end{macrocode}
3832 % \end{macro}
3833 %
3834 % \begin{macro}{\FVExtraRetokenizeVArg}
3835 % This retokenizes the detokenized output of something like \cmd{\FVExtraVerbatimDetokenize} or \cmd{\FVExtraDetokenizeVArg}. |#1| is a macro that receives the output, |#2| sets catcodes but includes no \cmd{\begingroup} or \cmd{\endgroup}, and |#3| is the detokenized characters. \cmd{\FV@RetokVArg@Read} contains an \cmd{\endgroup} that returns catcodes to their prior state.
3836 %
3837 % This is a somewhat atypical use of \cmd{\scantokens}. There is no |\everyeof{\noexpand}| to handle the end-of-file marker, and no |\endlinechar=-1| to ignore the end-of-line token so that it does not become a space. Rather, the end-of-line |^^M| is made \cmd{\active} and used as a delimiter by \cmd{\FV@RetokVArg@Read}, which reads characters under the new catcode regime, then stores them unexpanded in \cmd{\FV@TmpRetoked}.
3838 %
3839 % Inside \cmd{\scantokens} is |^^B#3^^C|. This becomes |^^B#3^^C^^M| once \cmd{\scantokens} inserts the end-of-line token. |^^B| is \cmd{\let} to \cmd{\FV@RetokVArg@Read}, rather than using \cmd{\FV@RetokVArg@Read} directly, because \cmd{\scantokens} acts as a \cmd{\write} followed by \cmd{\input}. That means that a command word like \cmd{\FV@RetokVArg@Read} will have a space inserted after it, while an \cmd{\active} character like |^^B| will not. Using |^^B| is a way to avoid needing to remove this space; it is simpler not to handle the scenario where \cmd{\FV@RetokVArg@Read} introduces a space and the detokenized characters also start with a space. The |^^C| is needed because trailing spaces on a line are automatically stripped, so a non-space character must be part of the delimiting token sequence.
3840 % \begin{macrocode}
3841 \begingroup
3842 \catcode`\^^B=\active
3843 \catcode`\^^C=\active
3844 \gdef\FVExtraRetokenizeVArg#1#2#3{%
3845 \begingroup
3846 #2%
3847 \catcode`\^^B=\active
3848 \catcode`\^^C=\active
3849 \catcode`\^^M=\active
3850 \let^^B\FV@RetokVArg@Read
3851 \let^^C\@empty
3852 \FV@DefEOLEmpty
3853 \scantokens{^^B#3^^C}%
3854 \expandafter\FV@RetokenizeVArg@i\expandafter{\FV@TmpRetoked}{#1}}%
3855 \gdef\FV@RetokenizeVArg@i#1#2{%
3856 #2{#1}}
3857 \endgroup
3858 % \end{macrocode}
3859 % \end{macro}
3860 %
3861 %
3862 %
3863 %
3864 % \subsection{Hooks}
3865 %
3866 %
3867 % \begin{macro}{\FV@UseKeyValues@Hook}
3868 % This hook is useful for tasks that must be accomplished immediately after the beginning of a command or environment, just after |\begingroup|, before any catcode or similar changes are made. Hooking into |\FV@CatCodes| is not a straightforward alternative, because it may be invoked multiple times in the original \fancyvrb\ code, plus additional times in \fvextra\ code during retokenization. Hooking into |\FV@FormattingPrep| is not a reliable alternative, because it may be invoked before or after |\FV@CatCodes|.
3869 % \begin{macrocode}
3870 \let\FV@UseKeyValues@Hook\@empty
3871 \expandafter\def\expandafter\FV@UseKeyValues\expandafter{%
3872 \FV@UseKeyValues\FV@UseKeyValues@Hook}
3873 % \end{macrocode}
3874 % \end{macro}
3875 %
3876 %
3877 % \begin{macro}{\FV@FormattingPrep@PreHook,\FV@FormattingPrep@PostHook}
3878 % These are hooks for extending |\FV@FormattingPrep|. |\FV@FormattingPrep| is inside a group, before the beginning of processing, so it is a good place to add extension code. These hooks are used for such things as tweaking math mode behavior and preparing for |breakbefore| and |breakafter|. The |PreHook| should typically be used, unless \pkg{fancyvrb}'s font settings, whitespace setup, and active character definitions are needed for extension code.
3879 % \begin{macrocode}
3880 \let\FV@FormattingPrep@PreHook\@empty
3881 \let\FV@FormattingPrep@PostHook\@empty
3882 \expandafter\def\expandafter\FV@FormattingPrep\expandafter{%
3883 \expandafter\FV@FormattingPrep@PreHook\FV@FormattingPrep\FV@FormattingPrep@PostHook}
3884 % \end{macrocode}
3885 % \end{macro}
3886 %
3887 %
3888 % \begin{macro}{\FV@PygmentsHook}
3889 % This is a hook for turning on Pygments-related features for packages like \pkg{minted} and \pkg{pythontex} (\cref{sec:impl:pygments}). It needs to be the first thing in |\FV@FormattingPrep@PreHook|, since it will potentially affect some of the later things in the hook. It is activated by |\VerbatimPygments|.
3890 % \begin{macrocode}
3891 \def\FV@PygmentsHook{}
3892 \g@addto@macro\FV@FormattingPrep@PreHook{\FV@PygmentsHook}
3893 % \end{macrocode}
3894 % \end{macro}
3895 %
3896 %
3897 % \subsection{Escaped characters}
3898 % \begin{macro}{\FV@EscChars}
3899 % Define versions of common escaped characters that reduce to raw characters. This is useful, for example, when working with text that is almost verbatim, but was captured in such a way that some escapes were unavoidable.
3900 % \begin{macrocode}
3901 \edef\FV@hashchar{\string#}
3902 \edef\FV@dollarchar{\string$}
3903 \edef\FV@ampchar{\string&}
3904 \edef\FV@underscorechar{\string_}
3905 \edef\FV@caretchar{\string^}
3906 \edef\FV@tildechar{\string~}
3907 \edef\FV@leftsquarebracket{\string[}
3908 \edef\FV@rightsquarebracket{\string]}
3909 \edef\FV@commachar{\string,}
3910 \newcommand{\FV@EscChars}{%
3911 \let\#\FV@hashchar
3912 \let\%\@percentchar
3913 \let\{\@charlb
3914 \let\}\@charrb
3915 \let\$\FV@dollarchar
3916 \let\&\FV@ampchar
3917 \let\_\FV@underscorechar
3918 \let\^\FV@caretchar
3919 \let\\\@backslashchar
3920 \let~\FV@tildechar
3921 \let\~\FV@tildechar
3922 \let\[\FV@leftsquarebracket
3923 \let\]\FV@rightsquarebracket
3924 \let\,\FV@commachar
3925 } %$ <- highlighting
3926 % \end{macrocode}
3927 % \end{macro}
3928 %
3929 %
3930 %
3931 % \subsection{Inline-only options}
3932 %
3933 % Create \cmd{\fvinlineset} for inline-only options. Note that this only applies to new or reimplemented inline commands that use \cmd{\FV@UseInlineKeyValues}.
3934 % \begin{macro}{\FV@InlineKeyValues}
3935 % \begin{macrocode}
3936 \def\FV@InlineKeyValues{}
3937 % \end{macrocode}
3938 % \end{macro}
3939 %
3940 % \begin{macro}{\fvinlineset}
3941 % \begin{macrocode}
3942 \def\fvinlineset#1{%
3943 \expandafter\def\expandafter\FV@InlineKeyValues\expandafter{%
3944 \FV@InlineKeyValues#1,}}
3945 % \end{macrocode}
3946 % \end{macro}
3947
3948 % \begin{macro}{\FV@UseInlineKeyValues}
3949 % \begin{macrocode}
3950 \def\FV@UseInlineKeyValues{%
3951 \expandafter\fvset\expandafter{\FV@InlineKeyValues}%
3952 \FV@ApplyBreakAnywhereInlineStretch}
3953 % \end{macrocode}
3954 % \end{macro}
3955 %
3956 %
3957 %
3958 %
3959 % \subsection{Reimplementations}
3960 % \label{sec:impl:reimplementations}
3961 %
3962 % \fvextra\ reimplements some \fancyvrb\ internals. The patches in \cref{sec:impl:patches} fix bugs, handle edge cases, and extend existing functionality in logical ways, while leaving default \fancyvrb\ behavior largely unchanged. In contrast, reimplementations add features by changing existing behavior in significant ways. As a result, there is a boolean option |extra| that allows them to be disabled.
3963 %
3964 %
3965 % \subsubsection{\texttt{extra} option}
3966 % Boolean option that governs whether reimplemented commands and environments should be used, rather than the original definitions.
3967 % \begin{macro}{FV@extra}
3968 % \begin{macrocode}
3969 \newbool{FV@extra}
3970 % \end{macrocode}
3971 % \end{macro}
3972 %
3973 % \begin{macro}{extra}
3974 % \begin{macrocode}
3975 \define@booleankey{FV}{extra}%
3976 {\booltrue{FV@extra}}%
3977 {\boolfalse{FV@extra}}
3978 \fvset{extra=true}
3979 % \end{macrocode}
3980 % \end{macro}
3981 %
3982 %
3983 % \subsubsection{\cmd{\FancyVerbFormatInline}}
3984 % This allows customization of inline verbatim material. It is the inline equivalent of \cmd{\FancyVerbFormatLine} and \cmd{\FancyVerbFormatText}.
3985 % \begin{macro}{\FancyVerbFormatInline}
3986 % \begin{macrocode}
3987 \def\FancyVerbFormatInline#1{#1}
3988 % \end{macrocode}
3989 % \end{macro}
3990 %
3991 %
3992 % \subsubsection{\texttt{\textbackslash Verb}}
3993 %
3994 % \cmd{\Verb} is reimplemented so that it functions as well as possible when used within other commands.
3995 %
3996 % \cmd{\verb} cannot be used inside other commands. The original \fancyvrb\ implementation of \cmd{\Verb} does work inside other commands, but being inside other commands reduces its functionality since there is no attempt at retokenization. When used inside other commands, it essentially reduces to \cmd{\texttt}. \cmd{\Verb} also fails when the delimiting characters are active, since it assumes that the closing delimiting character will have catcode 12.
3997 %
3998 % \fvextra's re-implemented \cmd{\Verb} uses \cmd{\scantokens} and careful consideration of catcodes to (mostly) remedy this. It also adds support for paired curly braces |{...}| as the delimiters for the verbatim argument, since this is often convenient when \cmd{\Verb} is used within another command. The original \cmd{\Verb} implementation is completely incompatible with curly braces being used as delimiters, so this doesn't affect backward compatibility.
3999 %
4000 % The re-implemented \cmd{\Verb} is constructed with \cmd{\FVExtraRobustCommand} so that it will function correctly after being in an expansion-only context, so long as the argument is delimited with curly braces.
4001 %
4002 % \begin{macro}{\Verb}
4003 % \begin{macrocode}
4004 \def\Verb{%
4005 \FVExtraRobustCommand\RobustVerb\FVExtraUnexpandedReadStarOArgBVArg}
4006 % \end{macrocode}
4007 % \end{macro}
4008 %
4009 % \begin{macro}{\RobustVerb}
4010 % \begin{macrocode}
4011 \protected\def\RobustVerb{\FV@Command{}{Verb}}
4012 \FVExtrapdfstringdefDisableCommands{%
4013 \def\RobustVerb{}}
4014 % \end{macrocode}
4015 % \end{macro}
4016 %
4017 % \begin{macro}{\FVC@Verb@FV}
4018 % Save the original \fancyvrb\ definition of \cmd{\FVC@Verb}, so that the |extra| option can switch back to it.
4019 % \begin{macrocode}
4020 \let\FVC@Verb@FV\FVC@Verb
4021 % \end{macrocode}
4022 % \end{macro}
4023 %
4024 % \begin{macro}{\FVC@Verb}
4025 % Redefine \cmd{\FVC@Verb} so that it will adjust based on |extra|.
4026 % \begin{macrocode}
4027 \def\FVC@Verb{%
4028 \begingroup
4029 \FV@UseInlineKeyValues\FV@UseKeyValues
4030 \ifFV@extra
4031 \expandafter\endgroup\expandafter\FVC@Verb@Extra
4032 \else
4033 \expandafter\endgroup\expandafter\FVC@Verb@FV
4034 \fi}
4035 % \end{macrocode}
4036 % \end{macro}
4037 %
4038 % \begin{macro}{\FVC@Verb@Extra}
4039 % \fvextra\ reimplementation of \cmd{\FVC@Verb}.
4040 %
4041 % When used after expansion, there is a check for valid delimiters, curly braces. If incorrect delimiters are used, and there are no following curly braces, then the reader macro \cmd{\FVExtraUnexpandedReadStarOArgBVArg} will give an error about unmatched braces. However, if incorrect delimiters are used, and there \emph{are} following braces in a subsequent command, then this error will be triggered, preventing interference with the following command by the reader macro.
4042 % \begin{macrocode}
4043 \def\FVC@Verb@Extra{%
4044 \ifbool{FVExtraRobustCommandExpanded}%
4045 {\@ifnextchar\bgroup
4046 {\FVC@Verb@Extra@i}%
4047 {\PackageError{fvextra}%
4048 {\string\Verb\space delimiters must be paired curly braces in this context}%
4049 {Use curly braces as delimiters}}}%
4050 {\FVC@Verb@Extra@i}}
4051 % \end{macrocode}
4052 % \end{macro}
4053 %
4054 % \begin{macro}{\FVC@Verb@Extra@i}
4055 % \begin{macrocode}
4056 \def\FVC@Verb@Extra@i{%
4057 \begingroup
4058 \ifbool{FV@vargsingleline}%
4059 {\let\FV@Reader\FVExtraReadVArgSingleLine}%
4060 {\let\FV@Reader\FVExtraReadVArg}%
4061 \FV@Reader{%
4062 \FV@UseInlineKeyValues\FV@UseKeyValues\FV@FormattingPrep
4063 \FVExtraDetokenizeVArg{%
4064 \FVExtraRetokenizeVArg{\FVC@Verb@Extra@ii}{\FV@CatCodes}}}}
4065 % \end{macrocode}
4066 % \end{macro}
4067 %
4068 % \begin{macro}{\FVC@Verb@Extra@ii}
4069 % |breaklines| is only applied when there is no background color, since |\colorbox| prevents line breaks.
4070 % \begin{macrocode}
4071 \def\FVC@Verb@Extra@ii#1{%
4072 \ifx\FancyVerbBackgroundColor\relax
4073 \expandafter\@firstoftwo
4074 \else
4075 \expandafter\@secondoftwo
4076 \fi
4077 {\ifbool{FV@breaklines}%
4078 {\FV@InsertBreaks{\FancyVerbFormatInline}{#1}}%
4079 {\mbox{\FancyVerbFormatInline{#1}}}}%
4080 {\setlength{\FV@TmpLength}{\fboxsep}%
4081 \ifx\FancyVerbBackgroundColorPadding\relax
4082 \setlength{\fboxsep}{0pt}%
4083 \else
4084 \setlength{\fboxsep}{\FancyVerbBackgroundColorPadding}%
4085 \fi
4086 \colorbox{\FancyVerbBackgroundColor}{%
4087 \setlength{\fboxsep}{\FV@TmpLength}%
4088 \FancyVerbBackgroundColorVPhantom\FancyVerbFormatInline{#1}}}%
4089 \endgroup}
4090 % \end{macrocode}
4091 % \end{macro}
4092 %
4093 %
4094 % \subsubsection{\cmd{\SaveVerb}}
4095 %
4096 % This is reimplemented, following \cmd{\Verb} as a template, so that both \cmd{\Verb} and \cmd{\SaveVerb} are using the same reading and tokenization macros. This also adds support for \cmd{\fvinlineset}. Since the definition in \fancyvrb\ is
4097 %\begin{verbatim}
4098 %\def\SaveVerb{\FV@Command{}{SaveVerb}}
4099 %\end{verbatim}
4100 % only the internal macros need to be reimplemented.
4101 %
4102 % \begin{macro}{\FVC@SaveVerb@FV}
4103 % \begin{macrocode}
4104 \let\FVC@SaveVerb@FV\FVC@SaveVerb
4105 % \end{macrocode}
4106 % \end{macro}
4107 %
4108 % \begin{macro}{\FVC@SaveVerb}
4109 % \begin{macrocode}
4110 \def\FVC@SaveVerb{%
4111 \begingroup
4112 \FV@UseInlineKeyValues\FV@UseKeyValues
4113 \ifFV@extra
4114 \expandafter\endgroup\expandafter\FVC@SaveVerb@Extra
4115 \else
4116 \expandafter\endgroup\expandafter\FVC@SaveVerb@FV
4117 \fi}
4118 % \end{macrocode}
4119 % \end{macro}
4120 %
4121 % \begin{macro}{\FVC@SaveVerb@Extra}
4122 % In addition to following the \cmd{\Verb} implementation, this saves a raw version of the text to allow |retokenize| with \cmd{\UseVerb}. The raw version is also used for conversion to a PDF string if that is needed.
4123 % \begin{macrocode}
4124 \def\FVC@SaveVerb@Extra#1{%
4125 \@namedef{FV@SV@#1}{}%
4126 \@namedef{FV@SVRaw@#1}{}%
4127 \begingroup
4128 \ifbool{FV@vargsingleline}%
4129 {\let\FV@Reader\FVExtraReadVArgSingleLine}%
4130 {\let\FV@Reader\FVExtraReadVArg}%
4131 \FV@Reader{%
4132 \FVC@SaveVerb@Extra@i{#1}}}
4133 % \end{macrocode}
4134 % \end{macro}
4135 %
4136 % \begin{macro}{\FVC@SaveVerb@Extra@i}
4137 % \begin{macrocode}
4138 \def\FVC@SaveVerb@Extra@i#1#2{%
4139 \FV@UseInlineKeyValues\FV@UseKeyValues\FV@FormattingPrep
4140 \FVExtraDetokenizeVArg{%
4141 \FVExtraRetokenizeVArg{\FVC@SaveVerb@Extra@ii{#1}{#2}}{\FV@CatCodes}}{#2}}
4142 % \end{macrocode}
4143 % \end{macro}
4144 %
4145 % \begin{macro}{\FVC@SaveVerb@Extra@ii}
4146 % \begin{macrocode}
4147 \def\FVC@SaveVerb@Extra@ii#1#2#3{%
4148 \global\let\FV@AfterSave\FancyVerbAfterSave
4149 \endgroup
4150 \@namedef{FV@SV@#1}{#3}%
4151 \@namedef{FV@SVRaw@#1}{#2}%
4152 \FV@AfterSave}%
4153 % \end{macrocode}
4154 % \end{macro}
4155 %
4156 %
4157 % \subsubsection{\cmd{\UseVerb}}
4158 %
4159 % This adds support for \cmd{\fvinlineset} and line breaking. It also adds movable argument and PDF string support. A new option |retokenize| is defined that determines whether the typeset output is based on the |commandchars| and |codes| in place when \cmd{\SaveVerb} was used (default), or is retokenized under current |commandchars| and |codes|.
4160 %
4161 % \begin{macro}{FV@retokenize}
4162 % \begin{macro}{retokenize}
4163 % Whether \cmd{\UseVerb} uses saved verbatim with its original tokenization, or retokenizes under current |commandchars| and |codes|.
4164 % \begin{macrocode}
4165 \newbool{FV@retokenize}
4166 \define@booleankey{FV}{retokenize}%
4167 {\booltrue{FV@retokenize}}{\boolfalse{FV@retokenize}}
4168 % \end{macrocode}
4169 % \end{macro}
4170 % \end{macro}
4171 %
4172 %
4173 % \begin{macro}{\UseVerb}
4174 % \begin{macrocode}
4175 \def\UseVerb{%
4176 \FVExtraRobustCommand\RobustUseVerb\FVExtraUseVerbUnexpandedReadStarOArgMArg}
4177 % \end{macrocode}
4178 % \end{macro}
4179 %
4180 % \begin{macro}{\RobustUseVerb}
4181 % \begin{macrocode}
4182 \protected\def\RobustUseVerb{\FV@Command{}{UseVerb}}
4183 \FVExtrapdfstringdefDisableCommands{%
4184 \def\RobustUseVerb{}}
4185 % \end{macrocode}
4186 % \end{macro}
4187 %
4188 % \begin{macro}{\FVC@UseVerb@FV}
4189 % \begin{macrocode}
4190 \let\FVC@UseVerb@FV\FVC@UseVerb
4191 % \end{macrocode}
4192 % \end{macro}
4193 %
4194 %
4195 % \begin{macro}{\FVC@UseVerb}
4196 % \begin{macrocode}
4197 \def\FVC@UseVerb{%
4198 \begingroup
4199 \FV@UseInlineKeyValues\FV@UseKeyValues
4200 \ifFV@extra
4201 \expandafter\endgroup\expandafter\FVC@UseVerb@Extra
4202 \else
4203 \expandafter\endgroup\expandafter\FVC@UseVerb@FV
4204 \fi}
4205 % \end{macrocode}
4206 % \end{macro}
4207 %
4208 %
4209 % \begin{macro}{\FVC@UseVerb@Extra}
4210 % \begin{macrocode}
4211 \def\FVC@UseVerb@Extra#1{%
4212 \@ifundefined{FV@SV@#1}%
4213 {\FV@Error{Short verbatim text never saved to name `#1'}\FV@eha}%
4214 {\begingroup
4215 \FV@UseInlineKeyValues\FV@UseKeyValues\FV@FormattingPrep
4216 \ifbool{FV@retokenize}%
4217 {\expandafter\let\expandafter\FV@Tmp\csname FV@SVRaw@#1\endcsname
4218 \expandafter\FV@UseVerb@Extra@Retok\expandafter{\FV@Tmp}}%
4219 {\expandafter\let\expandafter\FV@Tmp\csname FV@SV@#1\endcsname
4220 \expandafter\FV@UseVerb@Extra\expandafter{\FV@Tmp}}}}
4221 % \end{macrocode}
4222 % \end{macro}
4223 %
4224 % \begin{macro}{\FV@UseVerb@Extra@Retok}
4225 % \begin{macrocode}
4226 \def\FV@UseVerb@Extra@Retok#1{%
4227 \FVExtraDetokenizeVArg{%
4228 \FVExtraRetokenizeVArg{\FV@UseVerb@Extra}{\FV@CatCodes}}{#1}}
4229 % \end{macrocode}
4230 % \end{macro}
4231 %
4232 % \begin{macro}{\FV@UseVerb@Extra}
4233 % \begin{macrocode}
4234 \let\FV@UseVerb@Extra\FVC@Verb@Extra@ii
4235 % \end{macrocode}
4236 % \end{macro}
4237 %
4238 %
4239 %
4240 %
4241 % \subsection{New commands and environments}
4242 % \label{sec:impl:new-commands-environments}
4243 %
4244 % \subsubsection{\cmd{\EscVerb}}
4245 % This is a variant of \cmd{\Verb} in which backslash escapes of the form |\<char>| are used for |<char>|. Backslash escapes are \emph{only} permitted for printable, non-alphanumeric ASCII characters. The argument is read under a normal catcode regime, so any characters that cannot be read under normal catcodes must always be escaped, and the argument must always be delimited by curly braces. This ensures that \cmd{\EscVerb} behaves identically whether or not it is used inside another command.
4246 %
4247 % \cmd{\EscVerb} is constructed with \cmd{\FVExtraRobustCommand} so that it will function correctly after being in an expansion-only context.
4248 %
4249 % \begin{macro}{\EscVerb}
4250 % Note that while the typeset mandatory argument will be read under normal catcodes, the reader macro for expansion is \cmd{\FVExtraUnexpandedReadStarOArgBEscVArg}. This reflects how the argument will be typeset.
4251 % \begin{macrocode}
4252 \def\EscVerb{%
4253 \FVExtraRobustCommand\RobustEscVerb\FVExtraUnexpandedReadStarOArgBEscVArg}
4254 % \end{macrocode}
4255 % \end{macro}
4256 %
4257 % \begin{macro}{\RobustEscVerb}
4258 % \begin{macrocode}
4259 \protected\def\RobustEscVerb{\FV@Command{}{EscVerb}}
4260 \FVExtrapdfstringdefDisableCommands{%
4261 \def\RobustEscVerb{}}
4262 % \end{macrocode}
4263 % \end{macro}
4264 %
4265 % \begin{macro}{\FVC@EscVerb}
4266 % Delimiting with curly braces is required, so that the command will always behave the same whether or not it has been through expansion.
4267 % \begin{macrocode}
4268 \def\FVC@EscVerb{%
4269 \@ifnextchar\bgroup
4270 {\FVC@EscVerb@i}%
4271 {\PackageError{fvextra}%
4272 {Invalid argument; argument must be delimited by paired curly braces}%
4273 {Delimit argument with curly braces}}}
4274 % \end{macrocode}
4275 % \end{macro}
4276 %
4277 % \begin{macro}{\FVC@EscVerb@i}
4278 % \begin{macrocode}
4279 \def\FVC@EscVerb@i#1{%
4280 \begingroup
4281 \FV@UseInlineKeyValues\FV@UseKeyValues\FV@FormattingPrep
4282 \FVExtraDetokenizeREscVArg{%
4283 \FVExtraRetokenizeVArg{\FVC@EscVerb@ii}{\FV@CatCodes}}{#1}}
4284 % \end{macrocode}
4285 % \end{macro}
4286 %
4287 % \begin{macro}{\FVC@EscVerb@ii}
4288 % \begin{macrocode}
4289 \let\FVC@EscVerb@ii\FVC@Verb@Extra@ii
4290 % \end{macrocode}
4291 % \end{macro}
4292 %
4293 %
4294 % \subsubsection{\cmd{VerbEnv}}
4295 % Environment variant of \cmd{\Verb}. Depending on how this is used in the future, it may be worth improving error message and error recovery functionality, using techniques from \pkg{fancyvrb}.
4296 % \begin{macro}{\VerbEnv}
4297 % \begin{macrocode}
4298 \def\VerbEnv{%
4299 \ifcsname @currenvir\endcsname
4300 \ifx\@currenvir\@empty
4301 \PackageError{fvextra}{VerbEnv is an environment}{VerbEnv is an environment}%
4302 \else
4303 \ifx\@currenvir\relax
4304 \PackageError{fvextra}{VerbEnv is an environment}{VerbEnv is an environment}%
4305 \fi
4306 \fi
4307 \else
4308 \PackageError{fvextra}{VerbEnv is an environment}{VerbEnv is an environment}%
4309 \fi
4310 \VerbatimEnvironment
4311 \FVExtraReadOArgBeforeVEnv{\expandafter\VerbEnv@i\expandafter{\FV@EnvironName}}}
4312 \def\VerbEnv@i#1#2{%
4313 \begingroup
4314 \let\do\@makeother\FVExtraDoSpecials
4315 \catcode`\ =\active
4316 \catcode`\^^I=\active
4317 \catcode`\^^M=\active
4318 \VerbEnv@ii{#1}{#2}}
4319 \begingroup
4320 \catcode`\!=0
4321 \catcode`\<=1
4322 \catcode`\>=2
4323 !catcode`!\=12
4324 !catcode`!{=12
4325 !catcode`!}=12
4326 !catcode`!^^M=!active%
4327 !gdef!VerbEnv@ii#1#2#3^^M<%
4328 !endgroup%
4329 !def!VerbEnv@CheckLine##1\end{#1}##2!FV@Sentinel<%
4330 !if!relax!detokenize<##2>!relax%
4331 !else%
4332 !PackageError<fvextra><Missing environment contents><Missing environment contents>%
4333 !let!VerbEnv@iii!VerbEnv@iii@Error%
4334 !fi>%
4335 !VerbEnv@CheckLine#3\end{#1}!FV@Sentinel%
4336 !VerbEnv@iii<#1><#2><#3>>%
4337 !endgroup%
4338 \def\VerbEnv@iii@Error#1#2#3{}
4339 \def\VerbEnv@iii#1#2#3{%
4340 \begingroup
4341 \let\do\@makeother\FVExtraDoSpecials
4342 \catcode`\ =10\relax
4343 \catcode`\^^M=\active
4344 \VerbEnv@iv{#1}{#2}{#3}}
4345 \begingroup
4346 \catcode`\!=0
4347 \catcode`\<=1
4348 \catcode`\>=2
4349 !catcode`!\=12
4350 !catcode`!{=12
4351 !catcode`!}=12
4352 !catcode`!^^M=!active%
4353 !gdef!VerbEnv@iv#1#2#3#4^^M<%
4354 !endgroup%
4355 !def!VerbEnv@CheckEndDelim##1\end{#1}##2!FV@Sentinel<%
4356 !if!relax!detokenize<##2>!relax%
4357 !PackageError<fvextra><Missing end for environment !FV@EnvironName><Add environment end>%
4358 !let!VerbEnv@v!VerbEnv@v@Error%
4359 !else%
4360 !VerbEnv@CheckEndLeading##1!FV@Sentinel%
4361 !VerbEnv@CheckEndTrailing##2!FV@Sentinel%
4362 !fi>%
4363 !def!VerbEnv@CheckEndTrailing##1\end{#1}!FV@Sentinel<%
4364 !if!relax!detokenize<##1>!relax%
4365 !else%
4366 !PackageError<fvextra>%
4367 <Discarded text after end of environment !FV@EnvironName>%
4368 <Discarded text after end of environment !FV@EnvironName>%
4369 !let!VerbEnv@v!VerbEnv@v@Error%
4370 !fi>%
4371 !VerbEnv@CheckEndDelim#4\end{#1}!FV@Sentinel%
4372 !VerbEnv@v<#2><#3>>%
4373 !endgroup
4374 \def\VerbEnv@CheckEndLeading{%
4375 \FVExtra@ifnextcharAny\@sptoken%
4376 {\VerbEnv@CheckEndLeading@Continue}%
4377 {\ifx\@let@token\FV@Sentinel
4378 \expandafter\VerbEnv@CheckEndLeading@End
4379 \else
4380 \expandafter\VerbEnv@CheckEndLeading@EndError
4381 \fi}}
4382 \def\VerbEnv@CheckEndLeading@Continue#1{%
4383 \VerbEnv@CheckEndLeading}
4384 \def\VerbEnv@CheckEndLeading@End#1\FV@Sentinel{}
4385 \def\VerbEnv@CheckEndLeading@EndError{%
4386 \PackageError{fvextra}%
4387 {Discarded text before end of environment \FV@EnvironName}%
4388 {Discarded text before end of environment \FV@EnvironName}%
4389 \let\VerbEnv@v\VerbEnv@v@Error}
4390 \def\VerbEnv@v@Error#1#2{}
4391 \def\VerbEnv@v#1#2{%
4392 \Verb[#1]{#2}%
4393 \expandafter\end\expandafter{\FV@EnvironName}}
4394 % \end{macrocode}
4395 % \end{macro}
4396 % \begin{macro}{\endVerbEnv}
4397 % \begin{macrocode}
4398 \def\endVerbEnv{\global\let\FV@EnvironName\relax}
4399 % \end{macrocode}
4400 % \end{macro}
4401 %
4402 %
4403 %
4404 % \subsubsection{\cmd{VerbatimWrite}}
4405 %
4406 % This environment writes its contents to a file verbatim. Differences from \fancyvrb's |VerbatimOut|:
4407 % \begin{itemize}
4408 % \item Multiple |VerbatimWrite| environments can write to the same file. The file is set via the |writefilehandle| option. This does mean that the user is responsible for creating a new file handle via |\newwrite| and then ideally invoking |\closeout| at the appropriate time.
4409 % \item By default, text is really written verbatim. This is accomplished by a combination of setting catcodes to 12 (other) and |\detokenize|. This can be customized using the new |writer| option, which defines a macro that performs any processing on each line before writing it to file. By default, all \fancyvrb\ options except for |VerbatimWrite|-specific options are ignored. This can be customized on a per-environment basis via environment optional arguments.
4410 % \end{itemize}
4411 %
4412 % \begin{macro}{writefilehandle, \FancyVerbWriteFileHandle}
4413 % Set file handle for |VerbatimWrite|.
4414 % \begin{macrocode}
4415 \define@key{FV}{writefilehandle}{%
4416 \FV@SetWrite#1\FV@Sentinel}
4417 \def\FV@SetWrite#1#2\FV@Sentinel{%
4418 \let\FancyVerbWriteFileHandle\relax
4419 \if\relax\detokenize{#2}\relax
4420 \let\FancyVerbWriteFileHandle#1\relax
4421 \fi
4422 \ifx\FancyVerbWriteFileHandle\relax
4423 \PackageError{fvextra}%
4424 {Missing or invalid file handle for write}%
4425 {Need file handle from \string\newwrite}%
4426 \fi}
4427 \let\FancyVerbWriteFileHandle\relax
4428 % \end{macrocode}
4429 % \end{macro}
4430 %
4431 %
4432 % \begin{macro}{writer,\FV@Writer}
4433 % Define writer macro that processes each line before writing.
4434 % \begin{macrocode}
4435 \define@key{FV}{writer}{%
4436 \let\FV@Writer#1\relax}
4437 \def\FancyVerbDefaultWriter#1{%
4438 \immediate\write\FancyVerbWriteFileHandle{\detokenize{#1}}}
4439 \fvset{writer=\FancyVerbDefaultWriter}
4440 % \end{macrocode}
4441 % \end{macro}
4442 %
4443 %
4444 % \begin{macro}{VerbatimWrite}
4445 % The environment implementation follows standard \fancyvrb\ environment style.
4446 %
4447 % A special write counter is used to track line numbers while avoiding incrementing the regular counter that is used for typeset code. Some macros do nothing with the default |writer|, but are needed to enable \fancyvrb\ options when a custom |writer| is used in conjunction with optional environment arguments. These include |\FancyVerbDefineActive|, |\FancyVerbFormatCom|, and |\FV@DefineTabOut|.
4448 % \begin{macrocode}
4449 \newcounter{FancyVerbWriteLine}
4450 \def\VerbatimWrite{%
4451 \FV@Environment
4452 {codes=,commandchars=none,commentchar=none,defineactive,%
4453 gobble=0,formatcom=,firstline,lastline}%
4454 {VerbatimWrite}}
4455 \def\FVB@VerbatimWrite{%
4456 \@bsphack
4457 \begingroup
4458 \setcounter{FancyVerbWriteLine}{0}%
4459 \let\c@FancyVerbLine\c@FancyVerbWriteLine
4460 \FV@UseKeyValues
4461 \FV@DefineWhiteSpace
4462 \def\FV@Space{\space}%
4463 \FV@DefineTabOut
4464 \let\FV@ProcessLine\FV@Writer
4465 \let\FV@FontScanPrep\relax
4466 \let\@noligs\relax
4467 \FancyVerbDefineActive
4468 \FancyVerbFormatCom
4469 \FV@Scan}
4470 \def\FVE@VerbatimWrite{%
4471 \endgroup
4472 \@esphack}
4473 \def\endVerbatimWrite{\FVE@VerbatimWrite}
4474 % \end{macrocode}
4475 % \end{macro}
4476 %
4477 %
4478 %
4479 % \subsubsection{\cmd{VerbatimBuffer}}
4480 %
4481 % This environment stores its contents verbatim in a ``buffer,'' a sequence of numbered macros each of which contains one line of the environment. The ``buffered'' lines can then be looped over for further processing or later use.
4482 %
4483 % By default, all \fancyvrb\ options except for |VerbatimBuffer|-specific options are ignored. This can be customized on a per-environment basis via environment optional arguments.
4484 %
4485 % \begin{macro}{afterbuffer,\FV@afterbuffer}
4486 % Macro that is inserted after the last line of the environment is buffered, immediately before the environment ends.
4487 % \begin{macrocode}
4488 \define@key{FV}{afterbuffer}{%
4489 \def\FV@afterbuffer{#1}}
4490 \fvset{afterbuffer=}
4491 % \end{macrocode}
4492 % \end{macro}
4493 %
4494 % \begin{macro}{\FancyVerbBufferIndex}
4495 % Current index in buffer during buffering. This is given a |FancyVerb*| macro name since it may be accessed by the user in defining custom |bufferer|.
4496 % \begin{macrocode}
4497 \def\FancyVerbBufferIndex{0}
4498 % \end{macrocode}
4499 % \end{macro}
4500 %
4501 % \begin{macro}{bufferer,\FV@Bufferer,\FancyVerbDefaultBufferer}
4502 % This is the macro that adds lines to the buffer. The default is designed to create a truly verbatim buffer via |\detokenize|.
4503 % \begin{macrocode}
4504 \define@key{FV}{bufferer}{%
4505 \let\FV@Bufferer=#1\relax}
4506 \def\FancyVerbDefaultBufferer#1{%
4507 \expandafter\xdef\csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname{%
4508 \detokenize{#1}}}
4509 \fvset{bufferer=\FancyVerbDefaultBufferer}
4510 % \end{macrocode}
4511 % \end{macro}
4512 %
4513 % \begin{macro}{bufferlengthname, \FancyVerbBufferLengthName}
4514 % Name of macro storing the length of the buffer.
4515 % \begin{macrocode}
4516 \define@key{FV}{bufferlengthname}{%
4517 \ifcsname#1\endcsname
4518 \else
4519 \expandafter\xdef\csname#1\endcsname{0}%
4520 \fi
4521 \def\FancyVerbBufferLengthName{#1}%
4522 \expandafter\def\expandafter\FV@bufferlengthmacro\expandafter{%
4523 \csname#1\endcsname}}
4524 \fvset{bufferlengthname=FancyVerbBufferLength}
4525 % \end{macrocode}
4526 % \end{macro}
4527 %
4528 % \begin{macro}{bufferlinename, \FancyVerbBufferLineName}
4529 % Base name of buffer line macros. This is given a |\FancyVerb*| macro name since it may be accessed by the user in defining custom |bufferer|.
4530 % \begin{macrocode}
4531 \define@key{FV}{bufferlinename}{%
4532 \def\FancyVerbBufferLineName{#1}}
4533 \fvset{bufferlinename=FancyVerbBufferLine}
4534 % \end{macrocode}
4535 % \end{macro}
4536 %
4537 % \begin{macro}{buffername}
4538 % Shortcut for setting |bufferlengthname| and |bufferlinename|.
4539 % \begin{macrocode}
4540 \define@key{FV}{buffername}{%
4541 \fvset{bufferlengthname=#1length,bufferlinename=#1line}}
4542 % \end{macrocode}
4543 % \end{macro}
4544 %
4545 % \begin{macro}{globalbuffer, FV@globalbuffer}
4546 % Whether buffer line macros and the buffer length macro are defined globally.
4547 % \begin{macrocode}
4548 \newbool{FV@globalbuffer}
4549 \define@booleankey{FV}{globalbuffer}%
4550 {\booltrue{FV@globalbuffer}}%
4551 {\boolfalse{FV@globalbuffer}}
4552 \fvset{globalbuffer=false}
4553 % \end{macrocode}
4554 % \end{macro}
4555 %
4556 %
4557 % \begin{macro}{VerbatimBuffer}
4558 % The environment implementation follows standard \fancyvrb\ environment style.
4559 %
4560 % A special buffer counter is used to track line numbers while avoiding incrementing the regular counter that is used for typeset code. Some macros do nothing with the default |bufferer|, but are needed to enable \fancyvrb\ options when a custom |bufferer| is used in conjunction with optional environment arguments. These include |\FancyVerbDefineActive| and |\FancyVerbFormatCom|. Since counters are global, the exact location of the |\setcounter| commands at the end of the environment relative to |\begingroup...\endgroup| is not important.
4561 % \begin{macrocode}
4562 \newcounter{FancyVerbBufferLine}
4563 \def\FancyVerbBufferDepth{0}
4564 \def\VerbatimBuffer{%
4565 \FV@Environment
4566 {codes=,commandchars=none,commentchar=none,defineactive,%
4567 gobble=0,formatcom=,firstline,lastline}%
4568 {VerbatimBuffer}}
4569 \def\FVB@VerbatimBuffer{%
4570 \@bsphack
4571 \xdef\FancyVerbBufferDepth{\the\numexpr\FancyVerbBufferDepth+1\relax}%
4572 \begingroup
4573 \FV@UseKeyValues
4574 \setcounter{FancyVerbBufferLine}{\FV@bufferlengthmacro}%
4575 \let\c@FancyVerbLine\c@FancyVerbBufferLine
4576 \xdef\FancyVerbBufferIndex{\FV@bufferlengthmacro}%
4577 \ifbool{FV@globalbuffer}%
4578 {}%
4579 {\expandafter\xdef\csname FV@setbufferlocalscopevars\FancyVerbBufferDepth\endcsname{%
4580 \unexpanded{\def\FV@oldbufferlength}%
4581 {\FV@bufferlengthmacro}%
4582 \unexpanded{\def\FV@bufferlengthmacro}%
4583 {\unexpanded\expandafter{\FV@bufferlengthmacro}}%
4584 \unexpanded{\def\FancyVerbBufferLineName}%
4585 {\unexpanded\expandafter{\FancyVerbBufferLineName}}}}%
4586 \ifx\FV@afterbuffer\@empty
4587 \else
4588 \ifx\FV@afterbuffer\relax
4589 \else
4590 \expandafter\global\expandafter
4591 \let\csname FV@afterbuffer\FancyVerbBufferDepth\endcsname\FV@afterbuffer
4592 \fi
4593 \fi
4594 \FV@DefineWhiteSpace
4595 \def\FV@ProcessLine{%
4596 \xdef\FancyVerbBufferIndex{\the\numexpr\FancyVerbBufferIndex+1\relax}%
4597 \FV@Bufferer}%
4598 \let\FV@FontScanPrep\relax
4599 \let\@noligs\relax
4600 \FancyVerbDefineActive
4601 \FancyVerbFormatCom
4602 \FV@Scan}
4603 \def\FVE@VerbatimBuffer{%
4604 \expandafter\xdef\FV@bufferlengthmacro{\FancyVerbBufferIndex}%
4605 \gdef\FancyVerbBufferIndex{0}%
4606 \endgroup
4607 \@esphack
4608 \ifcsname FV@afterbuffer\FancyVerbBufferDepth\endcsname
4609 \begingroup
4610 \csname FV@afterbuffer\FancyVerbBufferDepth\endcsname
4611 \endgroup
4612 \expandafter\global\expandafter
4613 \let\csname FV@afterbuffer\FancyVerbBufferDepth\endcsname\FV@Undefined
4614 \fi
4615 \ifcsname FV@setbufferlocalscopevars\FancyVerbBufferDepth\endcsname
4616 \begingroup
4617 \csname FV@setbufferlocalscopevars\FancyVerbBufferDepth\endcsname
4618 \loop\unless\ifnum\FV@bufferlengthmacro=\FV@oldbufferlength\relax
4619 \expandafter\global\expandafter
4620 \let\csname\FancyVerbBufferLineName\FV@bufferlengthmacro\endcsname\FV@Undefined
4621 \expandafter\xdef\FV@bufferlengthmacro{%
4622 \the\numexpr\FV@bufferlengthmacro-1\relax}%
4623 \repeat
4624 \endgroup
4625 \expandafter\global\expandafter
4626 \let\csname FV@setbufferlocalscopevars\FancyVerbBufferDepth\endcsname\FV@Undefined
4627 \fi
4628 \xdef\FancyVerbBufferDepth{\the\numexpr\FancyVerbBufferDepth-1\relax}}
4629 \def\endVerbatimBuffer{\FVE@VerbatimBuffer}
4630 % \end{macrocode}
4631 % \end{macro}
4632 %
4633 %
4634 %
4635 % \subsubsection{\cmd{\VerbatimInsertBuffer}}
4636 % \begin{macro}{\VerbatimInsertBuffer, insertenvname}
4637 % This inserts an existing buffer created with |VerbatimBuffer| as a verbatim environment. By default, the inserted environment is |Verbatim|; this can be modified with the option |insertenvname| to any |Verbatim|- or |BVerbatim|-based environment, or any environment with a compatible implementation. The |Verbatim| and |BVerbatim| internals are customized to function with a buffer in a command context.
4638 %
4639 % Notes on the implementation of |\VerbatimInsertBuffer@i|:
4640 % \begin{itemize}
4641 % \item The active |^^M| allows a verbatim environment to read optional arguments in the usual way, without requiring modifications to argument-reading macros.
4642 % \item The |\begingroup\fvset{#1}\global\let\FV@CurrentVerbatimInsertEnvName...| is used to extract any |insertenvname| setting from optional arguments. Most optional arguments apply to the verbatim environment or to the customized verbatim internals that are invoked within it. However, |insertenvname| is needed earlier to determine which verbatim environment is in use. It is not possible simply to use |\fvset{#1}| before the verbatim environment to apply all settings, because that would conflict with the precedence of option processing in |\FV@Environment|.
4643 % \end{itemize}
4644 % \begin{macrocode}
4645 \define@key{FV}{insertenvname}{%
4646 \def\FV@VerbatimInsertEnvName{#1}}
4647 \fvset{insertenvname=Verbatim}
4648 \def\FV@Environment@InsertBuffer#1#2{%
4649 \def\FV@KeyValues{#1}%
4650 \FV@GetKeyValues{\@nameuse{FVB@#2}}}
4651 \def\FV@Scan@InsertBuffer{%
4652 \FV@CatCodes
4653 \xdef\FV@EnvironName{\FV@VerbatimInsertEnvName}%
4654 \ifnum\FV@bufferlengthmacro=\z@\relax
4655 \PackageError{fvextra}%
4656 {Buffer length macro \expandafter\string\FV@bufferlengthmacro\space
4657 is invalid or zero}%
4658 {}%
4659 \let\FV@GetLine\relax
4660 \fi
4661 \FV@BeginScanning}%
4662 \def\VerbatimInsertBuffer@def@FV@Line#1{%
4663 \FVExtraRetokenizeVArg{\def\FV@Line}{}{#1}}
4664 \def\FancyVerbGetLine@VerbatimInsertBuffer{%
4665 \ifnum\FancyVerbBufferIndex>\FV@bufferlengthmacro\relax
4666 \global\let\FV@EnvironName\relax
4667 \let\next\relax
4668 \else
4669 \ifcsname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname
4670 \expandafter\let\expandafter\FV@Line@Buffer
4671 \csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname
4672 \expandafter\VerbatimInsertBuffer@def@FV@Line\expandafter{\FV@Line@Buffer}%
4673 \def\next{\FV@PreProcessLine\FV@GetLine}%
4674 \xdef\FancyVerbBufferIndex{\the\numexpr\FancyVerbBufferIndex+1\relax}%
4675 \else
4676 \def\next{%
4677 \PackageError{fvextra}%
4678 {Buffer with line macro named
4679 "\FancyVerbBufferLineName\FancyVerbBufferIndex" does not exist}%
4680 {Check bufferlinename, bufferlengthname, and globalbuffer settings}%
4681 }%
4682 \fi
4683 \fi
4684 \next}
4685 \newcommand{\VerbatimInsertBuffer}[1][]{%
4686 \begingroup
4687 \let\FV@Scan\FV@Scan@InsertBuffer
4688 \let\FV@CheckScan\relax
4689 \let\FV@Environment\FV@Environment@InsertBuffer
4690 \let\FancyVerbGetLine\FancyVerbGetLine@VerbatimInsertBuffer
4691 \gdef\FancyVerbBufferIndex{1}%
4692 \VerbatimInsertBuffer@i{#1}%
4693 \gdef\FancyVerbBufferIndex{0}%
4694 \endgroup
4695 \@doendpe}
4696 \begingroup
4697 \catcode`\^^M=\active
4698 \gdef\VerbatimInsertBuffer@i#1{%
4699 \begingroup%
4700 \fvset{#1}%
4701 \global\let\FV@CurrentVerbatimInsertEnvName\FV@VerbatimInsertEnvName%
4702 \endgroup%
4703 \csname \FV@CurrentVerbatimInsertEnvName\endcsname[#1]^^M%
4704 \csname end\FV@CurrentVerbatimInsertEnvName\endcsname%
4705 \global\let\FV@CurrentVerbatimInsertEnvName\FV@Undefined}%
4706 \endgroup
4707 % \end{macrocode}
4708 % \end{macro}
4709 %
4710 %
4711 % \subsubsection{\cmd{\VerbatimClearBuffer}}
4712 % \begin{macro}{\VerbatimClearBuffer}
4713 % Clear an existing buffer.
4714 % \begin{macrocode}
4715 \newcommand{\VerbatimClearBuffer}[1][]{%
4716 \begingroup
4717 \def\FV@KeyValues{#1}%
4718 \FV@UseKeyValues
4719 \xdef\FancyVerbBufferIndex{\FV@bufferlengthmacro}%
4720 \expandafter\xdef\FV@bufferlengthmacro{0}%
4721 \loop\unless\ifnum\FancyVerbBufferIndex<1\relax
4722 \expandafter\global\expandafter\let
4723 \csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname
4724 \FV@Undefined
4725 \xdef\FancyVerbBufferIndex{\the\numexpr\FancyVerbBufferIndex-1\relax}%
4726 \repeat
4727 \gdef\FancyVerbBufferIndex{0}%
4728 \endgroup}
4729 % \end{macrocode}
4730 % \end{macro}
4731 %
4732 %
4733 % \subsubsection{\cmd{\InsertBuffer}}
4734 % \begin{macro}{\InsertBuffer, wrapperenvname, wrapperenvopt, wrapperenvarg}
4735 % This inserts an existing buffer created with |VerbatimBuffer| so that it is interpreted as \LaTeX. The result is essentially the same as if the buffered text had been included literally at the insertion point.
4736 % \begin{macrocode}
4737 \define@key{FV}{wrapperenvname}{%
4738 \def\FV@WrapperEnvName{#1}%
4739 \ifx\FV@WrapperEnvName\@empty
4740 \let\FV@WrapperEnvName\relax
4741 \fi}
4742 \fvset{wrapperenvname=}
4743 \define@key{FV}{wrapperenvopt}{%
4744 \def\FV@WrapperEnvOpt{#1}%
4745 \ifx\FV@WrapperEnvOpt\@empty
4746 \let\FV@WrapperEnvOpt\relax
4747 \fi}
4748 \fvset{wrapperenvopt=}
4749 \define@key{FV}{wrapperenvarg}{%
4750 \def\FV@WrapperEnvArg{#1}%
4751 \ifx\FV@WrapperEnvArg\@empty
4752 \let\FV@WrapperEnvArg\relax
4753 \fi}
4754 \fvset{wrapperenvarg=}
4755 \newcommand{\InsertBuffer}[1][]{%
4756 \begingroup
4757 \def\FV@KeyValues{#1}%
4758 \FV@UseKeyValues
4759 \ifnum\FV@bufferlengthmacro<1
4760 \expandafter\endgroup\expandafter\@gobble
4761 \else
4762 \expandafter\@firstofone
4763 \fi
4764 \InsertBuffer@i}
4765 \def\InsertBuffer@i{%
4766 \InsertBuffer@expandbuffer
4767 \expandafter\endgroup\expandafter\scantokens\expandafter{%
4768 \FV@expandedbuffer\noexpand}\relax}
4769 \def\InsertBuffer@expandbuffer{%
4770 \edef\FV@expandedbuffer{%
4771 \ifx\FV@WrapperEnvName\relax
4772 \else
4773 \unexpanded\expandafter\expandafter\expandafter{%
4774 \expandafter\string\expandafter\begin\expandafter{\FV@WrapperEnvName}}%
4775 \ifx\FV@WrapperEnvOpt\relax
4776 \else
4777 \unexpanded\expandafter{\expandafter[\FV@WrapperEnvOpt]}%
4778 \fi
4779 \ifx\FV@WrapperEnvArg\relax
4780 \else
4781 \unexpanded\expandafter{\expandafter{\FV@WrapperEnvArg}}%
4782 \fi
4783 \unexpanded{^^J}%
4784 \fi
4785 \InsertBuffer@expandbufferlines{1}%
4786 \ifx\FV@WrapperEnvName\relax
4787 \unskip
4788 \else
4789 \unexpanded\expandafter\expandafter\expandafter{%
4790 \expandafter\string\expandafter\end\expandafter{\FV@WrapperEnvName}^^J}%
4791 \fi}}
4792 \def\InsertBuffer@expandbufferlines#1{%
4793 \unexpanded\expandafter\expandafter\expandafter{%
4794 \csname\FancyVerbBufferLineName#1\endcsname^^J}%
4795 \ifnum\FV@bufferlengthmacro=#1
4796 \expandafter\@gobble
4797 \else
4798 \expandafter\@firstofone
4799 \fi
4800 {\expandafter\InsertBuffer@expandbufferlines\expandafter{\the\numexpr#1+1\relax}}}
4801 % \end{macrocode}
4802 % \end{macro}
4803 %
4804 %
4805 % \subsubsection{\cmd{\ClearBuffer}}
4806 % \begin{macro}{\ClearBuffer}
4807 % Clear an existing buffer. Alias for |\VerbatimClearBuffer|.
4808 % \begin{macrocode}
4809 \let\ClearBuffer\VerbatimClearBuffer
4810 % \end{macrocode}
4811 % \end{macro}
4812 %
4813 %
4814 % \subsubsection{\cmd{\BufferMdfivesum}}
4815 % \begin{macro}{\BufferMdfivesum}
4816 % Calculate the MD5 sum of the current buffer.
4817 % \begin{macrocode}
4818 \def\BufferMdfivesum{%
4819 \csname str_mdfive_hash:e\endcsname{%
4820 \ifnum\FV@bufferlengthmacro<1
4821 \expandafter\@gobble
4822 \else
4823 \expandafter\@firstofone
4824 \fi
4825 {\BufferMdfivesum@i{1}}}}
4826 \def\BufferMdfivesum@i#1{%
4827 \csname\FancyVerbBufferLineName#1\endcsname^^J%
4828 \ifnum\FV@bufferlengthmacro=#1
4829 \expandafter\@gobble
4830 \else
4831 \expandafter\@firstofone
4832 \fi
4833 {\expandafter\BufferMdfivesum@i\expandafter{\the\numexpr#1+1\relax}}}
4834 % \end{macrocode}
4835 % \end{macro}
4836 %
4837 %
4838 % \subsubsection{\cmd{\IterateBuffer}, \cmd{\IterateBufferBreak}}
4839 % \begin{macro}{\IterateBuffer, \IterateBufferBreak}
4840 % Loop over buffer, applying a macro to each line.
4841 % \begin{macrocode}
4842 \newcommand{\IterateBuffer}[2][]{%
4843 \if\relax\detokenize{#1}\relax
4844 \else
4845 \let\FancyVerbBufferLengthName@beforeiter\FancyVerbBufferLengthName
4846 \let\FV@bufferlengthmacro@beforeiter\FV@bufferlengthmacro
4847 \let\FancyVerbBufferLineName@beforeiter\FancyVerbBufferLineName
4848 \begingroup
4849 \def\FV@KeyValues{#1}%
4850 \FV@UseKeyValues
4851 \global\let\FancyVerbBufferLengthName@iter\FancyVerbBufferLengthName
4852 \global\let\FV@bufferlengthmacro@iter\FV@bufferlengthmacro
4853 \global\let\FancyVerbBufferLineName@iter\FancyVerbBufferLineName
4854 \endgroup
4855 \let\FancyVerbBufferLengthName\FancyVerbBufferLengthName@iter
4856 \let\FV@bufferlengthmacro\FV@bufferlengthmacro@iter
4857 \let\FancyVerbBufferLineName\FancyVerbBufferLineName@iter
4858 \global\let\FancyVerbBufferLengthName@iter\FV@Undefined
4859 \global\let\FV@bufferlengthmacro@iter\FV@Undefined
4860 \global\let\FancyVerbBufferLineName@iter\FV@Undefined
4861 \fi
4862 \gdef\FancyVerbBufferIndex{1}%
4863 \def\FV@IterateBuffer@cmd{#2}%
4864 \def\IterateBufferBreak{\xdef\FancyVerbBufferIndex{\FV@bufferlengthmacro}}%
4865 \loop\unless\ifnum\FancyVerbBufferIndex>\FV@bufferlengthmacro\relax
4866 \expandafter\let\expandafter\FV@IterateBuffer@line
4867 \csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname
4868 \expandafter\FV@IterateBuffer@cmd\expandafter{\FV@IterateBuffer@line}%
4869 \xdef\FancyVerbBufferIndex{\the\numexpr\FancyVerbBufferIndex+1\relax}%
4870 \repeat
4871 \gdef\FancyVerbBufferIndex{0}%
4872 \let\FV@IterateBuffer@cmd\FV@Undefined
4873 \let\IterateBufferBreak\FV@Undefined
4874 \let\FV@IterateBuffer@line\FV@Undefined
4875 \if\relax\detokenize{#1}\relax
4876 \else
4877 \let\FancyVerbBufferLengthName\FancyVerbBufferLengthName@beforeiter
4878 \let\FV@bufferlengthmacro\FV@bufferlengthmacro@beforeiter
4879 \let\FancyVerbBufferLineName\FancyVerbBufferLineName@beforeiter
4880 \let\FancyVerbBufferLengthName@beforeiter\FV@Undefined
4881 \let\FV@bufferlengthmacro@beforeiter\FV@Undefined
4882 \let\FancyVerbBufferLineName@beforeiter\FV@Undefined
4883 \fi}
4884 % \end{macrocode}
4885 % \end{macro}
4886 %
4887 %
4888 % \begin{macro}{\WriteBuffer}
4889 % Buffer equivalent of |VerbatimWrite|.
4890 % \begin{macrocode}
4891 \def\WriteBuffer{%
4892 \FV@Command{}{WriteBuffer}}
4893 \def\FVC@WriteBuffer{%
4894 \@bsphack
4895 \begingroup
4896 \setcounter{FancyVerbWriteLine}{0}%
4897 \let\c@FancyVerbLine\c@FancyVerbWriteLine
4898 \FV@UseKeyValues
4899 \FV@DefineWhiteSpace
4900 \def\FV@Space{\space}%
4901 \FV@DefineTabOut
4902 \gdef\FancyVerbBufferIndex{1}%
4903 \loop\unless\ifnum\FancyVerbBufferIndex>\FV@bufferlengthmacro\relax
4904 \stepcounter{FancyVerbWriteLine}%
4905 \expandafter\let\expandafter\FV@WriteBuffer@line
4906 \csname\FancyVerbBufferLineName\FancyVerbBufferIndex\endcsname
4907 \expandafter\FV@Writer\expandafter{\FV@WriteBuffer@line}%
4908 \xdef\FancyVerbBufferIndex{\the\numexpr\FancyVerbBufferIndex+1\relax}%
4909 \repeat
4910 \gdef\FancyVerbBufferIndex{0}%
4911 \endgroup
4912 \@esphack}
4913 % \end{macrocode}
4914 % \end{macro}
4915 %
4916 %
4917 %
4918 %
4919 % \subsection{Patches}
4920 % \label{sec:impl:patches}
4921 %
4922 %
4923 % \subsubsection{Delimiting characters for verbatim commands}
4924 % \label{sec:impl:patches:delimiting-verbatim-commands}
4925 %
4926 % Unlike \cmd{\verb}, \fancyvrb's commands like \cmd{\Verb} cannot take arguments delimited by characters like |#| and |%| due to the way that starred commands and optional arguments are implemented. The relevant macros are redefined to make this possible.
4927 %
4928 % \fancyvrb's \cmd{\Verb} is actually implemented in \cmd{\FVC@Verb}. This is invoked by a helper macro \cmd{\FV@Command} which allows versions of commands with customized options:
4929 % \begin{quote}
4930 % \cmd{\FV@Command}\marg{customized\_options}\marg{base\_command\_name}
4931 % \end{quote}
4932 % \cmd{\Verb} is then defined as |\def\Verb{\FV@Command{}{Verb}}|. The definition of \cmd{\FV@Command} (and \cmd{\FV@@Command} which it uses internally) involves looking ahead for a star |*| (\cmd{\@ifstar}) and for a left square bracket |[| that delimits an optional argument (\cmd{\@ifnextchar}). As a result, the next character is tokenized under the current, normal catcode regime. This prevents \cmd{\Verb} from being able to use delimiting characters like |#| and |%| that work with \cmd{\verb}.
4933 %
4934 % \cmd{\FV@Command} and \cmd{\FV@@Command} are redefined so that this lookahead tokenizes under a typical verbatim catcode regime (with one exception that is explained below). This enables \cmd{\verb}-style delimiters. This does not account for any custom catcode changes introduced by \cmd{\fvset}, customized commands, or optional arguments. However, delimiting characters should never need custom catcodes, and both the \fancyvrb\ definition of \cmd{\Verb} (when not used inside another macro) as well as the \fvextra\ reimplementation (in all cases) handle the possibility of delimiters with valid but non-typical catcodes. Other, non-verbatim commands that use \cmd{\FV@Command}, such as \cmd{\UseVerb}, are not affected by the patch.
4935 %
4936 % The catcode regime for lookahead has one exception to a typical verbatim catcode regime: The curly braces |{}| retain their normal codes. This allows the \fvextra\ reimplementation of \cmd{\Verb} to use a pair of curly braces as delimiters, which can be convenient when \cmd{\Verb} is used within another command. Since the original \fancyvrb\ implementation of \cmd{\Verb} with unpatched \cmd{\FV@Command} is incompatible with curly braces being used as delimiters in any form, this does not affect any pre-existing \fancyvrb\ functionality.
4937 %
4938 % \begin{macro}{\FV@Command}
4939 % \begin{macrocode}
4940 \def\FV@Command#1#2{%
4941 \FVExtra@ifstarVArg
4942 {\def\FV@KeyValues{#1,showspaces,showtabs}\FV@@Command{#2}}%
4943 {\def\FV@KeyValues{#1}\FV@@Command{#2}}}
4944 % \end{macrocode}
4945 % \end{macro}
4946 %
4947 % \begin{macro}{\FV@@Command}
4948 % \begin{macrocode}
4949 \def\FV@@Command#1{%
4950 \FVExtra@ifnextcharVArg[%
4951 {\FV@GetKeyValues{\@nameuse{FVC@#1}}}%
4952 {\@nameuse{FVC@#1}}}
4953 % \end{macrocode}
4954 % \end{macro}
4955 %
4956 %
4957 % \subsubsection{\cmd{\CustomVerbatimCommand} compatibility with \cmd{\FVExtraRobustCommand}}
4958 %
4959 % \begin{macro}{\@CustomVerbatimCommand}
4960 % |#1| is \cmd{\newcommand} or \cmd{\renewcommand}, |#2| is the (re)new command, |#3| is the base \fancyvrb\ command, |#4| is options.
4961 % \begin{macrocode}
4962 \def\@CustomVerbatimCommand#1#2#3#4{%
4963 \begingroup\fvset{#4}\endgroup
4964 \@ifundefined{FVC@#3}%
4965 {\FV@Error{Command `\string#3' is not a FancyVerb command.}\@eha}%
4966 {\ifcsname Robust#3\endcsname
4967 \expandafter\@firstoftwo
4968 \else
4969 \expandafter\@secondoftwo
4970 \fi
4971 {\expandafter\let\expandafter\@tempa\csname #3\endcsname
4972 \def\@tempb##1##2##3{%
4973 \expandafter\def\expandafter\@tempc\expandafter{%
4974 \csname Robust\expandafter\@gobble\string#2\endcsname}%
4975 \def\@tempd####1{%
4976 #1{#2}{##1####1##3}}%
4977 \expandafter\@tempd\@tempc
4978 \expandafter\protected\expandafter\def\@tempc{\FV@Command{#4}{#3}}}%
4979 \expandafter\@tempb\@tempa}%
4980 {#1{#2}{\FV@Command{#4}{#3}}}}}
4981 % \end{macrocode}
4982 % \end{macro}
4983 %
4984 %
4985 % \subsubsection{Visible spaces}
4986 %
4987 % \begin{macro}{\FancyVerbSpace}
4988 % The default definition of visible spaces (|showspaces=true|) could allow font commands to escape under some circumstances, depending on how it is used:
4989 %\begin{verbatim}
4990 %{\catcode`\ =12 \gdef\FancyVerbSpace{\tt }}
4991 %\end{verbatim}
4992 % |\textvisiblespace| is not an alternative because it does not have the correct width. The redefinition follows \url{https://tex.stackexchange.com/a/120231/10742}.
4993 % \begin{macrocode}
4994 \def\FancyVerbSpace{%
4995 \makebox[0.5em]{%
4996 \kern.07em
4997 \vrule height.3ex
4998 \hrulefill
4999 \vrule height.3ex
5000 \kern.07em}}
5001 % \end{macrocode}
5002 % \end{macro}
5003 %
5004 %
5005 % \subsubsection{\texttt{obeytabs} with visible tabs and with tabs inside macro arguments}
5006 % \label{sec:patch:obeytabs}
5007 %
5008 % |\FV@TrueTab| governs tab appearance when |obeytabs=true| and |showtabs=true|. It is redefined so that symbols with flexible width, such as |\rightarrowfill|, will work as expected. In the original \fancyvrb\ definition, |\kern\@tempdima\hbox to\z@{...}|. The |\kern| is removed and instead the |\hbox| is given the width |\@tempdima|.
5009 %
5010 % |\FV@TrueTab| and related macros are also modified so that they function for tabs inside macro arguments when |obeytabs=true| (inside curly braces |{}| with their normal meaning, when using |commandchars|, etc.). The \fancyvrb\ implementation of tab expansion assumes that tabs are never inside a group; when a group that contains a tab is present, the entire line typically vanishes. The new implementation keeps the \fancyvrb\ behavior exactly for tabs outside groups; they are perfectly expanded to tab stops. Tabs inside groups cannot be perfectly expanded to tab stops, at least not using the \fancyvrb\ approach. Instead, when \fvextra\ encounters a run of whitespace characters (tabs and possibly spaces), it makes the assumption that the nearest tab stop was at the beginning of the run. This gives the correct behavior if the whitespace characters are leading indentation that happens to be within a macro. Otherwise, it will typically not give correct tab expansion---but at least the entire line will not be discarded, and the run of whitespace will be represented, even if imperfectly.
5011 %
5012 % A general solution to tab expansion may be possible, but will almost certainly require multiple compiles, perhaps even one compile (or more) per tab. The \pkg{zref} package provides a |\zsaveposx| macro that stores the current $x$ position on the page for subsequent compiles. This macro, or a similar macro from another package, could be used to establish a reference point at the beginning of each line. Then each run of whitespace that contains a tab could have a reference point established at its start, and tabs could be expanded based on the distance between the start of the run and the start of the line. Such an approach would allow the first run of whitespace to measure its distance from the start of the line on the 2nd compile (once both reference points were established), so it would be able expand the first run of whitespace correctly on the 3rd compile. That would allow a second run of whitespace to definitely establish its starting point on the 3rd compile, which would allow it to expand correctly on the 4th compile. And so on. Thus, while it should be possible to perform completely correct tab expansion with such an approach, it will in general require at least 4 compiles to do better than the current approach. Furthermore, the sketch of the algorithm provided so far does not include any complications introduced by line breaking. In the current approach, it is necessary to determine how each tab would be expanded in the absence of line breaking, save all tab widths, and then expand using saved widths during the actual typesetting with line breaking.
5013 %
5014 %
5015 % \begin{macro}{FV@TrueTabGroupLevel}
5016 % Counter for keeping track of the group level (|\currentgrouplevel|) at the very beginning of a line, inside |\FancyVerbFormatLine| but outside |\FancyVerbFormatText|, which is where the tab expansion macro is invoked. This allows us to determine whether we are in a group, and expand tabs accordingly.
5017 % \begin{macrocode}
5018 \newcounter{FV@TrueTabGroupLevel}
5019 % \end{macrocode}
5020 % \end{macro}
5021 %
5022 %
5023 % \begin{macro}{\FV@@ObeyTabs}
5024 % The \fancyvrb\ macro responsible for tab expansion is modified so that it can handle tabs inside groups, even if imperfectly. We need to use a special version of the space, |\FV@Space@ObeyTabs|, that within a group will capture all following spaces or tabs and then insert them with tab expansion based on the beginning of the run of whitespace. We need to record the current group level, but then increment it by $1$ because all comparisons will be performed within the |\hbox{...}|. The |\FV@TmpCurrentGroupLevel| is needed for compatibility with the \pkg{calc} package, which redefines |\setcounter|.
5025 % \begin{macrocode}
5026 \def\FV@@ObeyTabs#1{%
5027 \let\FV@Space@Orig\FV@Space
5028 \let\FV@Space\FV@Space@ObeyTabs
5029 \edef\FV@TmpCurrentGroupLevel{\the\currentgrouplevel}%
5030 \setcounter{FV@TrueTabGroupLevel}{\FV@TmpCurrentGroupLevel}%
5031 \addtocounter{FV@TrueTabGroupLevel}{1}%
5032 \setbox\FV@TabBox=\hbox{#1}\box\FV@TabBox
5033 \let\FV@Space\FV@Space@Orig}
5034 % \end{macrocode}
5035 % \end{macro}
5036 %
5037 %
5038 % \begin{macro}{\FV@TrueTab}
5039 % Version that follows \fancyvrb\ if not in a group and takes another approach otherwise.
5040 % \begin{macrocode}
5041 \def\FV@TrueTab{%
5042 \ifnum\value{FV@TrueTabGroupLevel}=\the\currentgrouplevel\relax
5043 \expandafter\FV@TrueTab@NoGroup
5044 \else
5045 \expandafter\FV@TrueTab@Group
5046 \fi}
5047 % \end{macrocode}
5048 % \end{macro}
5049 %
5050 % \begin{macro}{\FV@TrueTabSaveWidth}
5051 % When linebreaking is in use, the \fancyvrb\ tab expansion algorithm cannot be used directly, since it involves |\hbox|, which doesn't allow for line breaks. In those cases, tab widths will be calculated for the case without breaks and saved, and then saved widths will be used in the actual typesetting. This macro is |\let| to width-saving code in those cases.
5052 % \begin{macrocode}
5053 \let\FV@TrueTabSaveWidth\relax
5054 % \end{macrocode}
5055 % \end{macro}
5056 %
5057 % \begin{macro}{FV@TrueTabCounter}
5058 % Counter for tracking saved tabs.
5059 % \begin{macrocode}
5060 \newcounter{FV@TrueTabCounter}
5061 % \end{macrocode}
5062 % \end{macro}
5063 %
5064 % \begin{macro}{\FV@TrueTabSaveWidth@Save}
5065 % Save the current tab width, then increment the tab counter. |\@tempdima| will hold the current tab width.
5066 % \begin{macrocode}
5067 \def\FV@TrueTabSaveWidth@Save{%
5068 \expandafter\xdef\csname FV@TrueTab:Width\arabic{FV@TrueTabCounter}\endcsname{%
5069 \number\@tempdima}%
5070 \stepcounter{FV@TrueTabCounter}}
5071 % \end{macrocode}
5072 % \end{macro}
5073 %
5074 % \begin{macro}{\FV@TrueTab@NoGroup}
5075 % This follows the \fancyvrb\ approach exactly, except for the |\hbox to\@tempdima| adjustment and the addition of |\FV@TrueTabSaveWidth|.
5076 % \begin{macrocode}
5077 \def\FV@TrueTab@NoGroup{%
5078 \egroup
5079 \@tempdima=\FV@ObeyTabSize sp\relax
5080 \@tempcnta=\wd\FV@TabBox
5081 \advance\@tempcnta\FV@@ObeyTabSize\relax
5082 \divide\@tempcnta\@tempdima
5083 \multiply\@tempdima\@tempcnta
5084 \advance\@tempdima-\wd\FV@TabBox
5085 \FV@TrueTabSaveWidth
5086 \setbox\FV@TabBox=\hbox\bgroup
5087 \unhbox\FV@TabBox\hbox to\@tempdima{\hss\FV@TabChar}}
5088 % \end{macrocode}
5089 % \end{macro}
5090 %
5091 % \begin{macro}{FV@ObeyTabs@Whitespace@Tab}
5092 % In a group where runs of whitespace characters are collected, we need to keep track of whether a tab has been found, so we can avoid expansion and the associated |\hbox| for spaces without tabs.
5093 % \begin{macrocode}
5094 \newbool{FV@ObeyTabs@Whitespace@Tab}
5095 % \end{macrocode}
5096 % \end{macro}
5097 %
5098 % \begin{macro}{\FV@TrueTab@Group}
5099 % If in a group, a tab should start collecting whitespace characters for later tab expansion, beginning with itself. The collected whitespace will use |\FV@FVTabToken| and |\FV@FVSpaceToken| so that any |\ifx| comparisons performed later will behave as expected. This shouldn't be strictly necessary, because |\FancyVerbBreakStart| operates with saved tab widths rather than using the tab expansion code directly. But it is safer in case any other unanticipated scanning is going on.
5100 % \begin{macrocode}
5101 \def\FV@TrueTab@Group{%
5102 \booltrue{FV@ObeyTabs@Whitespace@Tab}%
5103 \gdef\FV@TmpWhitespace{\FV@FVTabToken}%
5104 \FV@ObeyTabs@ScanWhitespace}
5105 % \end{macrocode}
5106 % \end{macro}
5107 %
5108 %
5109 % \begin{macro}{\FV@Space@ObeyTabs}
5110 % Space treatment, like tab treatment, now depends on whether we are in a group, because in a group we want to collect all runs of whitespace and then expand any tabs.
5111 % \begin{macrocode}
5112 \def\FV@Space@ObeyTabs{%
5113 \ifnum\value{FV@TrueTabGroupLevel}=\the\currentgrouplevel\relax
5114 \expandafter\FV@Space@ObeyTabs@NoGroup
5115 \else
5116 \expandafter\FV@Space@ObeyTabs@Group
5117 \fi}
5118 % \end{macrocode}
5119 % \end{macro}
5120 %
5121 % \begin{macro}{\FV@Space@ObeyTabs@NoGroup}
5122 % Fall back to normal space.
5123 % \begin{macrocode}
5124 \def\FV@Space@ObeyTabs@NoGroup{\FV@Space@Orig}
5125 % \end{macrocode}
5126 % \end{macro}
5127 %
5128 % \begin{macro}{\FV@Space@ObeyTabs@Group}
5129 % Make a note that no tabs have yet been encountered, store the current space, then scan for following whitespace.
5130 % \begin{macrocode}
5131 \def\FV@Space@ObeyTabs@Group{%
5132 \boolfalse{FV@ObeyTabs@Whitespace@Tab}%
5133 \gdef\FV@TmpWhitespace{\FV@FVSpaceToken}%
5134 \FV@ObeyTabs@ScanWhitespace}
5135 % \end{macrocode}
5136 % \end{macro}
5137 %
5138 %
5139 % \begin{macro}{\FV@ObeyTabs@ScanWhitespace}
5140 % Collect whitespace until the end of the run, then process it. Proper lookahead comparison requires |\FV@FVSpaceToken| and |\FV@FVTabToken|.
5141 % \begin{macrocode}
5142 \def\FV@ObeyTabs@ScanWhitespace{%
5143 \@ifnextchar\FV@FVSpaceToken%
5144 {\FV@TrueTab@CaptureWhitespace@Space}%
5145 {\ifx\@let@token\FV@FVTabToken
5146 \expandafter\FV@TrueTab@CaptureWhitespace@Tab
5147 \else
5148 \expandafter\FV@ObeyTabs@ResolveWhitespace
5149 \fi}}
5150 \def\FV@TrueTab@CaptureWhitespace@Space#1{%
5151 \g@addto@macro\FV@TmpWhitespace{\FV@FVSpaceToken}%
5152 \FV@ObeyTabs@ScanWhitespace}
5153 \def\FV@TrueTab@CaptureWhitespace@Tab#1{%
5154 \booltrue{FV@ObeyTabs@Whitespace@Tab}%
5155 \g@addto@macro\FV@TmpWhitespace{\FV@FVTabToken}%
5156 \FV@ObeyTabs@ScanWhitespace}
5157 % \end{macrocode}
5158 % \end{macro}
5159 %
5160 %
5161 % \begin{macro}{\FV@TrueTab@Group@Expand}
5162 % Yet another tab definition, this one for use in the actual expansion of tabs in whitespace. This uses the \fancyvrb\ algorithm, but only over a restricted region known to contain no groups.
5163 % \begin{macrocode}
5164 \newbox\FV@TabBox@Group
5165 \def\FV@TrueTab@Group@Expand{%
5166 \egroup
5167 \@tempdima=\FV@ObeyTabSize sp\relax
5168 \@tempcnta=\wd\FV@TabBox@Group
5169 \advance\@tempcnta\FV@@ObeyTabSize\relax
5170 \divide\@tempcnta\@tempdima
5171 \multiply\@tempdima\@tempcnta
5172 \advance\@tempdima-\wd\FV@TabBox@Group
5173 \FV@TrueTabSaveWidth
5174 \setbox\FV@TabBox@Group=\hbox\bgroup
5175 \unhbox\FV@TabBox@Group\hbox to\@tempdima{\hss\FV@TabChar}}
5176 % \end{macrocode}
5177 % \end{macro}
5178 %
5179 % \begin{macro}{\FV@ObeyTabs@ResolveWhitespace}
5180 % Need to make sure the right definitions of the space and tab are in play here. Only do tab expansion, with the associated |\hbox|, if a tab is indeed present.
5181 % \begin{macrocode}
5182 \def\FV@ObeyTabs@ResolveWhitespace{%
5183 \let\FV@Space\FV@Space@Orig
5184 \let\FV@Tab\FV@TrueTab@Group@Expand
5185 \expandafter\FV@ObeyTabs@ResolveWhitespace@i\expandafter{\FV@TmpWhitespace}%
5186 \let\FV@Space\FV@Space@ObeyTabs
5187 \let\FV@Tab\FV@TrueTab}
5188 \def\FV@ObeyTabs@ResolveWhitespace@i#1{%
5189 \ifbool{FV@ObeyTabs@Whitespace@Tab}%
5190 {\setbox\FV@TabBox@Group=\hbox{#1}\box\FV@TabBox@Group}%
5191 {#1}}
5192 % \end{macrocode}
5193 % \end{macro}
5194 %
5195 %
5196 % \subsubsection{Fonts and symbols in math mode}
5197 %
5198 % The single quote (\texttt{\textquotesingle}) does not become |^\prime| when typeset math is included within verbatim content, due to the definition of the character in |\@noligs|. This patch adds a new definition of the character in math mode, inspired by \url{http://tex.stackexchange.com/q/223876/10742}. It also redefines other characters in |\@noligs| to behave normally within math mode and switches the default font within math mode, so that \pkg{amsmath}'s |\text| will work as expected.
5199 %
5200 % \begin{macro}{\FV@pr@m@s}
5201 % Define a version of |\pr@m@s| from |latex.ltx| that works with active \texttt{\textquotesingle}. In verbatim contexts, \texttt{\textquotesingle} is made active by |\@noligs|.
5202 % \begin{macrocode}
5203 \begingroup
5204 \catcode`\'=\active
5205 \catcode`\^=7
5206 \gdef\FV@pr@m@s{%
5207 \ifx'\@let@token
5208 \expandafter\pr@@@s
5209 \else
5210 \ifx^\@let@token
5211 \expandafter\expandafter\expandafter\pr@@@t
5212 \else
5213 \egroup
5214 \fi
5215 \fi}
5216 \endgroup
5217 % \end{macrocode}
5218 % \end{macro}
5219 %
5220 %
5221 % \begin{macro}{\FV@SetupMathFont}
5222 % Set the font back to default from the verbatim font.
5223 % \begin{macrocode}
5224 \def\FV@SetupMathFont{%
5225 \everymath\expandafter{\the\everymath\fontfamily{\familydefault}\selectfont}}
5226 \g@addto@macro\FV@FormattingPrep@PreHook{\FV@SetupMathFont}
5227 % \end{macrocode}
5228 % \end{macro}
5229 %
5230 %
5231 % \begin{macro}{\FV@SetupMathActive}
5232 % Make all characters in |\FVExtraDoSpecials| behave more normally within math. This is for |mathescape| or when |$| has its normal catcode via |codes|. This does not retokenize, so it can only provide a better approximation of regular math.
5233 %
5234 % At minimum, the characters in |\@noligs| will typically be made |\active| within verbatim contexts to produce literal versions of themselves. The relevant definition from |latex.ltx|:
5235 %\begin{verbatim}
5236 %\def\verbatim@nolig@list{\do\`\do\<\do\>\do\,\do\'\do\-}
5237 %\end{verbatim}
5238 % It is also possible that additional characters will be made |\active| to produce literal versions of themselves or for other purposes. These |\active| characters can interfere with math.
5239 %
5240 % The strategy here has three components:
5241 % \begin{itemize}
5242 % \item For a character in |\FVExtraDoSpecials| that is active before the verbatim context begins, save its definition. Then restore this within math via |\everymath|.
5243 % \item For a character in |\FVExtraDoSpecials| that is not active, but does have a |\mathcode| of |"8000|, save the active definition. Then via |\everymath| define the active version of this character to give the saved active definition within math mode and a detokenized character otherwise.
5244 % \item For a character in |\FVExtraDoSpecials| that is not active and does not have |a \mathcode| of |"8000|, use |\everymath| to define the active version of the character to give a detokenized character.
5245 % \end{itemize}
5246 %
5247 % Detecting a |\mathcode| of |"8000| must be done in an engine-dependent manner. Partial reference: \url{https://tex.stackexchange.com/a/520122/10742}.
5248 %
5249 % Also switch to |\FV@pr@m@s| so that math prime |'| behaves as expected.
5250 % \begin{macrocode}
5251 \begingroup
5252 \expandafter\ifx\csname directlua\endcsname\relax
5253 \expandafter\ifx\csname XeTeXrevision\endcsname\relax
5254 \global\let\FV@mathcodenum\mathcode
5255 \gdef\FV@mathcodenumactive{32768}%
5256 \else
5257 \global\let\FV@mathcodenum\Umathcodenum
5258 \gdef\FV@mathcodenumactive{2097151}%
5259 \fi
5260 \else
5261 \global\let\FV@mathcodenum\Umathcodenum
5262 \gdef\FV@mathcodenumactive{16777216}%
5263 \fi
5264 \endgroup
5265 \def\FV@SetupMathActive{%
5266 \def\do##1{%
5267 \ifnum\catcode`##1=\active
5268 \expandafter\expandafter\expandafter\let\expandafter\expandafter\expandafter\FV@Tmp
5269 \csname FV@Char@CatActive:\number`##1\endcsname
5270 \expandafter\let
5271 \csname FV@Char@Math:\number`##1\endcsname\FV@Tmp
5272 \else
5273 \ifnum\FV@mathcodenum`##1=\FV@mathcodenumactive\relax
5274 \expandafter\expandafter\expandafter\let\expandafter\expandafter\expandafter\FV@Tmp
5275 \csname FV@Char@CatActive:\number`##1\endcsname
5276 \expandafter\let
5277 \csname FV@Char@MathActive:\number`##1\endcsname\FV@Tmp
5278 \expandafter
5279 \def\csname FV@Char@Math:\number`##1\endcsname{%
5280 \expandafter\ifmmode\expandafter\expandafter
5281 \csname FV@Char@MathActive:\number`##1\endcsname
5282 \else
5283 \expandafter\expandafter
5284 \csname FV@Char@CatDetok:\number`##1\endcsname
5285 \fi}%
5286 \else
5287 \expandafter\let\expandafter\FV@Tmp
5288 \csname FV@Char@CatDetok:\number`##1\endcsname
5289 \expandafter\let
5290 \csname FV@Char@Math:\number`##1\endcsname\FV@Tmp
5291 \fi
5292 \fi}%
5293 \FVExtraDoSpecials
5294 \everymath\expandafter{%
5295 \the\everymath
5296 \FV@ApplyMathActive}}
5297 \def\FV@ApplyMathActive{%
5298 \let\pr@m@s\FV@pr@m@s
5299 \def\do##1{%
5300 \expandafter\let\expandafter\FV@Tmp
5301 \csname FV@Char@CatActive:\number`##1\endcsname
5302 \expandafter\expandafter\expandafter\let\expandafter\FV@Tmp
5303 \csname FV@Char@Math:\number`##1\endcsname}%
5304 \FVExtraDoSpecials}
5305 \FV@AddToHook\FV@UseKeyValues@Hook{\FV@SetupMathActive}
5306 % \end{macrocode}
5307 % \end{macro}
5308 %
5309 %
5310 % \subsubsection{Ophaned label}
5311 %
5312 % \begin{macro}{\FV@BeginListFrame@Lines}
5313 % When |frame=lines| is used with a label, the label can be orphaned. This overwrites the default definition to add |\penalty\@M|. The fix is attributed to \url{http://tex.stackexchange.com/a/168021/10742}.
5314 % \begin{macrocode}
5315 \def\FV@BeginListFrame@Lines{%
5316 \begingroup
5317 \lineskip\z@skip
5318 \FV@SingleFrameLine{\z@}%
5319 \kern-0.5\baselineskip\relax
5320 \baselineskip\z@skip
5321 \kern\FV@FrameSep\relax
5322 \penalty\@M
5323 \endgroup}
5324 % \end{macrocode}
5325 % \end{macro}
5326 %
5327 %
5328 %
5329 %
5330 % \subsubsection{\texttt{rulecolor} and \texttt{fillcolor}}
5331 %
5332 % The |rulecolor| and |fillcolor| options are redefined so that they accept color names directly, rather than requiring |\color{|\meta{color\_name}|}|. The definitions still allow the old usage.
5333 %
5334 % \begin{macro}{rulecolor}
5335 % \begin{macrocode}
5336 \define@key{FV}{rulecolor}{%
5337 \ifstrempty{#1}%
5338 {\let\FancyVerbRuleColor\relax}%
5339 {\ifstrequal{#1}{none}%
5340 {\let\FancyVerbRuleColor\relax}%
5341 {\def\@tempa{#1}%
5342 \FV@KVProcess@RuleColor#1\FV@Undefined}}}
5343 \def\FV@KVProcess@RuleColor#1#2\FV@Undefined{%
5344 \ifx#1\color
5345 \else
5346 \expandafter\def\expandafter\@tempa\expandafter{%
5347 \expandafter\color\expandafter{\@tempa}}%
5348 \fi
5349 \let\FancyVerbRuleColor\@tempa}
5350 \fvset{rulecolor=none}
5351 % \end{macrocode}
5352 % \end{macro}
5353 %
5354 % \begin{macro}{fillcolor}
5355 % \begin{macrocode}
5356 \define@key{FV}{fillcolor}{%
5357 \ifstrempty{#1}%
5358 {\let\FancyVerbFillColor\relax}%
5359 {\ifstrequal{#1}{none}%
5360 {\let\FancyVerbFillColor\relax}%
5361 {\def\@tempa{#1}%
5362 \FV@KVProcess@FillColor#1\FV@Undefined}}}
5363 \def\FV@KVProcess@FillColor#1#2\FV@Undefined{%
5364 \ifx#1\color
5365 \else
5366 \expandafter\def\expandafter\@tempa\expandafter{%
5367 \expandafter\color\expandafter{\@tempa}}%
5368 \fi
5369 \let\FancyVerbFillColor\@tempa}
5370 \fvset{fillcolor=none}
5371 % \end{macrocode}
5372 % \end{macro}
5373 %
5374 %
5375 %
5376 %
5377 % \subsection{Extensions}
5378 %
5379 %
5380 % \subsubsection{New options requiring minimal implementation}
5381 % \label{sec:impl:extensions:options}
5382 %
5383 % \begin{macro}{linenos}
5384 % \pkg{fancyvrb} allows line numbers via the options |numbers=left| and |numbers=right|. This creates a |linenos| key that is essentially an alias for |numbers=left|.
5385 % \begin{macrocode}
5386 \define@booleankey{FV}{linenos}%
5387 {\@nameuse{FV@Numbers@left}}{\@nameuse{FV@Numbers@none}}
5388 % \end{macrocode}
5389 % \end{macro}
5390 %
5391 %
5392 % \begin{macro}{tab}
5393 % Redefine |\FancyVerbTab|.
5394 % \begin{macrocode}
5395 \define@key{FV}{tab}{\def\FancyVerbTab{#1}}
5396 % \end{macrocode}
5397 % \end{macro}
5398 %
5399 %
5400 % \begin{macro}{tabcolor}
5401 % Set tab color, or allow it to adjust to surroundings (the default \fancyvrb\ behavior). This involves re-creating the |showtabs| option to add |\FV@TabColor|.
5402 % \begin{macrocode}
5403 \define@key{FV}{tabcolor}%
5404 {\ifstrempty{#1}%
5405 {\let\FV@TabColor\relax}%
5406 {\ifstrequal{#1}{none}%
5407 {\let\FV@TabColor\relax}%
5408 {\def\FV@TabColor{\textcolor{#1}}}}}
5409 \define@booleankey{FV}{showtabs}%
5410 {\def\FV@TabChar{\FV@TabColor{\FancyVerbTab}}}%
5411 {\let\FV@TabChar\relax}
5412 \fvset{tabcolor=none, showtabs=false}
5413 % \end{macrocode}
5414 % \end{macro}
5415 %
5416 %
5417 % \begin{macro}{showspaces, FV@showspaces}
5418 % Reimplement |showspaces| with a bool to work with new space options.
5419 % \begin{macrocode}
5420 \newbool{FV@showspaces}
5421 \define@booleankey{FV}{showspaces}%
5422 {\booltrue{FV@showspaces}}%
5423 {\boolfalse{FV@showspaces}}
5424 \fvset{showspaces=false}
5425 % \end{macrocode}
5426 % \end{macro}
5427 %
5428 %
5429 % \begin{macro}{space}
5430 % Redefine |\FancyVerbSpace|, which is the visible space.
5431 % \begin{macrocode}
5432 \define@key{FV}{space}{\def\FancyVerbSpace{#1}}
5433 % \end{macrocode}
5434 % \end{macro}
5435 %
5436 %
5437 % \begin{macro}{spacecolor}
5438 % Set space color, or allow it to adjust to surroundings (the default \fancyvrb\ behavior). This involves re-creating the |showspaces| option to add |\FV@SpaceColor|.
5439 % \begin{macrocode}
5440 \define@key{FV}{spacecolor}%
5441 {\ifstrempty{#1}%
5442 {\let\FV@SpaceColor\relax}%
5443 {\ifstrequal{#1}{none}%
5444 {\let\FV@SpaceColor\relax}%
5445 {\def\FV@SpaceColor{\textcolor{#1}}}}}
5446 \fvset{spacecolor=none}
5447 % \end{macrocode}
5448 % \end{macro}
5449 %
5450 %
5451 % \begin{macro}{spacebreak, \FancyVerbSpaceBreak}
5452 % Line break for spaces that is inserted when spaces are visible (|showspaces=true|) or when breaks around spaces are handled specially (|breakcollapsespaces=false|). Not used for regular spaces under default conditions.
5453 % \begin{macrocode}
5454 \define@key{FV}{spacebreak}{%
5455 \def\FancyVerbSpaceBreak{#1}}
5456 \fvset{spacebreak=\discretionary{}{}{}}
5457 % \end{macrocode}
5458 % \end{macro}
5459 %
5460 %
5461 % \begin{macro}{breakcollapsespaces, FV@breakcollapsespaces}
5462 % When a line break occurs within a sequence of regular space characters (|showspaces=false|), collapse the spaces into a single space and then replace it with the break. When this is |true|, a sequence of spaces will cause at most a single line break, and the first character on the wrapped line after the break will be a non-space character. When this is |false|, a sequence of spaces may result in multiple line breaks. Each wrapped line besides the last will contain only spaces. The final wrapped line may contain leading spaces before any non-space character(s).
5463 % \begin{macrocode}
5464 \newbool{FV@breakcollapsespaces}
5465 \define@booleankey{FV}{breakcollapsespaces}%
5466 {\booltrue{FV@breakcollapsespaces}}%
5467 {\boolfalse{FV@breakcollapsespaces}}%
5468 \fvset{breakcollapsespaces=true}
5469 % \end{macrocode}
5470 % \end{macro}
5471 %
5472 %
5473 % \begin{macro}{\FV@DefFVSpace}
5474 % Redefine |\FV@Space| based on \pkg{fvextra} options that affect spaces.
5475 %
5476 % This must be added to |\FV@FormattingPrep@PreHook|, but only after |breakbefore| and |breakafter| macros are defined. Hence the |\AtEndOfPackage|.
5477 % \begin{macrocode}
5478 \def\FV@DefFVSpace{%
5479 \ifbool{FV@showspaces}%
5480 {\ifbool{FV@breaklines}%
5481 {\ifcsname FV@BreakBefore@Token\FV@SpaceCatTen\endcsname
5482 \def\FV@Space{\FV@SpaceColor{\FancyVerbSpace}}%
5483 \else\ifcsname FV@BreakAfter@Token\FV@SpaceCatTen\endcsname
5484 \def\FV@Space{\FV@SpaceColor{\FancyVerbSpace}}%
5485 \else
5486 \def\FV@Space{\FV@SpaceColor{\FancyVerbSpace}\FancyVerbSpaceBreak}%
5487 \fi\fi}%
5488 {\def\FV@Space{\FV@SpaceColor{\FancyVerbSpace}}}}%
5489 {\ifbool{FV@breaklines}%
5490 {\ifcsname FV@BreakBefore@Token\FV@SpaceCatTen\endcsname
5491 \def\FV@Space{\mbox{\FV@SpaceCatTen}}%
5492 \else\ifcsname FV@BreakAfter@Token\FV@SpaceCatTen\endcsname
5493 \def\FV@Space{\mbox{\FV@SpaceCatTen}}%
5494 \else
5495 \ifbool{FV@breakcollapsespaces}%
5496 {\def\FV@Space{\FV@SpaceCatTen}}%
5497 {\def\FV@Space{\mbox{\FV@SpaceCatTen}\FancyVerbSpaceBreak}}%
5498 \fi\fi}%
5499 {\def\FV@Space{\FV@SpaceCatTen}}}}%
5500 \AtEndOfPackage{%
5501 \g@addto@macro\FV@FormattingPrep@PreHook{\FV@DefFVSpace}}
5502 % \end{macrocode}
5503 % \end{macro}
5504 %
5505 %
5506 % \begin{macro}{mathescape}
5507 % Give |$|, |&|, |^|, and |_| their normal catcodes to allow normal typeset math.
5508 % \begin{macrocode}
5509 \define@booleankey{FV}{mathescape}%
5510 {\let\FancyVerbMathEscape\FV@MathEscape}%
5511 {\let\FancyVerbMathEscape\relax}
5512 \def\FV@MathEscape{\catcode`\$=3\catcode`\&=4\catcode`\^=7\catcode`\_=8\relax}
5513 \FV@AddToHook\FV@CatCodesHook\FancyVerbMathEscape
5514 \fvset{mathescape=false}
5515 % \end{macrocode}
5516 % \end{macro}
5517 %
5518 %
5519 % \begin{macro}{beameroverlays}
5520 % Give |<| and |>| their normal catcodes (not |\active|), so that \pkg{beamer} overlays will work. This modifies |\@noligs| because that is the only way to prevent the settings from being overwritten later. This could have used |\FV@CatCodesHook|, but then it would have had to compare |\@noligs| to |\relax| to avoid issues when |\let\@noligs\relax| in |VerbatimOut|.
5521 % \begin{macrocode}
5522 \define@booleankey{FV}{beameroverlays}%
5523 {\let\FancyVerbBeamerOverlays\FV@BeamerOverlays}%
5524 {\let\FancyVerbBeamerOverlays\relax}
5525 \def\FV@BeamerOverlays{%
5526 \expandafter\def\expandafter\@noligs\expandafter{\@noligs
5527 \catcode`\<=12\catcode`\>=12\relax}}
5528 \FV@AddToHook\FV@FormattingPrep@PreHook\FancyVerbBeamerOverlays
5529 \fvset{beameroverlays=false}
5530 % \end{macrocode}
5531 % \end{macro}
5532 %
5533 %
5534 % \begin{macro}{curlyquotes}
5535 % Let \texttt{\textasciigrave} and \texttt{\textquotesingle} produce curly quotation marks \texttt{`} and \texttt{'} rather than the backtick and typewriter single quotation mark produced by default via \pkg{upquote}.
5536 % \begin{macrocode}
5537 \newbool{FV@CurlyQuotes}
5538 \define@booleankey{FV}{curlyquotes}%
5539 {\booltrue{FV@CurlyQuotes}}%
5540 {\boolfalse{FV@CurlyQuotes}}
5541 \def\FancyVerbCurlyQuotes{%
5542 \ifbool{FV@CurlyQuotes}%
5543 {\expandafter\def\expandafter\@noligs\expandafter{\@noligs
5544 \begingroup\lccode`\~=`\`\lowercase{\endgroup\def~}{`}%
5545 \begingroup\lccode`\~=`\'\lowercase{\endgroup\def~}{'}}}%
5546 {}}
5547 \g@addto@macro\FV@FormattingPrep@PreHook{\FancyVerbCurlyQuotes}
5548 \fvset{curlyquotes=false}
5549 % \end{macrocode}
5550 % \end{macro}
5551 %
5552 %
5553 % \begin{macro}{fontencoding}
5554 % Add option for font encoding.
5555 % \begin{macrocode}
5556 \define@key{FV}{fontencoding}%
5557 {\ifstrempty{#1}%
5558 {\let\FV@FontEncoding\relax}%
5559 {\ifstrequal{#1}{none}%
5560 {\let\FV@FontEncoding\relax}%
5561 {\def\FV@FontEncoding{\fontencoding{#1}}}}}
5562 \expandafter\def\expandafter\FV@SetupFont\expandafter{%
5563 \expandafter\FV@FontEncoding\FV@SetupFont}
5564 \fvset{fontencoding=none}
5565 % \end{macrocode}
5566 % \end{macro}
5567 %
5568 %
5569 %
5570 %
5571 % \subsubsection{Formatting with \texttt{\textbackslash FancyVerbFormatLine}, \texttt{\textbackslash FancyVerbFormatText}, and \texttt{\textbackslash FancyVerbHighlightLine}}
5572 %
5573 % \fancyvrb\ defines |\FancyVerbFormatLine|, which defines the formatting for each line. The introduction of line breaks introduces an issue for |\FancyVerbFormatLine|. Does it format the entire line, including any whitespace in the margins or behind line break symbols (that is, is it outside the |\parbox| in which the entire line is wrapped when breaking is active)? Or does it only format the text part of the line, only affecting the actual characters (inside the |\parbox|)? Since both might be desirable, |\FancyVerbFormatLine| is assigned to the entire line, and a new macro |\FancyVerbFormatText| is assigned to the text, within the |\parbox|.
5574 %
5575 % An additional complication is that the \fancyvrb\ documentation says that the default value is |\def\FancyVerbFormatLine#1{#1}|. But the actual default is |\def\FancyVerbFormatLine#1{\FV@ObeyTabs{#1}}|. That is, |\FV@ObeyTabs| needs to operate directly on the line to handle tabs. As a result, \emph{all} \fancyvrb\ commands that involve |\FancyVerbFormatLine| are patched, so that |\def\FancyVerbFormatLine#1{#1}|.
5576 %
5577 % An additional macro |\FancyVerbHighlightLine| is added between |\FancyVerbFormatLine| and |\FancyVerbFormatText|. This is used to highlight selected lines (\cref{sec:impl:extensions:highlighting}). It is inside |\FancyVerbHighlightLine| so that if |\FancyVerbHighlightLine| is used to provide a background color, |\FancyVerbHighlightLine| can override it.
5578 %
5579 % \begin{macro}{\FancyVerbFormatLine}
5580 % Format the entire line, following the definition given in the \fancyvrb\ documentation. Because this is formatting the entire line, using boxes works with line breaking.
5581 % \begin{macrocode}
5582 \def\FancyVerbFormatLine#1{#1}
5583 % \end{macrocode}
5584 % \end{macro}
5585 %
5586 %
5587 % \begin{macro}{\FancyVerbFormatText}
5588 % Format only the text part of the line. Because this is inside all of the line breaking commands, using boxes here can conflict with line breaking.
5589 % \begin{macrocode}
5590 \def\FancyVerbFormatText#1{#1}
5591 % \end{macrocode}
5592 % \end{macro}
5593 %
5594 %
5595 % \begin{macro}{\FV@ListProcessLine@NoBreak}
5596 % Redefined |\FV@ListProcessLine| in which |bgcolor| support is added, |\FancyVerbFormatText| is added, and tab handling is explicit. The |@NoBreak| suffix is added because |\FV@ListProcessLine| will be |\let| to either this macro or to |\FV@ListProcessLine@Break| depending on whether line breaking is enabled.
5597 % \begin{macrocode}
5598 \def\FV@ListProcessLine@NoBreak#1{%
5599 \hbox to \hsize{%
5600 \kern\leftmargin
5601 \hbox to \linewidth{%
5602 \FV@LeftListNumber
5603 \FV@LeftListFrame
5604 \FV@BGColor@List{%
5605 \FancyVerbFormatLine{%
5606 \FancyVerbHighlightLine{%
5607 \FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}\hss
5608 \FV@RightListFrame
5609 \FV@RightListNumber}%
5610 \hss}\FV@bgcoloroverlap}
5611 % \end{macrocode}
5612 % \end{macro}
5613 %
5614 %
5615 % \begin{macro}{\FV@BProcessLine}
5616 % Redefined |\FV@BProcessLine| in which |\FancyVerbFormatText| is added and tab handling is explicit.
5617 % \begin{macrocode}
5618 \def\FV@BProcessLine#1{%
5619 \hbox{\FancyVerbFormatLine{%
5620 \ifx\FancyVerbBackgroundColor\relax
5621 \else
5622 \expandafter\FancyVerbBackgroundColorVPhantom
5623 \fi
5624 \FancyVerbHighlightLine{%
5625 \FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}}
5626 % \end{macrocode}
5627 % \end{macro}
5628 %
5629 %
5630 %
5631 % \subsubsection{Line numbering}
5632 %
5633 % Add several new line numbering options. |numberfirstline| always numbers the first line, regardless of |stepnumber|. |stepnumberfromfirst| numbers the first line, and then every line that differs from its number by a multiple of |stepnumber|. |stepnumberoffsetvalues| determines whether line number are always an exact multiple of |stepnumber| (the new default behavior) or whether there is an offset when |firstnumber| $\ne 1$ (the old default behavior). A new option |numbers=both| is created to allow line numbers on both left and right simultaneously.
5634 %
5635 % \begin{macro}{FV@NumberFirstLine}
5636 % \begin{macrocode}
5637 \newbool{FV@NumberFirstLine}
5638 % \end{macrocode}
5639 % \end{macro}
5640 %
5641 % \begin{macro}{numberfirstline}
5642 % \begin{macrocode}
5643 \define@booleankey{FV}{numberfirstline}%
5644 {\booltrue{FV@NumberFirstLine}}%
5645 {\boolfalse{FV@NumberFirstLine}}
5646 \fvset{numberfirstline=false}
5647 % \end{macrocode}
5648 % \end{macro}
5649 %
5650 % \begin{macro}{FV@StepNumberFromFirst}
5651 % \begin{macrocode}
5652 \newbool{FV@StepNumberFromFirst}
5653 % \end{macrocode}
5654 % \end{macro}
5655 %
5656 % \begin{macro}{stepnumberfromfirst}
5657 % \begin{macrocode}
5658 \define@booleankey{FV}{stepnumberfromfirst}%
5659 {\booltrue{FV@StepNumberFromFirst}}%
5660 {\boolfalse{FV@StepNumberFromFirst}}
5661 \fvset{stepnumberfromfirst=false}
5662 % \end{macrocode}
5663 % \end{macro}
5664 %
5665 % \begin{macro}{FV@StepNumberOffsetValues}
5666 % \begin{macrocode}
5667 \newbool{FV@StepNumberOffsetValues}
5668 % \end{macrocode}
5669 % \end{macro}
5670 %
5671 % \begin{macro}{stepnumberoffsetvalues}
5672 % \begin{macrocode}
5673 \define@booleankey{FV}{stepnumberoffsetvalues}%
5674 {\booltrue{FV@StepNumberOffsetValues}}%
5675 {\boolfalse{FV@StepNumberOffsetValues}}
5676 \fvset{stepnumberoffsetvalues=false}
5677 % \end{macrocode}
5678 % \end{macro}
5679 %
5680 %
5681 % \begin{macro}{\FV@Numbers@left}
5682 % Redefine \fancyvrb\ macro to account for |numberfirstline|, |stepnumberfromfirst|, and |stepnumberoffsetvalues|. The |\let\FancyVerbStartNum\@ne| is needed to account for the case where |firstline| is never set, and defaults to zero (|\z@|).
5683 % \begin{macrocode}
5684 \def\FV@Numbers@left{%
5685 \let\FV@RightListNumber\relax
5686 \def\FV@LeftListNumber{%
5687 \ifx\FancyVerbStartNum\z@
5688 \let\FancyVerbStartNum\@ne
5689 \fi
5690 \ifbool{FV@StepNumberFromFirst}%
5691 {\@tempcnta=\FV@CodeLineNo
5692 \@tempcntb=\FancyVerbStartNum
5693 \advance\@tempcntb\FV@StepNumber
5694 \divide\@tempcntb\FV@StepNumber
5695 \multiply\@tempcntb\FV@StepNumber
5696 \advance\@tempcnta\@tempcntb
5697 \advance\@tempcnta-\FancyVerbStartNum
5698 \@tempcntb=\@tempcnta}%
5699 {\ifbool{FV@StepNumberOffsetValues}%
5700 {\@tempcnta=\FV@CodeLineNo
5701 \@tempcntb=\FV@CodeLineNo}%
5702 {\@tempcnta=\c@FancyVerbLine
5703 \@tempcntb=\c@FancyVerbLine}}%
5704 \divide\@tempcntb\FV@StepNumber
5705 \multiply\@tempcntb\FV@StepNumber
5706 \ifnum\@tempcnta=\@tempcntb
5707 \if@FV@NumberBlankLines
5708 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5709 \else
5710 \ifx\FV@Line\empty
5711 \else
5712 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5713 \fi
5714 \fi
5715 \else
5716 \ifbool{FV@NumberFirstLine}{%
5717 \ifnum\FV@CodeLineNo=\FancyVerbStartNum
5718 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5719 \fi}{}%
5720 \fi}%
5721 }
5722 % \end{macrocode}
5723 % \end{macro}
5724 %
5725 %
5726 % \begin{macro}{\FV@Numbers@right}
5727 % Redefine \fancyvrb\ macro to account for |numberfirstline|, |stepnumberfromfirst|, and |stepnumberoffsetvalues|.
5728 % \begin{macrocode}
5729 \def\FV@Numbers@right{%
5730 \let\FV@LeftListNumber\relax
5731 \def\FV@RightListNumber{%
5732 \ifx\FancyVerbStartNum\z@
5733 \let\FancyVerbStartNum\@ne
5734 \fi
5735 \ifbool{FV@StepNumberFromFirst}%
5736 {\@tempcnta=\FV@CodeLineNo
5737 \@tempcntb=\FancyVerbStartNum
5738 \advance\@tempcntb\FV@StepNumber
5739 \divide\@tempcntb\FV@StepNumber
5740 \multiply\@tempcntb\FV@StepNumber
5741 \advance\@tempcnta\@tempcntb
5742 \advance\@tempcnta-\FancyVerbStartNum
5743 \@tempcntb=\@tempcnta}%
5744 {\ifbool{FV@StepNumberOffsetValues}%
5745 {\@tempcnta=\FV@CodeLineNo
5746 \@tempcntb=\FV@CodeLineNo}%
5747 {\@tempcnta=\c@FancyVerbLine
5748 \@tempcntb=\c@FancyVerbLine}}%
5749 \divide\@tempcntb\FV@StepNumber
5750 \multiply\@tempcntb\FV@StepNumber
5751 \ifnum\@tempcnta=\@tempcntb
5752 \if@FV@NumberBlankLines
5753 \hbox to\z@{\kern\FV@NumberSep\theFancyVerbLine\hss}%
5754 \else
5755 \ifx\FV@Line\empty
5756 \else
5757 \hbox to\z@{\kern\FV@NumberSep\theFancyVerbLine\hss}%
5758 \fi
5759 \fi
5760 \else
5761 \ifbool{FV@NumberFirstLine}{%
5762 \ifnum\FV@CodeLineNo=\FancyVerbStartNum
5763 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5764 \fi}{}%
5765 \fi}%
5766 }
5767 % \end{macrocode}
5768 % \end{macro}
5769 %
5770 %
5771 % \begin{macro}{\FV@Numbers@both}
5772 % Define a new macro to allow |numbers=both|. This copies the definitions of |\FV@LeftListNumber| and |\FV@RightListNumber| from |\FV@Numbers@left| and |\FV@Numbers@right|, without the |\relax|'s.
5773 % \begin{macrocode}
5774 \def\FV@Numbers@both{%
5775 \def\FV@LeftListNumber{%
5776 \ifx\FancyVerbStartNum\z@
5777 \let\FancyVerbStartNum\@ne
5778 \fi
5779 \ifbool{FV@StepNumberFromFirst}%
5780 {\@tempcnta=\FV@CodeLineNo
5781 \@tempcntb=\FancyVerbStartNum
5782 \advance\@tempcntb\FV@StepNumber
5783 \divide\@tempcntb\FV@StepNumber
5784 \multiply\@tempcntb\FV@StepNumber
5785 \advance\@tempcnta\@tempcntb
5786 \advance\@tempcnta-\FancyVerbStartNum
5787 \@tempcntb=\@tempcnta}%
5788 {\ifbool{FV@StepNumberOffsetValues}%
5789 {\@tempcnta=\FV@CodeLineNo
5790 \@tempcntb=\FV@CodeLineNo}%
5791 {\@tempcnta=\c@FancyVerbLine
5792 \@tempcntb=\c@FancyVerbLine}}%
5793 \divide\@tempcntb\FV@StepNumber
5794 \multiply\@tempcntb\FV@StepNumber
5795 \ifnum\@tempcnta=\@tempcntb
5796 \if@FV@NumberBlankLines
5797 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5798 \else
5799 \ifx\FV@Line\empty
5800 \else
5801 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5802 \fi
5803 \fi
5804 \else
5805 \ifbool{FV@NumberFirstLine}{%
5806 \ifnum\FV@CodeLineNo=\FancyVerbStartNum
5807 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5808 \fi}{}%
5809 \fi}%
5810 \def\FV@RightListNumber{%
5811 \ifx\FancyVerbStartNum\z@
5812 \let\FancyVerbStartNum\@ne
5813 \fi
5814 \ifbool{FV@StepNumberFromFirst}%
5815 {\@tempcnta=\FV@CodeLineNo
5816 \@tempcntb=\FancyVerbStartNum
5817 \advance\@tempcntb\FV@StepNumber
5818 \divide\@tempcntb\FV@StepNumber
5819 \multiply\@tempcntb\FV@StepNumber
5820 \advance\@tempcnta\@tempcntb
5821 \advance\@tempcnta-\FancyVerbStartNum
5822 \@tempcntb=\@tempcnta}%
5823 {\ifbool{FV@StepNumberOffsetValues}%
5824 {\@tempcnta=\FV@CodeLineNo
5825 \@tempcntb=\FV@CodeLineNo}%
5826 {\@tempcnta=\c@FancyVerbLine
5827 \@tempcntb=\c@FancyVerbLine}}%
5828 \divide\@tempcntb\FV@StepNumber
5829 \multiply\@tempcntb\FV@StepNumber
5830 \ifnum\@tempcnta=\@tempcntb
5831 \if@FV@NumberBlankLines
5832 \hbox to\z@{\kern\FV@NumberSep\theFancyVerbLine\hss}%
5833 \else
5834 \ifx\FV@Line\empty
5835 \else
5836 \hbox to\z@{\kern\FV@NumberSep\theFancyVerbLine\hss}%
5837 \fi
5838 \fi
5839 \else
5840 \ifbool{FV@NumberFirstLine}{%
5841 \ifnum\FV@CodeLineNo=\FancyVerbStartNum
5842 \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
5843 \fi}{}%
5844 \fi}%
5845 }
5846 % \end{macrocode}
5847 % \end{macro}
5848 %
5849 %
5850 %
5851 % \subsubsection{Background color}
5852 % \label{sec:impl:extensions:bgcolor}
5853 %
5854 % Define an option |backgroundcolor| that provides a basic implementation of a background color behind commands and environments. \pkg{tcolorbox} or a similar package should be used for more sophisticated background colors.
5855 %
5856 % The patch to |\FV@List| prevents narrow horizontal gaps in the background color between lines of text under some circumstances. The |\ExplSyntax*| is for compatibility with patches from the \LaTeX\ Tagged PDF project.
5857 % \begin{macro}{backgroundcolor,bgcolor,\FancyVerbBackgroundColor}
5858 % \begin{macrocode}
5859 \define@key{FV}{backgroundcolor}{%
5860 \def\FancyVerbBackgroundColor{#1}%
5861 \ifx\FancyVerbBackgroundColor\FV@None
5862 \let\FancyVerbBackgroundColor\relax
5863 \else\ifx\FancyVerbBackgroundColor\@empty
5864 \let\FancyVerbBackgroundColor\relax
5865 \fi\fi}%
5866 \fvset{backgroundcolor=none}
5867 \define@key{FV}{bgcolor}{%
5868 \fvset{backgroundcolor=#1}}
5869 \ExplSyntaxOn
5870 \patchcmd{\FV@List}%
5871 {\FV@ObeyTabsInit}%
5872 {\FV@ObeyTabsInit
5873 \ifx\FancyVerbBackgroundColor\relax
5874 \else
5875 \lineskip\z@
5876 \fi}%
5877 {}%
5878 {\PackageError{fvextra}%
5879 {Failed~to~patch~\detokenize{\FV@List}for~backgroundcolor}%
5880 {Failed~to~patch~\detokenize{\FV@List}for~backgroundcolor}}
5881 \ExplSyntaxOff
5882 % \end{macrocode}
5883 % \end{macro}
5884 %
5885 %
5886 % \begin{macro}{backgroundcolorboxoverlap, bgcolorboxoverlap, \FV@backgroundcolorboxoverlap, \FV@bgcolorstrut, \FV@bgcoloroverlap}
5887 % \begin{macrocode}
5888 \define@key{FV}{backgroundcolorboxoverlap}{%
5889 \ifdim#1=0pt\relax
5890 \let\FV@backgroundcolorboxoverlap\relax
5891 \else
5892 \def\FV@backgroundcolorboxoverlap{#1}%
5893 \fi}
5894 \fvset{backgroundcolorboxoverlap=0.25pt}
5895 \define@key{FV}{bgcolorboxoverlap}{%
5896 \fvset{backgroundcolorboxoverlap=#1}}
5897 \newsavebox{\FV@bgcolorstructbox}
5898 \def\FV@bgcolorstrut{%
5899 \ifx\FancyVerbBackgroundColor\relax
5900 \FancyVerbBackgroundColorVPhantom\strut
5901 \else\ifx\FV@backgroundcolorboxoverlap\relax
5902 \FancyVerbBackgroundColorVPhantom\strut
5903 \else
5904 \savebox{\FV@bgcolorstructbox}{\hbox{\FancyVerbBackgroundColorVPhantom\strut}}%
5905 \vrule height \ht\FV@bgcolorstructbox
5906 depth \dimexpr\dp\FV@bgcolorstructbox+\FV@backgroundcolorboxoverlap\relax
5907 width 0pt\relax
5908 \fi\fi}
5909 \def\FV@bgcoloroverlap{%
5910 \ifx\FV@backgroundcolorboxoverlap\relax
5911 \else
5912 \vspace{-\FV@backgroundcolorboxoverlap}%
5913 \fi}
5914 % \end{macrocode}
5915 % \end{macro}
5916 %
5917 %
5918 % \begin{macro}{backgroundcolorvphantom,bgcolorvphantom,\FancyVerbBackgroundColorVPhantom}
5919 % \begin{macrocode}
5920 \define@key{FV}{backgroundcolorvphantom}{%
5921 \def\FancyVerbBackgroundColorVPhantom{#1}%
5922 \ifx\FancyVerbBackgroundColorVPhantom\FV@None
5923 \let\FancyVerbBackgroundColorVPhantom\relax
5924 \else\ifx\FancyVerbBackgroundColorVPhantom\@empty
5925 \let\FancyVerbBackgroundColorVPhantom\relax
5926 \fi\fi}
5927 \fvset{backgroundcolorvphantom=\vphantom{\"Apgjy}}
5928 \define@key{FV}{bgcolorvphantom}{%
5929 \fvset{backgroundcolorvphantom=#1}}
5930 % \end{macrocode}
5931 % \end{macro}
5932 %
5933 %
5934 % \begin{macro}{backgroundcolorpadding, bgcolorpadding, \FancyVerbBackgroundColorPadding}
5935 % \begin{macrocode}
5936 \let\FancyVerbBackgroundColorPadding\relax
5937 \def\FV@backgroundcolorpadding@none@framenotsingle{%
5938 \fvset{frame=none,framerule,rulecolor=none}}
5939 \def\FV@backgroundcolorpadding@dim@framenotsingle{%
5940 \fvset{frame=single,framerule=0pt,rulecolor=\FancyVerbBackgroundColor}}
5941 \define@key{FV}{backgroundcolorpadding}{%
5942 \def\FancyVerbBackgroundColorPadding{#1}%
5943 \ifx\FancyVerbBackgroundColorPadding\FV@None
5944 \let\FancyVerbBackgroundColorPadding\relax
5945 \else\ifx\FancyVerbBackgroundColorPadding\@empty
5946 \let\FancyVerbBackgroundColorPadding\relax
5947 \fi\fi
5948 \let\FV@Next\relax
5949 \ifx\FancyVerbBackgroundColorPadding\relax
5950 \ifx\FV@BeginListFrame\FV@BeginListFrame@Single
5951 \else
5952 \let\FV@Next\FV@backgroundcolorpadding@none@framenotsingle
5953 \fi
5954 \ifx\FV@LeftListFrame\FV@LeftListFrame@Single
5955 \else
5956 \let\FV@Next\FV@backgroundcolorpadding@none@framenotsingle
5957 \fi
5958 \ifx\FV@RightListFrame\FV@RightListFrame@Single
5959 \else
5960 \let\FV@Next\FV@backgroundcolorpadding@none@framenotsingle
5961 \fi
5962 \ifx\FV@EndListFrame\FV@EndListFrame@Single
5963 \else
5964 \let\FV@Next\FV@backgroundcolorpadding@none@framenotsingle
5965 \fi
5966 \FV@Next
5967 \fvset{framesep,fillcolor=none}%
5968 \else
5969 \ifx\FV@BeginListFrame\FV@BeginListFrame@Single
5970 \else
5971 \let\FV@Next\FV@backgroundcolorpadding@dim@framenotsingle
5972 \fi
5973 \ifx\FV@LeftListFrame\FV@LeftListFrame@Single
5974 \else
5975 \let\FV@Next\FV@backgroundcolorpadding@dim@framenotsingle
5976 \fi
5977 \ifx\FV@RightListFrame\FV@RightListFrame@Single
5978 \else
5979 \let\FV@Next\FV@backgroundcolorpadding@dim@framenotsingle
5980 \fi
5981 \ifx\FV@EndListFrame\FV@EndListFrame@Single
5982 \else
5983 \let\FV@Next\FV@backgroundcolorpadding@dim@framenotsingle
5984 \fi
5985 \FV@Next
5986 \fvset{framesep=#1,fillcolor=\FancyVerbBackgroundColor}%
5987 \fi}
5988 \define@key{FV}{bgcolorpadding}{\fvset{backgroundcolorpadding=#1}}
5989 % \end{macrocode}
5990 % \end{macro}
5991 %
5992 %
5993 % \begin{macro}{\FV@BGColor@List}
5994 % Background color for environments based on |Verbatim|. Puts each line in a colorbox.
5995 % \begin{macrocode}
5996 \def\FV@BGColor@List#1{%
5997 \ifx\FancyVerbBackgroundColor\relax
5998 \expandafter\@firstoftwo
5999 \else
6000 \expandafter\@secondoftwo
6001 \fi
6002 {#1}%
6003 {\setlength{\FV@TmpLength}{\fboxsep}%
6004 \setlength{\fboxsep}{0pt}%
6005 \colorbox{\FancyVerbBackgroundColor}{%
6006 \setlength{\fboxsep}{\FV@TmpLength}%
6007 \rlap{\FV@bgcolorstrut#1}%
6008 \hspace{\linewidth}%
6009 \ifx\FV@RightListFrame\relax\else
6010 \hspace{-\FV@FrameSep}%
6011 \hspace{-\FV@FrameRule}%
6012 \fi
6013 \ifx\FV@LeftListFrame\relax\else
6014 \hspace{-\FV@FrameSep}%
6015 \hspace{-\FV@FrameRule}%
6016 \fi}%
6017 \hss}}
6018 % \end{macrocode}
6019 % \end{macro}
6020 %
6021 %
6022 % \begin{macro}{\FV@BVerbatimBegin,\FV@BVerbatimEnd}
6023 % Reimplementation of |BVerbatim| macros to support |bgcolor|. Much of this follows the implementation of |SaveVerbatim| and |\BUseVerbatim|.
6024 %
6025 % Key values must be configured immediately for the |bgcolor| case, so that the background color and other settings are available. |\FV@UseKeyValues| will be invoked again within the original \fancyvrb\ implementation (now |\FV@BVerbatimBegin@NoBGColor|), but that doesn't cause any issues since |\FV@UseKeyValues| applies keys and then redefines |\FV@KeyValues| to empty. |\FV@FormattingPrep| doesn't appear explicitly, because it is in |\FV@BVerbatimBegin@NoBGColor|. The |\FV@BVerbatim@*@NoBGColor| macros are used regardless of whether there is a background color; they are wrapped in additional macros in the background color case.
6026 %
6027 % The definition of |\FV@BProcessLine| already accounts for |\FancyVerbBackgroundColorVPhantom|.
6028 % \begin{macrocode}
6029 \let\FV@BVerbatimBegin@NoBGColor\FV@BVerbatimBegin
6030 \let\FV@BVerbatimEnd@NoBGColor\FV@BVerbatimEnd
6031 \def\FV@BVerbatimBegin{%
6032 \begingroup
6033 \FV@UseKeyValues
6034 \ifx\FancyVerbBackgroundColor\relax
6035 \expandafter\FV@BVerbatimBegin@NoBGColor
6036 \else
6037 \expandafter\FV@BVerbatimBegin@BGColor
6038 \fi}
6039 \def\FV@BVerbatimEnd{%
6040 \ifx\FancyVerbBackgroundColor\relax
6041 \expandafter\FV@BVerbatimEnd@NoBGColor
6042 \else
6043 \expandafter\FV@BVerbatimEnd@BGColor
6044 \fi
6045 \endgroup}
6046 \def\FV@BVerbatimBegin@BGColor{%
6047 \gdef\FV@TheVerbatim{}%
6048 \ifx\FV@boxwidth\relax
6049 \gdef\FV@boxwidth@tmp{0pt}%
6050 \def\FV@ProcessLine##1{%
6051 \sbox{\FV@LineBox}{\FV@BProcessLine{##1}}%
6052 \ifdim\wd\FV@LineBox>\FV@boxwidth@tmp\relax
6053 \xdef\FV@boxwidth@tmp{\the\wd\FV@LineBox}%
6054 \fi
6055 \expandafter\gdef\expandafter\FV@TheVerbatim\expandafter{%
6056 \FV@TheVerbatim\FV@ProcessLine{##1}}}%
6057 \else
6058 \def\FV@ProcessLine##1{%
6059 \expandafter\gdef\expandafter\FV@TheVerbatim\expandafter{%
6060 \FV@TheVerbatim\FV@ProcessLine{##1}}}%
6061 \fi}
6062 \def\FV@BVerbatimEnd@BGColor{%
6063 \ifx\FV@boxwidth\relax
6064 \let\FV@boxwidth\FV@boxwidth@tmp
6065 \global\let\FV@boxwidth@tmp\FV@Undefined
6066 \fi
6067 \setlength{\FV@TmpLength}{\fboxsep}%
6068 \ifx\FancyVerbBackgroundColorPadding\relax
6069 \setlength{\fboxsep}{0pt}%
6070 \else
6071 \setlength{\fboxsep}{\FancyVerbBackgroundColorPadding}%
6072 \fi
6073 \colorbox{\FancyVerbBackgroundColor}{%
6074 \setlength{\fboxsep}{\FV@TmpLength}%
6075 \FV@BVerbatimBegin@NoBGColor\FV@TheVerbatim\FV@BVerbatimEnd@NoBGColor}%
6076 \gdef\FV@TheVerbatim{}}
6077 % \end{macrocode}
6078 % \end{macro}
6079 %
6080 %
6081 %
6082 % \subsubsection{Line highlighting or emphasis}
6083 % \label{sec:impl:extensions:highlighting}
6084 %
6085 % This adds an option |highlightlines| that allows specific lines, or lines within a range, to be highlighted or otherwise emphasized.
6086 %
6087 % \begin{macro}{highlightlines}\begin{macro}{\FV@HighlightLinesList}
6088 % \begin{macrocode}
6089 \define@key{FV}{highlightlines}{\def\FV@HighlightLinesList{#1}}%
6090 \fvset{highlightlines=}
6091 % \end{macrocode}
6092 % \end{macro}\end{macro}
6093 %
6094 % \begin{macro}{highlightcolor}\begin{macro}{\FV@HighlightColor}
6095 % Define color for highlighting. The default is LightCyan. A good alternative for a brighter color would be LemonChiffon.
6096 % \begin{macrocode}
6097 \define@key{FV}{highlightcolor}{\def\FancyVerbHighlightColor{#1}}%
6098 \let\FancyVerbHighlightColor\@empty
6099 \ifcsname definecolor\endcsname
6100 \ifx\definecolor\relax
6101 \else
6102 \definecolor{FancyVerbHighlightColor}{rgb}{0.878, 1, 1}
6103 \fvset{highlightcolor=FancyVerbHighlightColor}
6104 \fi\fi
6105 \AtBeginDocument{%
6106 \ifx\FancyVerbHighlightColor\@empty
6107 \ifcsname definecolor\endcsname
6108 \ifx\definecolor\relax
6109 \else
6110 \definecolor{FancyVerbHighlightColor}{rgb}{0.878, 1, 1}
6111 \fvset{highlightcolor=FancyVerbHighlightColor}
6112 \fi\fi
6113 \fi}
6114 % \end{macrocode}
6115 % \end{macro}\end{macro}
6116 %
6117 %
6118 % \begin{macro}{\FancyVerbHighlightLine}
6119 % This is the entry macro into line highlighting. By default it should do nothing. It is always invoked between |\FancyVerbFormatLine| and |\FancyVerbFormatText|, so that it can provide a background color (won't interfere with line breaking) and can override any formatting provided by |\FancyVerbFormatLine|. It is |\let| to |\FV@HighlightLine| when highlighting is active.
6120 % \begin{macrocode}
6121 \def\FancyVerbHighlightLine#1{#1}
6122 % \end{macrocode}
6123 % \end{macro}
6124 %
6125 %
6126 % \begin{macro}{\FV@HighlightLine}
6127 % This determines whether highlighting should be performed, and if so, which macro should be invoked.
6128 % \begin{macrocode}
6129 \def\FV@HighlightLine#1{%
6130 \@tempcnta=\c@FancyVerbLine
6131 \@tempcntb=\c@FancyVerbLine
6132 \ifcsname FV@HighlightLine:\number\@tempcnta\endcsname
6133 \advance\@tempcntb\m@ne
6134 \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname
6135 \advance\@tempcntb\tw@
6136 \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname
6137 \let\FV@HighlightLine@Next\FancyVerbHighlightLineMiddle
6138 \else
6139 \let\FV@HighlightLine@Next\FancyVerbHighlightLineLast
6140 \fi
6141 \else
6142 \advance\@tempcntb\tw@
6143 \ifcsname FV@HighlightLine:\number\@tempcntb\endcsname
6144 \let\FV@HighlightLine@Next\FancyVerbHighlightLineFirst
6145 \else
6146 \let\FV@HighlightLine@Next\FancyVerbHighlightLineSingle
6147 \fi
6148 \fi
6149 \else
6150 \let\FV@HighlightLine@Next\FancyVerbHighlightLineNormal
6151 \fi
6152 \FV@HighlightLine@Next{#1}%
6153 }
6154 % \end{macrocode}
6155 % \end{macro}
6156 %
6157 % \begin{macro}{\FancyVerbHighlightLineNormal}
6158 % A normal line that is not highlighted or otherwise emphasized. This could be redefined to de-emphasize the line.
6159 % \begin{macrocode}
6160 \def\FancyVerbHighlightLineNormal#1{#1}
6161 % \end{macrocode}
6162 % \end{macro}
6163 %
6164 % \begin{macro}{\FV@TmpLength}
6165 % \begin{macrocode}
6166 \newlength{\FV@TmpLength}
6167 % \end{macrocode}
6168 % \end{macro}
6169 %
6170 % \begin{macro}{\FancyVerbHighlightLineFirst}
6171 % The first line in a multi-line range.
6172 %
6173 % |\fboxsep| is set to zero so as to avoid indenting the line or changing inter-line spacing. It is restored to its original value inside to prevent any undesired effects. The |\strut| is needed to get the highlighting to be the appropriate height. The |\rlap| and |\hspace| make the |\colorbox| expand to the full |\linewidth|. Note that if |\fboxsep| $\ne0$, then we would want to use |\dimexpr\linewidth-2\fboxsep| or add |\hspace{-2\fboxsep}| at the end.
6174 %
6175 % If this macro is customized so that the text cannot take up the full |\linewidth|, then adjustments may need to be made here or in the line breaking code to make sure that line breaking takes place at the appropriate location.
6176 % \begin{macrocode}
6177 \def\FancyVerbHighlightLineFirst#1{%
6178 \setlength{\FV@TmpLength}{\fboxsep}%
6179 \setlength{\fboxsep}{0pt}%
6180 \colorbox{\FancyVerbHighlightColor}{%
6181 \setlength{\fboxsep}{\FV@TmpLength}%
6182 \rlap{\strut#1}%
6183 \hspace{\linewidth}%
6184 \ifx\FV@RightListFrame\relax\else
6185 \hspace{-\FV@FrameSep}%
6186 \hspace{-\FV@FrameRule}%
6187 \fi
6188 \ifx\FV@LeftListFrame\relax\else
6189 \hspace{-\FV@FrameSep}%
6190 \hspace{-\FV@FrameRule}%
6191 \fi
6192 }%
6193 \hss
6194 }
6195 % \end{macrocode}
6196 % \end{macro}
6197 %
6198 % \begin{macro}{\FancyVerbHighlightLineMiddle}
6199 % A middle line in a multi-line range.
6200 % \begin{macrocode}
6201 \let\FancyVerbHighlightLineMiddle\FancyVerbHighlightLineFirst
6202 % \end{macrocode}
6203 % \end{macro}
6204 %
6205 % \begin{macro}{\FancyVerbHighlightLineLast}
6206 % The last line in a multi-line range.
6207 % \begin{macrocode}
6208 \let\FancyVerbHighlightLineLast\FancyVerbHighlightLineFirst
6209 % \end{macrocode}
6210 % \end{macro}
6211 %
6212 % \begin{macro}{\FancyVerbHighlightLineSingle}
6213 % A single line not in a multi-line range.
6214 % \begin{macrocode}
6215 \let\FancyVerbHighlightLineSingle\FancyVerbHighlightLineFirst
6216 % \end{macrocode}
6217 % \end{macro}
6218 %
6219 %
6220 % \begin{macro}{\FV@HighlightLinesPrep}
6221 % Process the list of lines to highlight (if any). A macro is created for each line to be highlighted. During highlighting, a line is highlighted if the corresponding macro exists. All of the macro creating is ultimately within the current environment group so it stays local. |\FancyVerbHighlightLine| is |\let| to a version that will invoke the necessary logic.
6222 % \begin{macrocode}
6223 \def\FV@HighlightLinesPrep{%
6224 \ifx\FV@HighlightLinesList\@empty
6225 \else
6226 \let\FancyVerbHighlightLine\FV@HighlightLine
6227 \expandafter\FV@HighlightLinesPrep@i
6228 \fi}
6229 \def\FV@HighlightLinesPrep@i{%
6230 \renewcommand{\do}[1]{%
6231 \ifstrempty{##1}{}{\FV@HighlightLinesParse##1-\FV@Undefined}}%
6232 \expandafter\docsvlist\expandafter{\FV@HighlightLinesList}}
6233 \def\FV@HighlightLinesParse#1-#2\FV@Undefined{%
6234 \ifstrempty{#2}%
6235 {\FV@HighlightLinesParse@Single{#1}}%
6236 {\FV@HighlightLinesParse@Range{#1}#2\relax}}
6237 \def\FV@HighlightLinesParse@Single#1{%
6238 \expandafter\let\csname FV@HighlightLine:\detokenize{#1}\endcsname\relax}
6239 \newcounter{FV@HighlightLinesStart}
6240 \newcounter{FV@HighlightLinesStop}
6241 \def\FV@HighlightLinesParse@Range#1#2-{%
6242 \setcounter{FV@HighlightLinesStart}{#1}%
6243 \setcounter{FV@HighlightLinesStop}{#2}%
6244 \stepcounter{FV@HighlightLinesStop}%
6245 \FV@HighlightLinesParse@Range@Loop}
6246 \def\FV@HighlightLinesParse@Range@Loop{%
6247 \ifnum\value{FV@HighlightLinesStart}<\value{FV@HighlightLinesStop}\relax
6248 \expandafter\let\csname FV@HighlightLine:\arabic{FV@HighlightLinesStart}\endcsname\relax
6249 \stepcounter{FV@HighlightLinesStart}%
6250 \expandafter\FV@HighlightLinesParse@Range@Loop
6251 \fi}
6252 \g@addto@macro\FV@FormattingPrep@PreHook{\FV@HighlightLinesPrep}
6253 % \end{macrocode}
6254 % \end{macro}
6255 %
6256 %
6257 % \begin{macro}{\FV@StepLineNo@Patch@HighlightLine}
6258 % Patch |\FV@StepLineNo| so that when |numberblanklines=false|, blank lines won't be highlighted. If the previous line is at the end of a highlighted range, then |\let| |\FV@HighlightLine:<n>| to |\FV@Undefined| to prevent further highlighting. Otherwise, leave everything as-is since the blank line(s) are within a highlighted range.
6259 % \begin{macrocode}
6260 \def\FV@StepLineNo@Patch@HighlightLine{%
6261 \ifcsname FV@HighlightLine:\number\c@FancyVerbLine\endcsname
6262 \@tempcnta=\c@FancyVerbLine
6263 \advance\@tempcnta\@ne
6264 \ifcsname FV@HighlightLine:\number\@tempcnta\endcsname
6265 \else
6266 \expandafter\let
6267 \csname FV@HighlightLine:\number\c@FancyVerbLine\endcsname\FV@Undefined
6268 \fi
6269 \fi}
6270 \patchcmd{\FV@StepLineNo}%
6271 {\ifx\FV@Line\empty}%
6272 {\ifx\FV@Line\empty\FV@StepLineNo@Patch@HighlightLine}%
6273 {}%
6274 {\PackageError{fvextra}%
6275 {Failed to patch \string\FV@StepLineNo\ to make highlightlines
6276 compatible with numberblanklines}%
6277 {Failed to patch \string\FV@StepLineNo\ to make highlightlines
6278 compatible with numberblanklines}}
6279 % \end{macrocode}
6280 % \end{macro}
6281 %
6282 %
6283 %
6284 %
6285 % \subsection{Line breaking}
6286 %
6287 % The following code adds automatic line breaking functionality to \pkg{fancyvrb}'s |Verbatim| environment. Automatic breaks may be inserted after spaces, or before or after specified characters. Breaking before or after specified characters involves scanning each line token by token to insert |\discretionary| at all potential break locations.
6288 %
6289 %
6290 % \subsubsection{Options and associated macros}
6291 % Begin by defining keys, with associated macros, bools, and dimens.
6292 %
6293 % \begin{macro}{\FV@SetToWidthNChars}
6294 % Set a dimen to the width of a given number of characters. This is used in setting several indentation-related dimensions.
6295 % \begin{macrocode}
6296 \newcount\FV@LoopCount
6297 \newbox\FV@NCharsBox
6298 \def\FV@SetToWidthNChars#1#2{%
6299 \FV@LoopCount=#2\relax
6300 \ifnum\FV@LoopCount>0
6301 \def\FV@NChars{}%
6302 \loop
6303 \ifnum\FV@LoopCount>0
6304 \expandafter\def\expandafter\FV@NChars\expandafter{\FV@NChars x}%
6305 \fi
6306 \advance\FV@LoopCount by -1
6307 \ifnum\FV@LoopCount>0
6308 \repeat
6309 \setbox\FV@NCharsBox\hbox{\FV@NChars}%
6310 #1=\wd\FV@NCharsBox
6311 \else
6312 #1=0pt\relax
6313 \fi
6314 }
6315 % \end{macrocode}
6316 % \end{macro}
6317 %
6318 % \begin{macro}{FV@breaklines}
6319 % Turn line breaking on or off. The |\FV@ListProcessLine| from \fancyvrb\ is |\let| to a (patched) version of the original or a version that supports line breaks.
6320 % \begin{macrocode}
6321 \newbool{FV@breaklines}
6322 \define@booleankey{FV}{breaklines}%
6323 {\booltrue{FV@breaklines}%
6324 \let\FV@ListProcessLine\FV@ListProcessLine@Break}%
6325 {\boolfalse{FV@breaklines}%
6326 \let\FV@ListProcessLine\FV@ListProcessLine@NoBreak}
6327 \AtEndOfPackage{\fvset{breaklines=false}}
6328 % \end{macrocode}
6329 % \end{macro}
6330 %
6331 % \begin{macro}{\FV@BreakLinesLuaTeXHook}
6332 % Fix hyphen handling under LuaTeX. |\automatichyphenmode=2| would work for environments, but doesn't seem to work inline. Instead, the active hyphen is redefined to |\mbox{-}|.
6333 %
6334 % This is needed before |\@noligs| is ever used, so it is placed in |\FV@FormattingPrep@PreHook|.
6335 % \begin{macrocode}
6336 \def\FV@BreakLinesLuaTeXHook{%
6337 \expandafter\def\expandafter\@noligs\expandafter{\@noligs
6338 \begingroup\lccode`\~=`\-\lowercase{\endgroup\def~}{\leavevmode\kern\z@\mbox{-}}}}
6339 \ifcsname directlua\endcsname
6340 \ifx\directlua\relax
6341 \else
6342 \FV@AddToHook\FV@FormattingPrep@PreHook\FV@BreakLinesLuaTeXHook
6343 \fi
6344 \fi
6345 % \end{macrocode}
6346 % \end{macro}
6347 %
6348 %
6349 % \begin{macro}{\FV@BreakLinesIndentationHook}
6350 % A hook for performing on-the-fly indentation calculations when |breaklines=true|. This is used for all |*NChars| related indentation. It is important to use |\FV@FormattingPrep@PostHook| because it is always invoked \emph{after} any font-related settings.
6351 % \begin{macrocode}
6352 \def\FV@BreakLinesIndentationHook{}
6353 \g@addto@macro\FV@FormattingPrep@PostHook{%
6354 \ifFV@breaklines
6355 \FV@BreakLinesIndentationHook
6356 \fi}
6357 % \end{macrocode}
6358 % \end{macro}
6359 %
6360 %
6361 % \begin{macro}{\FV@BreakIndent}
6362 % \begin{macro}{\FV@BreakIndentNChars}
6363 % Indentation of continuation lines.
6364 % \begin{macrocode}
6365 \newdimen\FV@BreakIndent
6366 \newcount\FV@BreakIndentNChars
6367 \define@key{FV}{breakindent}{%
6368 \FV@BreakIndent=#1\relax
6369 \FV@BreakIndentNChars=0\relax}
6370 \define@key{FV}{breakindentnchars}{\FV@BreakIndentNChars=#1\relax}
6371 \g@addto@macro\FV@BreakLinesIndentationHook{%
6372 \ifnum\FV@BreakIndentNChars>0
6373 \FV@SetToWidthNChars{\FV@BreakIndent}{\FV@BreakIndentNChars}%
6374 \fi}
6375 \fvset{breakindentnchars=0}
6376 % \end{macrocode}
6377 % \end{macro}
6378 % \end{macro}
6379 %
6380 %
6381 % \begin{macro}{FV@breakautoindent}
6382 % Auto indentation of continuation lines to indentation of original line. Adds to |\FV@BreakIndent|.
6383 % \begin{macrocode}
6384 \newbool{FV@breakautoindent}
6385 \define@booleankey{FV}{breakautoindent}%
6386 {\booltrue{FV@breakautoindent}}{\boolfalse{FV@breakautoindent}}
6387 \fvset{breakautoindent=true}
6388 % \end{macrocode}
6389 % \end{macro}
6390 %
6391 % \begin{macro}{\FancyVerbBreakSymbolLeft}
6392 % The left-hand symbol indicating a break. Since breaking is done in such a way that a left-hand symbol will often be desired while a right-hand symbol may not be, a shorthand option |breaksymbol| is supplied. This shorthand convention is continued with other options applying to the left-hand symbol.
6393 % \begin{macrocode}
6394 \define@key{FV}{breaksymbolleft}{\def\FancyVerbBreakSymbolLeft{#1}}
6395 \define@key{FV}{breaksymbol}{\fvset{breaksymbolleft=#1}}
6396 \fvset{breaksymbolleft=\tiny\ensuremath{\hookrightarrow}}
6397 % \end{macrocode}
6398 % \end{macro}
6399 %
6400 % \begin{macro}{\FancyVerbBreakSymbolRight}
6401 % The right-hand symbol indicating a break.
6402 % \begin{macrocode}
6403 \define@key{FV}{breaksymbolright}{\def\FancyVerbBreakSymbolRight{#1}}
6404 \fvset{breaksymbolright={}}
6405 % \end{macrocode}
6406 % \end{macro}
6407 %
6408 %
6409 % \begin{macro}{\FV@BreakSymbolSepLeft}
6410 % \begin{macro}{\FV@BreakSymbolSepLeftNChars}
6411 % Separation of left break symbol from the text.
6412 % \begin{macrocode}
6413 \newdimen\FV@BreakSymbolSepLeft
6414 \newcount\FV@BreakSymbolSepLeftNChars
6415 \define@key{FV}{breaksymbolsepleft}{%
6416 \FV@BreakSymbolSepLeft=#1\relax
6417 \FV@BreakSymbolSepLeftNChars=0\relax}
6418 \define@key{FV}{breaksymbolsep}{\fvset{breaksymbolsepleft=#1}}
6419 \define@key{FV}{breaksymbolsepleftnchars}{\FV@BreakSymbolSepLeftNChars=#1\relax}
6420 \define@key{FV}{breaksymbolsepnchars}{\fvset{breaksymbolsepleftnchars=#1}}
6421 \g@addto@macro\FV@BreakLinesIndentationHook{%
6422 \ifnum\FV@BreakSymbolSepLeftNChars>0
6423 \FV@SetToWidthNChars{\FV@BreakSymbolSepLeft}{\FV@BreakSymbolSepLeftNChars}%
6424 \fi}
6425 \fvset{breaksymbolsepleftnchars=2}
6426 % \end{macrocode}
6427 % \end{macro}
6428 % \end{macro}
6429 %
6430 %
6431 % \begin{macro}{\FV@BreakSymbolSepRight}
6432 % \begin{macro}{\FV@BreakSymbolSepRightNChars}
6433 % Separation of right break symbol from the text.
6434 % \begin{macrocode}
6435 \newdimen\FV@BreakSymbolSepRight
6436 \newcount\FV@BreakSymbolSepRightNChars
6437 \define@key{FV}{breaksymbolsepright}{%
6438 \FV@BreakSymbolSepRight=#1\relax
6439 \FV@BreakSymbolSepRightNChars=0\relax}
6440 \define@key{FV}{breaksymbolseprightnchars}{\FV@BreakSymbolSepRightNChars=#1\relax}
6441 \g@addto@macro\FV@BreakLinesIndentationHook{%
6442 \ifnum\FV@BreakSymbolSepRightNChars>0
6443 \FV@SetToWidthNChars{\FV@BreakSymbolSepRight}{\FV@BreakSymbolSepRightNChars}%
6444 \fi}
6445 \fvset{breaksymbolseprightnchars=2}
6446 % \end{macrocode}
6447 % \end{macro}
6448 % \end{macro}
6449 %
6450 %
6451 % \begin{macro}{\FV@BreakSymbolIndentLeft}
6452 % \begin{macro}{\FV@BreakSymbolIndentLeftNChars}
6453 % Additional left indentation to make room for the left break symbol.
6454 % \begin{macrocode}
6455 \newdimen\FV@BreakSymbolIndentLeft
6456 \newcount\FV@BreakSymbolIndentLeftNChars
6457 \define@key{FV}{breaksymbolindentleft}{%
6458 \FV@BreakSymbolIndentLeft=#1\relax
6459 \FV@BreakSymbolIndentLeftNChars=0\relax}
6460 \define@key{FV}{breaksymbolindent}{\fvset{breaksymbolindentleft=#1}}
6461 \define@key{FV}{breaksymbolindentleftnchars}{\FV@BreakSymbolIndentLeftNChars=#1\relax}
6462 \define@key{FV}{breaksymbolindentnchars}{\fvset{breaksymbolindentleftnchars=#1}}
6463 \g@addto@macro\FV@BreakLinesIndentationHook{%
6464 \ifnum\FV@BreakSymbolIndentLeftNChars>0
6465 \FV@SetToWidthNChars{\FV@BreakSymbolIndentLeft}{\FV@BreakSymbolIndentLeftNChars}%
6466 \fi}
6467 \fvset{breaksymbolindentleftnchars=4}
6468 % \end{macrocode}
6469 % \end{macro}
6470 % \end{macro}
6471 %
6472 %
6473 % \begin{macro}{\FV@BreakSymbolIndentRight}
6474 % \begin{macro}{\FV@BreakSymbolIndentRightNChars}
6475 % Additional right indentation to make room for the right break symbol.
6476 % \begin{macrocode}
6477 \newdimen\FV@BreakSymbolIndentRight
6478 \newcount\FV@BreakSymbolIndentRightNChars
6479 \define@key{FV}{breaksymbolindentright}{%
6480 \FV@BreakSymbolIndentRight=#1\relax
6481 \FV@BreakSymbolIndentRightNChars=0\relax}
6482 \define@key{FV}{breaksymbolindentrightnchars}{\FV@BreakSymbolIndentRightNChars=#1\relax}
6483 \g@addto@macro\FV@BreakLinesIndentationHook{%
6484 \ifnum\FV@BreakSymbolIndentRightNChars>0
6485 \FV@SetToWidthNChars{\FV@BreakSymbolIndentRight}{\FV@BreakSymbolIndentRightNChars}%
6486 \fi}
6487 \fvset{breaksymbolindentrightnchars=4}
6488 % \end{macrocode}
6489 % \end{macro}
6490 % \end{macro}
6491 %
6492 %
6493 % We need macros that contain the logic for typesetting the break symbols. By default, the symbol macros contain everything regarding the symbol and its typesetting, while these macros contain pure logic. The symbols should be wrapped in braces so that formatting commands (for example, |\tiny|) don't escape.
6494 %
6495 % \begin{macro}{\FancyVerbBreakSymbolLeftLogic}
6496 % The left break symbol should only appear with continuation lines. Note that |linenumber| here refers to local line numbering for the broken line, \emph{not} line numbering for all lines in the environment being typeset.
6497 % \begin{macrocode}
6498 \newcommand{\FancyVerbBreakSymbolLeftLogic}[1]{%
6499 \ifnum\value{linenumber}=1\relax\else{#1}\fi}
6500 % \end{macrocode}
6501 % \end{macro}
6502 %
6503 % \begin{macro}{FancyVerbLineBreakLast}
6504 % We need a counter for keeping track of the local line number for the last segment of a broken line, so that we can avoid putting a right continuation symbol there. A line that is broken will ultimately be processed twice when there is a right continuation symbol, once to determine the local line numbering, and then again for actual insertion into the document.
6505 % \begin{macrocode}
6506 \newcounter{FancyVerbLineBreakLast}
6507 % \end{macrocode}
6508 % \end{macro}
6509 %
6510 % \begin{macro}{\FV@SetLineBreakLast}
6511 % Store the local line number for the last continuation line.
6512 % \begin{macrocode}
6513 \newcommand{\FV@SetLineBreakLast}{%
6514 \setcounter{FancyVerbLineBreakLast}{\value{linenumber}}}
6515 % \end{macrocode}
6516 % \end{macro}
6517 %
6518 % \begin{macro}{\FancyVerbBreakSymbolRightLogic}
6519 % Only insert a right break symbol if not on the last continuation line.
6520 % \begin{macrocode}
6521 \newcommand{\FancyVerbBreakSymbolRightLogic}[1]{%
6522 \ifnum\value{linenumber}=\value{FancyVerbLineBreakLast}\relax\else{#1}\fi}
6523 % \end{macrocode}
6524 % \end{macro}
6525 %
6526 %
6527 % \begin{macro}{\FancyVerbBreakStart}
6528 % Macro that starts fine-tuned breaking (|breakanywhere|, |breakbefore|, |breakafter|) by examining a line token-by-token. Initially |\let| to |\relax|; later |\let| to |\FV@Break| as appropriate.
6529 % \begin{macrocode}
6530 \let\FancyVerbBreakStart\relax
6531 % \end{macrocode}
6532 % \end{macro}
6533 %
6534 % \begin{macro}{\FancyVerbBreakStop}
6535 % Macro that stops the fine-tuned breaking region started by |\FancyVerbBreakStart|. Initially |\let| to |\relax|; later |\let| to |\FV@EndBreak| as appropriate.
6536 % \begin{macrocode}
6537 \let\FancyVerbBreakStop\relax
6538 % \end{macrocode}
6539 % \end{macro}
6540 %
6541 % \begin{macro}{\FV@Break@DefaultToken}
6542 % Macro that controls default token handling between |\FancyVerbBreakStart| and |\FancyVerbBreakStop|. Initially |\let| to |\FV@Break@NBToken|, which does not insert breaks. Later |\let| to |\FV@Break@AnyToken| or |\FV@Break@BeforeAfterToken| if |breakanywhere| or |breakbefore|/|breakafter| are in use.
6543 % \begin{macrocode}
6544 \let\FV@Break@DefaultToken\FV@Break@NBToken
6545 % \end{macrocode}
6546 % \end{macro}
6547 %
6548 %
6549 % \begin{macro}{FV@breakanywhere}
6550 % Allow line breaking (almost) anywhere. Set |\FV@Break| and |\FV@EndBreak| to be used, and |\let| |\FV@Break@DefaultToken| to the appropriate macro.
6551 % \begin{macrocode}
6552 \newbool{FV@breakanywhere}
6553 \define@booleankey{FV}{breakanywhere}%
6554 {\booltrue{FV@breakanywhere}%
6555 \let\FancyVerbBreakStart\FV@Break
6556 \let\FancyVerbBreakStop\FV@EndBreak
6557 \let\FV@Break@DefaultToken\FV@Break@AnyToken}%
6558 {\boolfalse{FV@breakanywhere}%
6559 \let\FancyVerbBreakStart\relax
6560 \let\FancyVerbBreakStop\relax
6561 \let\FV@Break@DefaultToken\FV@Break@NBToken}
6562 \fvset{breakanywhere=false}
6563 % \end{macrocode}
6564 % \end{macro}
6565 %
6566 % \begin{macro}{breakanywhereinlinestretch,\FV@breakanywhereinlinestretch,\FV@ApplyBreakAnywhereInlineStretch}
6567 % Stretch glue to insert at potential |breakanywhere| break locations in inline contexts, to give better line widths and avoid overfull |\hbox|.
6568 %
6569 % |\FV@UseInlineKeyValues| invokes |\FV@ApplyBreakAnywhereInlineStretch| to redefine |\FancyVerbBreakAnywhereBreak| locally.
6570 % \begin{macrocode}
6571 \define@key{FV}{breakanywhereinlinestretch}{%
6572 \def\FV@breakanywhereinlinestretch{#1}%
6573 \ifx\FV@breakanywhereinlinestretch\FV@None
6574 \let\FV@breakanywhereinlinestretch\relax
6575 \else\ifx\FV@breakanywhereinlinestretch\@empty
6576 \let\FV@breakanywhereinlinestretch\relax
6577 \fi\fi}
6578 \fvset{breakanywhereinlinestretch=none}
6579 \def\FV@ApplyBreakAnywhereInlineStretch{%
6580 \ifx\FV@breakanywhereinlinestretch\relax
6581 \else
6582 \let\FancyVerbBreakAnywhereBreak@Orig\FancyVerbBreakAnywhereBreak
6583 \def\FancyVerbBreakAnywhereBreak{%
6584 \nobreak\hspace{0pt plus \FV@breakanywhereinlinestretch}%
6585 \FancyVerbBreakAnywhereBreak@Orig}%
6586 \fi}
6587 % \end{macrocode}
6588 % \end{macro}
6589 %
6590 %
6591 % \begin{macro}{\FV@BreakBefore}
6592 % Allow line breaking (almost) anywhere, but only before specified characters.
6593 % \begin{macrocode}
6594 \define@key{FV}{breakbefore}{%
6595 \ifstrempty{#1}%
6596 {\let\FV@BreakBefore\@empty
6597 \let\FancyVerbBreakStart\relax
6598 \let\FancyVerbBreakStop\relax
6599 \let\FV@Break@DefaultToken\FV@Break@NBToken}%
6600 {\def\FV@BreakBefore{#1}%
6601 \let\FancyVerbBreakStart\FV@Break
6602 \let\FancyVerbBreakStop\FV@EndBreak
6603 \let\FV@Break@DefaultToken\FV@Break@BeforeAfterToken}%
6604 }
6605 \fvset{breakbefore={}}
6606 % \end{macrocode}
6607 % \end{macro}
6608 %
6609 % \begin{macro}{FV@breakbeforeinrun}
6610 % Determine whether breaking before specified characters is always allowed before each individual character, or is only allowed before the first in a run of identical characters.
6611 % \begin{macrocode}
6612 \newbool{FV@breakbeforeinrun}
6613 \define@booleankey{FV}{breakbeforeinrun}%
6614 {\booltrue{FV@breakbeforeinrun}}%
6615 {\boolfalse{FV@breakbeforeinrun}}%
6616 \fvset{breakbeforeinrun=false}
6617 % \end{macrocode}
6618 % \end{macro}
6619 %
6620 % \begin{macro}{\FV@BreakBeforePrep}
6621 % We need a way to break before characters if and only if they have been specified as breaking characters. It would be possible to do that via a nested conditional, but that would be messy. It is much simpler to create an empty macro whose name contains the character, and test for the existence of this macro. This needs to be done inside a |\begingroup...\endgroup| so that the macros do not have to be cleaned up manually. A good place to do this is in |\FV@FormattingPrep|, which is inside a group and before processing starts. The macro is added to |\FV@FormattingPrep@PreHook|, which contains \fvextra\ extensions to |\FV@FormattingPrep|, after |\FV@BreakAfterPrep| is defined below.
6622 %
6623 % The procedure here is a bit roundabout. We need to use |\FV@EscChars| to handle character escapes, but the character redefinitions need to be kept local, requiring that we work within a |\begingroup...\endgroup|. So we loop through the breaking tokens and assemble a macro that will itself define character macros. Only this defining macro is declared global, and it contains \emph{expanded} characters so that there is no longer any dependence on |\FV@EscChars|.
6624 %
6625 % |\FV@BreakBeforePrep@PygmentsHook| allows additional break preparation for Pygments-based packages such as \pkg{minted} and \pkg{pythontex}. When Pygments highlights code, it converts some characters into macros; they do not appear literally. As a result, for breaking to occur correctly, breaking macros need to be created for these character macros and not only for the literal characters themselves.
6626 %
6627 % A pdfTeX-compatible version for working with UTF-8 is defined later, and |\FV@BreakBeforePrep| is |\let| to it under pdfTeX as necessary.
6628 % \begin{macrocode}
6629 \def\FV@BreakBeforePrep{%
6630 \ifx\FV@BreakBefore\@empty\relax
6631 \else
6632 \gdef\FV@BreakBefore@Def{}%
6633 \begingroup
6634 \def\FV@BreakBefore@Process##1##2\FV@Undefined{%
6635 \expandafter\FV@BreakBefore@Process@i\expandafter{##1}%
6636 \expandafter\ifx\expandafter\relax\detokenize{##2}\relax
6637 \else
6638 \FV@BreakBefore@Process##2\FV@Undefined
6639 \fi
6640 }%
6641 \def\FV@BreakBefore@Process@i##1{%
6642 \g@addto@macro\FV@BreakBefore@Def{%
6643 \@namedef{FV@BreakBefore@Token\detokenize{##1}}{}}%
6644 }%
6645 \FV@EscChars
6646 \expandafter\FV@BreakBefore@Process\FV@BreakBefore\FV@Undefined
6647 \endgroup
6648 \FV@BreakBefore@Def
6649 \FV@BreakBeforePrep@PygmentsHook
6650 \fi
6651 }
6652 \def\FV@BreakBeforePrep@PygmentsHook{}
6653 % \end{macrocode}
6654 % \end{macro}
6655 %
6656 % \begin{macro}{\FV@BreakAfter}
6657 % Allow line breaking (almost) anywhere, but only after specified characters.
6658 % \begin{macrocode}
6659 \define@key{FV}{breakafter}{%
6660 \ifstrempty{#1}%
6661 {\let\FV@BreakAfter\@empty
6662 \let\FancyVerbBreakStart\relax
6663 \let\FancyVerbBreakStop\relax
6664 \let\FV@Break@DefaultToken\FV@Break@NBToken}%
6665 {\def\FV@BreakAfter{#1}%
6666 \let\FancyVerbBreakStart\FV@Break
6667 \let\FancyVerbBreakStop\FV@EndBreak
6668 \let\FV@Break@DefaultToken\FV@Break@BeforeAfterToken}%
6669 }
6670 \fvset{breakafter={}}
6671 % \end{macrocode}
6672 % \end{macro}
6673 %
6674 % \begin{macro}{FV@breakafterinrun}
6675 % Determine whether breaking after specified characters is always allowed after each individual character, or is only allowed after the last in a run of identical characters.
6676 % \begin{macrocode}
6677 \newbool{FV@breakafterinrun}
6678 \define@booleankey{FV}{breakafterinrun}%
6679 {\booltrue{FV@breakafterinrun}}%
6680 {\boolfalse{FV@breakafterinrun}}%
6681 \fvset{breakafterinrun=false}
6682 % \end{macrocode}
6683 % \end{macro}
6684 %
6685 % \begin{macro}{\FV@BreakAfterPrep}
6686 % This is the |breakafter| equivalent of |\FV@BreakBeforePrep|. It is also used within |\FV@FormattingPrep|. The order of |\FV@BreakBeforePrep| and |\FV@BreakAfterPrep| is important; |\FV@BreakAfterPrep| must always be second, because it checks for conflicts with |breakbefore|.
6687 %
6688 % A pdfTeX-compatible version for working with UTF-8 is defined later, and |\FV@BreakAfterPrep| is |\let| to it under pdfTeX as necessary.
6689 % \begin{macrocode}
6690 \def\FV@BreakAfterPrep{%
6691 \ifx\FV@BreakAfter\@empty\relax
6692 \else
6693 \gdef\FV@BreakAfter@Def{}%
6694 \begingroup
6695 \def\FV@BreakAfter@Process##1##2\FV@Undefined{%
6696 \expandafter\FV@BreakAfter@Process@i\expandafter{##1}%
6697 \expandafter\ifx\expandafter\relax\detokenize{##2}\relax
6698 \else
6699 \FV@BreakAfter@Process##2\FV@Undefined
6700 \fi
6701 }%
6702 \def\FV@BreakAfter@Process@i##1{%
6703 \ifcsname FV@BreakBefore@Token\detokenize{##1}\endcsname
6704 \ifbool{FV@breakbeforeinrun}%
6705 {\ifbool{FV@breakafterinrun}%
6706 {}%
6707 {\PackageError{fvextra}%
6708 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}%
6709 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}}}%
6710 {\ifbool{FV@breakafterinrun}%
6711 {\PackageError{fvextra}%
6712 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}%
6713 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}}%
6714 {}}%
6715 \fi
6716 \g@addto@macro\FV@BreakAfter@Def{%
6717 \@namedef{FV@BreakAfter@Token\detokenize{##1}}{}}%
6718 }%
6719 \FV@EscChars
6720 \expandafter\FV@BreakAfter@Process\FV@BreakAfter\FV@Undefined
6721 \endgroup
6722 \FV@BreakAfter@Def
6723 \FV@BreakAfterPrep@PygmentsHook
6724 \fi
6725 }
6726 \def\FV@BreakAfterPrep@PygmentsHook{}
6727 % \end{macrocode}
6728 % \end{macro}
6729 %
6730 % Now that |\FV@BreakBeforePrep| and |\FV@BreakAfterPrep| are defined, add them to |\FV@FormattingPrep@PreHook|, which is the \fvextra\ extension to |\FV@FormattingPrep|. The ordering here is important, since |\FV@BreakAfterPrep| contains compatibility checks with |\FV@BreakBeforePrep|, and thus must be used after it. Also, we have to check for the pdfTeX engine with \pkg{inputenc} using UTF-8, and use the |UTF| macros instead when that is the case.
6731 % \begin{macrocode}
6732 \g@addto@macro\FV@FormattingPrep@PreHook{%
6733 \ifFV@pdfTeXinputenc
6734 \ifdefstring{\inputencodingname}{utf8}%
6735 {\let\FV@BreakBeforePrep\FV@BreakBeforePrep@UTF
6736 \let\FV@BreakAfterPrep\FV@BreakAfterPrep@UTF}%
6737 {}%
6738 \fi
6739 \FV@BreakBeforePrep\FV@BreakAfterPrep}
6740 % \end{macrocode}
6741 %
6742 %
6743 % \begin{macro}{\FancyVerbBreakAnywhereSymbolPre}
6744 % The pre-break symbol for breaks introduced by |breakanywhere|. That is, the symbol before breaks that occur between characters, rather than at spaces.
6745 % \begin{macrocode}
6746 \define@key{FV}{breakanywheresymbolpre}{%
6747 \ifstrempty{#1}%
6748 {\def\FancyVerbBreakAnywhereSymbolPre{}}%
6749 {\def\FancyVerbBreakAnywhereSymbolPre{\hbox{#1}}}}
6750 \fvset{breakanywheresymbolpre={\,\footnotesize\ensuremath{_\rfloor}}}
6751 % \end{macrocode}
6752 % \end{macro}
6753 %
6754 % \begin{macro}{\FancyVerbBreakAnywhereSymbolPost}
6755 % The post-break symbol for breaks introduced by |breakanywhere|.
6756 % \begin{macrocode}
6757 \define@key{FV}{breakanywheresymbolpost}{%
6758 \ifstrempty{#1}%
6759 {\def\FancyVerbBreakAnywhereSymbolPost{}}%
6760 {\def\FancyVerbBreakAnywhereSymbolPost{\hbox{#1}}}}
6761 \fvset{breakanywheresymbolpost={}}
6762 % \end{macrocode}
6763 % \end{macro}
6764 %
6765 % \begin{macro}{\FancyVerbBreakBeforeSymbolPre}
6766 % The pre-break symbol for breaks introduced by |breakbefore|.
6767 % \begin{macrocode}
6768 \define@key{FV}{breakbeforesymbolpre}{%
6769 \ifstrempty{#1}%
6770 {\def\FancyVerbBreakBeforeSymbolPre{}}%
6771 {\def\FancyVerbBreakBeforeSymbolPre{\hbox{#1}}}}
6772 \fvset{breakbeforesymbolpre={\,\footnotesize\ensuremath{_\rfloor}}}
6773 % \end{macrocode}
6774 % \end{macro}
6775 %
6776 % \begin{macro}{\FancyVerbBreakBeforeSymbolPost}
6777 % The post-break symbol for breaks introduced by |breakbefore|.
6778 % \begin{macrocode}
6779 \define@key{FV}{breakbeforesymbolpost}{%
6780 \ifstrempty{#1}%
6781 {\def\FancyVerbBreakBeforeSymbolPost{}}%
6782 {\def\FancyVerbBreakBeforeSymbolPost{\hbox{#1}}}}
6783 \fvset{breakbeforesymbolpost={}}
6784 % \end{macrocode}
6785 % \end{macro}
6786 %
6787 % \begin{macro}{\FancyVerbBreakAfterSymbolPre}
6788 % The pre-break symbol for breaks introduced by |breakafter|.
6789 % \begin{macrocode}
6790 \define@key{FV}{breakaftersymbolpre}{%
6791 \ifstrempty{#1}%
6792 {\def\FancyVerbBreakAfterSymbolPre{}}%
6793 {\def\FancyVerbBreakAfterSymbolPre{\hbox{#1}}}}
6794 \fvset{breakaftersymbolpre={\,\footnotesize\ensuremath{_\rfloor}}}
6795 % \end{macrocode}
6796 % \end{macro}
6797 %
6798 % \begin{macro}{\FancyVerbBreakAfterSymbolPost}
6799 % The post-break symbol for breaks introduced by |breakafter|.
6800 % \begin{macrocode}
6801 \define@key{FV}{breakaftersymbolpost}{%
6802 \ifstrempty{#1}%
6803 {\def\FancyVerbBreakAfterSymbolPost{}}%
6804 {\def\FancyVerbBreakAfterSymbolPost{\hbox{#1}}}}
6805 \fvset{breakaftersymbolpost={}}
6806 % \end{macrocode}
6807 % \end{macro}
6808 %
6809 % \begin{macro}{\FancyVerbBreakAnywhereBreak}
6810 % The macro governing breaking for |breakanywhere=true|.
6811 % \begin{macrocode}
6812 \newcommand{\FancyVerbBreakAnywhereBreak}{%
6813 \discretionary{\FancyVerbBreakAnywhereSymbolPre}%
6814 {\FancyVerbBreakAnywhereSymbolPost}{}}
6815 % \end{macrocode}
6816 % \end{macro}
6817 %
6818 %
6819 % \begin{macro}{\FancyVerbBreakBeforeBreak}
6820 % The macro governing breaking for |breakbefore=true|.
6821 % \begin{macrocode}
6822 \newcommand{\FancyVerbBreakBeforeBreak}{%
6823 \discretionary{\FancyVerbBreakBeforeSymbolPre}%
6824 {\FancyVerbBreakBeforeSymbolPost}{}}
6825 % \end{macrocode}
6826 % \end{macro}
6827 %
6828 % \begin{macro}{\FancyVerbBreakAfterBreak}
6829 % The macro governing breaking for |breakafter=true|.
6830 % \begin{macrocode}
6831 \newcommand{\FancyVerbBreakAfterBreak}{%
6832 \discretionary{\FancyVerbBreakAfterSymbolPre}%
6833 {\FancyVerbBreakAfterSymbolPost}{}}
6834 % \end{macrocode}
6835 % \end{macro}
6836 %
6837 %
6838 % \begin{macro}{breaknonspaceingroup}
6839 % \begin{macro}{FV@breaknonspaceingroup}
6840 % When inserting breaks, insert breaks within groups (typically |{...}| but depends on |commandchars|) instead of skipping over them. This isn't the default because it is incompabile with many macros since it inserts breaks into all arguments. For those cases, redefining macros to use |\FancyVerbBreakStart...\FancyVerbBreakStop| to insert breaks is better.
6841 % \begin{macrocode}
6842 \newbool{FV@breaknonspaceingroup}
6843 \define@booleankey{FV}{breaknonspaceingroup}%
6844 {\booltrue{FV@breaknonspaceingroup}}%
6845 {\boolfalse{FV@breaknonspaceingroup}}
6846 \fvset{breaknonspaceingroup=false}
6847 % \end{macrocode}
6848 % \end{macro}
6849 % \end{macro}
6850 %
6851 %
6852 % \begin{macro}{breakpreferspaces,\FV@BreakHyphenation}
6853 % Adjust hyphenation settings for |breaklines|.
6854 %
6855 % When |breakbefore|, |breakafter|, or |breakanywhere| are in use, |\finalhyphendemerits=0| prevents the final ``word'' on a line from being put into a line segment by itself after line breaking. Otherwise, \TeX\ tries to avoid a line break (hyphenation) within a ``word'' on the penultimate line segment, and may accomplish this by introducing an unnecessary line break at the space before the last ``word.''
6856 %
6857 % |breakpreferspaces| (|\linepenalty|) determines whether line breaks are preferentially inserted at normal spaces (|breakcollapsespaces=true|, |showspaces=false|) rather than at other locations allowed by |breakbefore|, |breakafter|, or |breakanywhere|.
6858 % \begin{macrocode}
6859 \newbool{FV@breakpreferspaces}
6860 \booltrue{FV@breakpreferspaces}
6861 \define@booleankey{FV}{breakpreferspaces}%
6862 {\booltrue{FV@breakpreferspaces}}%
6863 {\boolfalse{FV@breakpreferspaces}}
6864 \def\FV@BreakHyphenation{%
6865 \finalhyphendemerits=0\relax
6866 \ifbool{FV@breakpreferspaces}{}{\linepenalty=\@M\relax}}
6867 \g@addto@macro\FV@FormattingPrep@PreHook{\FV@BreakHyphenation}
6868 % \end{macrocode}
6869 % \end{macro}
6870 %
6871 %
6872 % \subsubsection{Line breaking implementation}
6873 %
6874 % \paragraph{Helper macros}\hfill\\
6875 %
6876 % \begin{macro}{\FV@LineBox}
6877 % A box for saving a line of text, so that its dimensions may be determined and thus we may figure out if it needs line breaking.
6878 % \begin{macrocode}
6879 \newsavebox{\FV@LineBox}
6880 % \end{macrocode}
6881 % \end{macro}
6882 %
6883 % \begin{macro}{\FV@LineIndentBox}
6884 % A box for saving the indentation of code, so that its dimensions may be determined for use in auto-indentation of continuation lines.
6885 % \begin{macrocode}
6886 \newsavebox{\FV@LineIndentBox}
6887 % \end{macrocode}
6888 % \end{macro}
6889 %
6890 % \begin{macro}{\FV@LineIndentChars}
6891 % A macro for storing the indentation characters, if any, of a given line. For use in auto-indentation of continuation lines
6892 % \begin{macrocode}
6893 \let\FV@LineIndentChars\@empty
6894 % \end{macrocode}
6895 % \end{macro}
6896 %
6897 % \begin{macro}{\FV@GetLineIndent}
6898 % A macro that takes a line and determines the indentation, storing the indentation chars in |\FV@LineIndentChars|.
6899 % \begin{macrocode}
6900 \def\FV@GetLineIndent{%
6901 \@ifnextchar\FV@Sentinel
6902 {\FV@GetLineIndent@End}%
6903 {\ifx\@let@token\FV@FVSpaceToken
6904 \let\FV@Next\FV@GetLineIndent@Whitespace
6905 \else\ifx\@let@token\FV@FVTabToken
6906 \let\FV@Next\FV@GetLineIndent@Whitespace
6907 \else\ifcsname FV@PYG@Redefed\endcsname
6908 \ifx\@let@token\FV@PYG@Redefed
6909 \let\FV@Next\FV@GetLineIndent@Pygments
6910 \else
6911 \let\FV@Next\FV@GetLineIndent@End
6912 \fi
6913 \else
6914 \let\FV@Next\FV@GetLineIndent@End
6915 \fi\fi\fi
6916 \FV@Next}}
6917 \def\FV@GetLineIndent@End#1\FV@Sentinel{}
6918 \def\FV@GetLineIndent@Whitespace#1{%
6919 \expandafter\def\expandafter\FV@LineIndentChars\expandafter{\FV@LineIndentChars#1}%
6920 \FV@GetLineIndent}
6921 \def\FV@GetLineIndent@Pygments#1#2#3{%
6922 \FV@GetLineIndent#3}
6923 % \end{macrocode}
6924 % \end{macro}
6925 %
6926 %
6927 % \paragraph{Tab expansion}\hfill\\
6928 %
6929 % \noindent The \fancyvrb\ option |obeytabs| uses a clever algorithm involving boxing and unboxing to expand tabs based on tab stops rather than a fixed number of equivalent space characters. (See the definitions of |\FV@@ObeyTabs| and |\FV@TrueTab| in \cref{sec:patch:obeytabs}.) Unfortunately, since this involves |\hbox|, it interferes with the line breaking algorithm, and an alternative is required.
6930 %
6931 % There are probably many ways tab expansion could be performed while still allowing line breaks. The current approach has been chosen because it is relatively straightforward and yields identical results to the case without line breaks. Line breaking involves saving a line in a box, and determining whether the box is too wide. During this process, if |obeytabs=true|, |\FV@TrueTabSaveWidth|, which is inside |\FV@TrueTab|, is |\let| to a version that saves the width of every tab in a macro. When a line is broken, all tabs within it will then use a variant of |\FV@TrueTab| that sequentially retrieves the saved widths. This maintains the exact behavior of the case without line breaks.
6932 %
6933 % Note that the special version of |\FV@TrueTab| is based on the \fvextra\ patched version of |\FV@TrueTab|, not on the original |\FV@TrueTab| defined in \fancyvrb.
6934 %
6935 %
6936 % \begin{macro}{\FV@TrueTab@UseWidth}
6937 % Version of |\FV@TrueTab| that uses pre-computed tab widths.
6938 % \begin{macrocode}
6939 \def\FV@TrueTab@UseWidth{%
6940 \@tempdima=\csname FV@TrueTab:Width\arabic{FV@TrueTabCounter}\endcsname sp\relax
6941 \stepcounter{FV@TrueTabCounter}%
6942 \hbox to\@tempdima{\hss\FV@TabChar}}
6943 % \end{macrocode}
6944 % \end{macro}
6945 %
6946 %
6947 %
6948 % \paragraph{Line scanning and break insertion macros}\hfill\\
6949 %
6950 % \noindent The strategy here is to scan through text token by token, inserting potential breaks at appropriate points. The final text with breaks inserted is stored in |\FV@BreakBuffer|, which is ultimately passed on to a wrapper macro like |\FancyVerbFormatText| or |\FancyVerbFormatInline|.
6951 %
6952 % If user macros insert breaks via |\FancyVerbBreakStart...\FancyVerbBreakStop|, this invokes an additional scanning/insertion pass within each macro after expansion. The scanning/insertion only applies to the part of the expanded macros wrapped in |\FancyVerbBreakStart...\FancyVerbBreakStop|. At the time this occurs, during macro processing, text will already be wrapped in a wrapper macro like |\FancyVerbFormatText| or |\FancyVerbFormatInline|. That is, the built-in break insertion occurs before any typesetting, but user macro break insertion occurs during typesetting.
6953 %
6954 % Token comparison is currently based on |\ifx|. This is sufficient for verbatim text but a comparison based on |\detokenize| might be better for cases when |commandchars| is in use. For example, with |commandchars| characters other than the curly braces |{}| might be the group tokens.
6955 %
6956 % It would be possible to insert each token/group into the document immediately after it is scanned, instead of accumulating them in a ``buffer.'' But that would interfere with macros. Even in the current approach, macros that take optional arguments are problematic, since with some settings breaks will interference with optional arguments.\footnote{Through a suitable definition that tracks the current state and looks for square brackets, this might be circumvented. Then again, in verbatim contexts, macro use should be minimal, so the restriction to macros without optional arguments should generally not be an issue.}
6957 %
6958 % The last token is tracked with |\FV@LastToken|, to allow lookbehind when breaking by groups of identical characters. |\FV@LastToken| is |\let| to |\FV@Undefined| any time the last token was something that shouldn't be compared against (for example, a non-empty group), and it is not reset whenever the last token may be ignored (for example, |{}|). When setting |\FV@LastToken|, it is vital always to use |\let\FV@LastToken=...| so that |\let\FV@LastToken==| will work (so that the equals sign |=| won't break things).
6959 %
6960 % \begin{macro}{FV@BreakBufferDepth}
6961 % Track buffer depth while inserting breaks. Some macros and command sequences require recursive processing. For example, groups |{...}| (with |commandchars| and |breaknonspaceingroup|), math, and nested |\FancyVerbBreakStart...\FancyVerbBreakStop|. Depth starts at zero. The current buffer at depth $n$ is always |\FV@BreakBuffer|, with other buffers |\FV@BreakBuffer<n>| etc.\ named via |\csname| to allow for the integer.
6962 % \begin{macrocode}
6963 \newcounter{FV@BreakBufferDepth}
6964 % \end{macrocode}
6965 % \end{macro}
6966 %
6967 %
6968 % \begin{macro}{\FV@BreakBuffer@Append}
6969 % Append to |\FV@BreakBuffer|.
6970 % \begin{macrocode}
6971 \def\FV@BreakBuffer@Append#1{%
6972 \expandafter\def\expandafter\FV@BreakBuffer\expandafter{\FV@BreakBuffer#1}}
6973 % \end{macrocode}
6974 % \end{macro}
6975 %
6976 %
6977 % \begin{macro}{\FV@BreakBufferStart}
6978 % Create a new buffer, either at the beginning of scanning or during recursion. The single mandatory argument is the macro for handling tokens, which is |\let| to |\FV@Break@Token|. An intermediate |\FV@BreakBufferStart@i| is used to optimize |\ifx| comparisons for |\FV@BreakBufferStart| during scanning.
6979 %
6980 % For recursion, |\FV@BreakBuffer<n>| and |\FV@Break@Token<n>| store the state (buffer and token handling macro) immediately prior to recursion with depth |<n>|.
6981 % \begin{macrocode}
6982 \def\FV@BreakBufferStart{%
6983 \FV@BreakBufferStart@i}
6984 \def\FV@BreakBufferStart@i#1{%
6985 \ifnum\value{FV@BreakBufferDepth}>0\relax
6986 \expandafter\let\csname FV@BreakBuffer\arabic{FV@BreakBufferDepth}\endcsname
6987 \FV@BreakBuffer
6988 \expandafter\let\csname FV@Break@Token\arabic{FV@BreakBufferDepth}\endcsname
6989 \FV@Break@Token
6990 \fi
6991 \def\FV@BreakBuffer{}%
6992 \let\FV@Break@Token=#1%
6993 \stepcounter{FV@BreakBufferDepth}%
6994 \let\FV@LastToken=\FV@Undefined
6995 \FV@Break@Scan}
6996 % \end{macrocode}
6997 % \end{macro}
6998 %
6999 %
7000 % \begin{macro}{FV@UserMacroBreaks}
7001 % Whether a user macro is inserting breaks, as opposed to \pkg{fvextra}'s standard scanning routine. When breaks come from \pkg{fvextra}, |\FV@BreakBufferStop| does nothing with |\FV@BreakBuffer| at buffer depth 0, since |\FV@InsertBreaks| handles buffer insertion. When breaks come from user macros, |\FV@BreakBufferStop| needs to insert |\FV@BreakBuffer| at buffer depth 0.
7002 % \begin{macrocode}
7003 \newbool{FV@UserMacroBreaks}
7004 % \end{macrocode}
7005 % \end{macro}
7006 %
7007 %
7008 % \begin{macro}{\FV@BreakBufferStop}
7009 % Complete the current buffer. The single mandatory argument is a wrapper macro for |\FV@BreakBuffer|'s contents (for example, insert recursively scanned group into braces |{...}|). If the mandatory argument is empty, no wrapper is used.
7010 %
7011 % For \pkg{fvextra}'s standard scanning: If this is the main buffer (depth 0), stop scanning---which ultimately allows |\FV@BreakBuffer| to be handled by |\FV@InsertBreaks|. For user macros: Insert |\FV@BreakBuffer| at buffer depth 0. Otherwise for both cases: Append the current buffer to the previous buffer, and continue scanning.
7012 %
7013 % An intermediate |\FV@BreakBufferStop@i| is used to optimize |\ifx| comparisons for |\FV@BreakBufferStop| during scanning.
7014 % \begin{macrocode}
7015 \def\FV@BreakBufferStop{%
7016 \FV@BreakBufferStop@i}
7017 \def\FV@BreakBufferStop@i#1{%
7018 \addtocounter{FV@BreakBufferDepth}{-1}%
7019 \let\FV@LastToken=\FV@Undefined
7020 \ifnum\value{FV@BreakBufferDepth}<0\relax
7021 \PackageError{fvextra}%
7022 {Line break insertion error (extra \string\FancyVerbBreakStop?)}%
7023 {Line break insertion error (extra \string\FancyVerbBreakStop?)}%
7024 \def\FV@BreakBuffer{}%
7025 \fi
7026 \ifnum\value{FV@BreakBufferDepth}>0\relax
7027 \expandafter\@firstoftwo
7028 \else
7029 \expandafter\@secondoftwo
7030 \fi
7031 {\expandafter\FV@BreakBufferStop@ii\expandafter{\FV@BreakBuffer}{#1}}%
7032 {\ifbool{FV@UserMacroBreaks}%
7033 {\expandafter\let\expandafter\FV@BreakBuffer\expandafter\FV@Undefined\FV@BreakBuffer}%
7034 {}}}
7035 \def\FV@BreakBufferStop@ii#1#2{%
7036 \ifstrempty{#2}%
7037 {\FV@BreakBufferStop@iii{#1}}%
7038 {\expandafter\FV@BreakBufferStop@iii\expandafter{#2{#1}}}}
7039 \def\FV@BreakBufferStop@iii#1{%
7040 \expandafter\let\expandafter\FV@BreakBufferUpLevel
7041 \csname FV@BreakBuffer\arabic{FV@BreakBufferDepth}\endcsname
7042 \expandafter\def\expandafter\FV@BreakBuffer\expandafter{\FV@BreakBufferUpLevel#1}%
7043 \expandafter\let\expandafter\FV@Break@Token
7044 \csname FV@Break@Token\arabic{FV@BreakBufferDepth}\endcsname
7045 \FV@Break@Scan}
7046 % \end{macrocode}
7047 % \end{macro}
7048 %
7049 %
7050 % \begin{macro}{\FV@InsertBreaks}
7051 % This inserts breaks within text (|#2|) and stores the result in |\FV@BreakBuffer|. Then it invokes a macro (|#1|) on the result. That allows |\FancyVerbFormatInline| and |\FancyVerbFormatText| to operate on the final text (with breaks) directly, rather than being given text without breaks or text wrapped with macros that will (potentially recursively) insert breaks. (Breaks inserted by user macros are not yet present, though, since they are only inserted---potentially recursively---during macro processing.)
7052 %
7053 % The initial |\ifx| skips break insertion when break insertion is turned off (|\FancyVerbBreakStart| is |\relax|).
7054 %
7055 % The current definition of |\FV@Break@Token| is swapped for a UTF-8 compatible one under pdfTeX when necessary. In what follows, the default macros are defined after |\FV@Break|, since they make the algorithms simpler to understand. The more complex |UTF| variants are defined afterward.
7056 % \begin{macrocode}
7057 \def\FV@InsertBreaks#1#2{%
7058 \ifx\FancyVerbBreakStart\relax
7059 \expandafter\@firstoftwo
7060 \else
7061 \expandafter\@secondoftwo
7062 \fi
7063 {#1{#2}}%
7064 {\ifFV@pdfTeXinputenc
7065 \ifdefstring{\inputencodingname}{utf8}%
7066 {\ifx\FV@Break@DefaultToken\FV@Break@AnyToken
7067 \let\FV@Break@DefaultToken\FV@Break@AnyToken@UTF
7068 \else
7069 \ifx\FV@Break@DefaultToken\FV@Break@BeforeAfterToken
7070 \let\FV@Break@DefaultToken\FV@Break@BeforeAfterToken@UTF
7071 \fi
7072 \fi}%
7073 {}%
7074 \fi
7075 \setcounter{FV@BreakBufferDepth}{0}%
7076 \boolfalse{FV@UserMacroBreaks}%
7077 \FancyVerbBreakStart#2\FancyVerbBreakStop
7078 \setcounter{FV@BreakBufferDepth}{0}%
7079 \booltrue{FV@UserMacroBreaks}%
7080 \expandafter\FV@InsertBreaks@i\expandafter{\FV@BreakBuffer}{#1}}}
7081 \def\FV@InsertBreaks@i#1#2{%
7082 \let\FV@BreakBuffer\FV@Undefined
7083 #2{#1}}
7084 % \end{macrocode}
7085 % \end{macro}
7086 %
7087 %
7088 % \begin{macro}{\FV@Break}
7089 % The entry macro for break insertion. Whatever is delimited (after expansion) by |\FV@Break...\FV@EndBreak| will be scanned token by token/group by group, and accumulated (with any added breaks) in |\FV@BreakBuffer|. After scanning is complete, |\FV@BreakBuffer| will be inserted.
7090 % \begin{macrocode}
7091 \def\FV@Break{%
7092 \FV@BreakBufferStart{\FV@Break@DefaultToken}}
7093 % \end{macrocode}
7094 % \end{macro}
7095 %
7096 % \begin{macro}{\FV@EndBreak}
7097 % \begin{macrocode}
7098 \def\FV@EndBreak{%
7099 \FV@BreakBufferStop{}}
7100 % \end{macrocode}
7101 % \end{macro}
7102 %
7103 %
7104 % \begin{macro}{\FV@Break@Scan}
7105 % Look ahead via |\@ifnextchar|. Don't do anything if we're at the end of the region to be scanned. Otherwise, invoke a macro to deal with what's next based on whether it is math, or a group, or something else.
7106 %
7107 % This and some following macros are defined inside of groups to ensure proper catcodes.
7108 %
7109 % The check against |\FV@BreakBufferStart| should typically not be necessary; it is included for completeness and to allow for future extensions and customization. |\FV@BreakBufferStart| is only inserted raw (rather than wrapped in |\FancyVerbBreakStart|) in token processing macros, where it initiates (or restarts) scanning and is not itself scanned.
7110 %
7111 % \begin{macrocode}
7112 \begingroup
7113 \catcode`\$=3
7114 \gdef\FV@Break@Scan{%
7115 \@ifnextchar\FancyVerbBreakStart%
7116 {}%
7117 {\ifx\@let@token\FancyVerbBreakStop
7118 \let\FV@Break@Next\relax
7119 \else\ifx\@let@token\FV@BreakBufferStart
7120 \let\FV@Break@Next\relax
7121 \else\ifx\@let@token\FV@BreakBufferStop
7122 \let\FV@Break@Next\relax
7123 \else\ifx\@let@token$
7124 \let\FV@Break@Next\FV@Break@Math
7125 \else\ifx\@let@token\bgroup
7126 \let\FV@Break@Next\FV@Break@Group
7127 \else
7128 \let\FV@Break@Next\FV@Break@Token
7129 \fi\fi\fi\fi\fi
7130 \FV@Break@Next}}
7131 \endgroup
7132 % \end{macrocode}
7133 % \end{macro}
7134 %
7135 %
7136 % \begin{macro}{\FV@Break@Math}
7137 % Grab an entire math span, and insert it into |\FV@BreakBuffer|. Due to grouping, this works even when math contains things like |\text{$x$}|. After dealing with the math span, continue scanning.
7138 % \begin{macrocode}
7139 \begingroup
7140 \catcode`\$=3%
7141 \gdef\FV@Break@Math$#1${%
7142 \FV@BreakBufferStart{\FV@Break@NBToken}#1\FV@BreakBufferStop{\FV@Break@MathTemplate}}
7143 \gdef\FV@Break@MathTemplate#1{$#1$}
7144 \endgroup
7145 % \end{macrocode}
7146 % \end{macro}
7147 %
7148 % \begin{macro}{\FV@Break@Group}
7149 % Grab the group, and insert it into |\FV@BreakBuffer| (as a group) before continuing scanning.
7150 % \begin{macrocode}
7151 \def\FV@Break@Group#1{%
7152 \ifstrempty{#1}%
7153 {\FV@BreakBuffer@Append{{}}%
7154 \FV@Break@Scan}%
7155 {\ifbool{FV@breaknonspaceingroup}%
7156 {\FV@BreakBufferStart{\FV@Break@DefaultToken}%
7157 #1\FV@BreakBufferStop{\FV@Break@GroupTemplate}}%
7158 {\FV@BreakBufferStart{\FV@Break@NBToken}%
7159 #1\FV@BreakBufferStop{\FV@Break@GroupTemplate}}}}
7160 \def\FV@Break@GroupTemplate#1{{#1}}
7161 % \end{macrocode}
7162 % \end{macro}
7163 %
7164 %
7165 % \begin{macro}{\FV@Break@NBToken}
7166 % Append token to buffer while adding no breaks (|NB|) and reset last token.
7167 % \begin{macrocode}
7168 \def\FV@Break@NBToken#1{%
7169 \FV@BreakBuffer@Append{#1}%
7170 \let\FV@LastToken=\FV@Undefined
7171 \FV@Break@Scan}
7172 % \end{macrocode}
7173 % \end{macro}
7174 %
7175 %
7176 % \begin{macro}{\FV@Break@AnyToken}
7177 % Deal with breaking around any token. This doesn't break macros with \emph{mandatory} arguments, because |\FancyVerbBreakAnywhereBreak| is inserted \emph{before} the token. Groups themselves are added without any special handling. So a macro would end up right next to its original arguments, without anything being inserted. Optional arguments will cause this approach to fail; there is currently no attempt to identify them, since that is a much harder problem.
7178 %
7179 % If it is ever necessary, it would be possible to create a more sophisticated version involving catcode checks via |\ifcat|. Something like this:
7180 %
7181 %~
7182 %\hrule
7183 %\begin{verbatim}
7184 %\begingroup
7185 %\catcode`\a=11%
7186 %\catcode`\+=12%
7187 %\gdef\FV@Break...
7188 % \ifcat\noexpand#1a%
7189 % \FV@BreakBuffer@Append...
7190 % \else
7191 %...
7192 %\endgroup
7193 %\end{verbatim}
7194 %\hrule\par~
7195 %
7196 % \begin{macrocode}
7197 \def\FV@Break@AnyToken#1{%
7198 \ifx\FV@FVSpaceToken#1\relax
7199 \expandafter\@firstoftwo
7200 \else
7201 \expandafter\@secondoftwo
7202 \fi
7203 {\let\FV@LastToken=#1\FV@BreakBuffer@Append{#1}\FV@Break@Scan}%
7204 {\ifx\FV@LastToken\FV@FVSpaceToken
7205 \expandafter\@firstoftwo
7206 \else
7207 \expandafter\@secondoftwo
7208 \fi
7209 {\let\FV@LastToken=#1%
7210 \FV@BreakBuffer@Append{#1}\FV@Break@Scan}%
7211 {\let\FV@LastToken=#1%
7212 \FV@BreakBuffer@Append{\FancyVerbBreakAnywhereBreak#1}\FV@Break@Scan}}}
7213 % \end{macrocode}
7214 % \end{macro}
7215 %
7216 % \begin{macro}{\FV@Break@BeforeAfterToken}
7217 % Deal with breaking around only specified tokens. This is a bit trickier. We only break if a macro corresponding to the token exists. We also need to check whether the specified token should be grouped, that is, whether breaks are allowed between identical characters. All of this has to be written carefully so that nothing is accidentally inserted into the stream for future scanning.
7218 %
7219 % Dealing with tokens followed by empty groups (for example, |\x{}|) is particularly challenging when we want to avoid breaks between identical characters. When a token is followed by a group, we need to save the current token for later reference (|\x| in the example), then capture and save the following group, and then---only if the group was empty---see if the following token is identical to the old saved token.
7220 %
7221 % The |\csname @let@token\endcsname| prevents issues if |\@let@token| is ever |\else| or |\fi|.
7222 % \begin{macrocode}
7223 \def\FV@Break@BeforeAfterToken#1{%
7224 \ifcsname FV@BreakBefore@Token\detokenize{#1}\endcsname
7225 \let\FV@Break@Next\FV@Break@BeforeTokenBreak
7226 \else
7227 \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7228 \let\FV@Break@Next\FV@Break@AfterTokenBreak
7229 \else
7230 \let\FV@Break@Next\FV@Break@BeforeAfterTokenNoBreak
7231 \fi
7232 \fi
7233 \FV@Break@Next{#1}%
7234 }
7235 \def\FV@Break@BeforeAfterTokenNoBreak#1{%
7236 \FV@BreakBuffer@Append{#1}%
7237 \let\FV@LastToken=#1%
7238 \FV@Break@Scan}
7239 \def\FV@Break@BeforeTokenBreak#1{%
7240 \ifbool{FV@breakbeforeinrun}%
7241 {\ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7242 \ifx#1\FV@FVSpaceToken
7243 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7244 \else
7245 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak}%
7246 \fi
7247 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan
7248 \def\FV@RescanToken{#1}%
7249 \else
7250 \ifx#1\FV@FVSpaceToken
7251 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak#1}%
7252 \else
7253 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak#1}%
7254 \fi
7255 \let\FV@Break@Next\FV@Break@Scan
7256 \let\FV@LastToken=#1%
7257 \fi}%
7258 {\ifx#1\FV@LastToken\relax
7259 \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7260 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan
7261 \def\FV@RescanToken{#1}%
7262 \else
7263 \FV@BreakBuffer@Append{#1}%
7264 \let\FV@Break@Next\FV@Break@Scan
7265 \let\FV@LastToken=#1%
7266 \fi
7267 \else
7268 \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7269 \ifx#1\FV@FVSpaceToken
7270 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7271 \else
7272 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak}%
7273 \fi
7274 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan
7275 \def\FV@RescanToken{#1}%
7276 \else
7277 \ifx#1\FV@FVSpaceToken
7278 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak#1}%
7279 \else
7280 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak#1}%
7281 \fi
7282 \let\FV@Break@Next\FV@Break@Scan
7283 \let\FV@LastToken=#1%
7284 \fi
7285 \fi}%
7286 \FV@Break@Next}
7287 \def\FV@Break@BeforeTokenBreak@AfterRescan{%
7288 \expandafter\FV@Break@AfterTokenBreak\FV@RescanToken}
7289 \def\FV@Break@AfterTokenBreak#1{%
7290 \let\FV@LastToken=#1%
7291 \@ifnextchar\FV@FVSpaceToken%
7292 {\ifx#1\FV@FVSpaceToken
7293 \expandafter\@firstoftwo
7294 \else
7295 \expandafter\@secondoftwo
7296 \fi
7297 {\FV@Break@AfterTokenBreak@i{#1}}%
7298 {\FV@BreakBuffer@Append{#1}%
7299 \FV@Break@Scan}}%
7300 {\FV@Break@AfterTokenBreak@i{#1}}}
7301 \def\FV@Break@AfterTokenBreak@i#1{%
7302 \ifbool{FV@breakafterinrun}%
7303 {\ifx#1\FV@FVSpaceToken
7304 \FV@BreakBuffer@Append{#1\FancyVerbSpaceBreak}%
7305 \else
7306 \FV@BreakBuffer@Append{#1\FancyVerbBreakAfterBreak}%
7307 \fi
7308 \let\FV@Break@Next\FV@Break@Scan}%
7309 {\ifx\@let@token#1\relax
7310 \FV@BreakBuffer@Append{#1}%
7311 \let\FV@Break@Next\FV@Break@Scan
7312 \else
7313 \expandafter\ifx\csname @let@token\endcsname\bgroup\relax
7314 \FV@BreakBuffer@Append{#1}%
7315 \let\FV@Break@Next\FV@Break@AfterTokenBreak@Group
7316 \else
7317 \ifx#1\FV@FVSpaceToken
7318 \FV@BreakBuffer@Append{#1\FancyVerbSpaceBreak}%
7319 \else
7320 \FV@BreakBuffer@Append{#1\FancyVerbBreakAfterBreak}%
7321 \fi
7322 \let\FV@Break@Next\FV@Break@Scan
7323 \fi
7324 \fi}%
7325 \FV@Break@Next
7326 }
7327 \def\FV@Break@AfterTokenBreak@Group#1{%
7328 \ifstrempty{#1}%
7329 {\FV@BreakBuffer@Append{{}}%
7330 \@ifnextchar\FV@LastToken%
7331 {\FV@Break@Scan}%
7332 {\ifx\FV@LastToken\FV@FVSpaceToken
7333 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7334 \else
7335 \FV@BreakBuffer@Append{\FancyVerbBreakAfterBreak}%
7336 \fi
7337 \FV@Break@Scan}}%
7338 {\ifx\FV@LastToken\FV@FVSpaceToken
7339 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7340 \else
7341 \FV@BreakBuffer@Append{\FancyVerbBreakAfterBreak}%
7342 \fi
7343 \FV@Break@Group{#1}}}
7344 % \end{macrocode}
7345 % \end{macro}
7346 %
7347 %
7348 % \paragraph{Line scanning and break insertion macros for pdfTeX with UTF-8}\hfill\\
7349 % The macros above work with the XeTeX and LuaTeX engines and are also fine for pdfTeX with 8-bit character encodings. Unfortunately, pdfTeX works with multi-byte UTF-8 code points at the byte level, making things significantly trickier. The code below re-implements the macros in a manner compatible with the \pkg{inputenc} package with option |utf8|. Note that there is no attempt for compatibility with |utf8x|; |utf8| has been significantly improved in recent years and should be sufficient in the vast majority of cases. And implementing variants for |utf8| was already sufficiently painful.
7350 %
7351 % Create macros conditionally:
7352 % \begin{macrocode}
7353 \ifFV@pdfTeXinputenc
7354 % \end{macrocode}
7355 %
7356 % \begin{macro}{\FV@BreakBeforePrep@UTF}
7357 % We need |UTF| variants of the |breakbefore| and |breakafter| prep macros. These are only ever used with \pkg{inputenc} with UTF-8. There is no need for encoding checks here; checks are performed in |\FV@FormattingPrep@PreHook| (checks are inserted into it after the non-|UTF| macro definitions).
7358 % \begin{macrocode}
7359 \def\FV@BreakBeforePrep@UTF{%
7360 \ifx\FV@BreakBefore\@empty\relax
7361 \else
7362 \gdef\FV@BreakBefore@Def{}%
7363 \begingroup
7364 \def\FV@BreakBefore@Process##1{%
7365 \ifcsname FV@U8:\detokenize{##1}\endcsname
7366 \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{##1}\endcsname
7367 \let\FV@UTF@octets@after\FV@BreakBefore@Process@ii
7368 \else
7369 \ifx##1\FV@Undefined
7370 \let\FV@Break@Next\@gobble
7371 \else
7372 \let\FV@Break@Next\FV@BreakBefore@Process@i
7373 \fi
7374 \fi
7375 \FV@Break@Next##1%
7376 }%
7377 \def\FV@BreakBefore@Process@i##1{%
7378 \expandafter\FV@BreakBefore@Process@ii\expandafter{##1}}%
7379 \def\FV@BreakBefore@Process@ii##1{%
7380 \g@addto@macro\FV@BreakBefore@Def{%
7381 \@namedef{FV@BreakBefore@Token\detokenize{##1}}{}}%
7382 \FV@BreakBefore@Process
7383 }%
7384 \FV@EscChars
7385 \expandafter\FV@BreakBefore@Process\FV@BreakBefore\FV@Undefined
7386 \endgroup
7387 \FV@BreakBefore@Def
7388 \FV@BreakBeforePrep@PygmentsHook
7389 \fi
7390 }
7391 % \end{macrocode}
7392 % \end{macro}
7393 %
7394 %
7395 % \begin{macro}{\FV@BreakAfterPrep@UTF}
7396 % \begin{macrocode}
7397 \def\FV@BreakAfterPrep@UTF{%
7398 \ifx\FV@BreakAfter\@empty\relax
7399 \else
7400 \gdef\FV@BreakAfter@Def{}%
7401 \begingroup
7402 \def\FV@BreakAfter@Process##1{%
7403 \ifcsname FV@U8:\detokenize{##1}\endcsname
7404 \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{##1}\endcsname
7405 \let\FV@UTF@octets@after\FV@BreakAfter@Process@ii
7406 \else
7407 \ifx##1\FV@Undefined
7408 \let\FV@Break@Next\@gobble
7409 \else
7410 \let\FV@Break@Next\FV@BreakAfter@Process@i
7411 \fi
7412 \fi
7413 \FV@Break@Next##1%
7414 }%
7415 \def\FV@BreakAfter@Process@i##1{%
7416 \expandafter\FV@BreakAfter@Process@ii\expandafter{##1}}%
7417 \def\FV@BreakAfter@Process@ii##1{%
7418 \ifcsname FV@BreakBefore@Token\detokenize{##1}\endcsname
7419 \ifbool{FV@breakbeforeinrun}%
7420 {\ifbool{FV@breakafterinrun}%
7421 {}%
7422 {\PackageError{fvextra}%
7423 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}%
7424 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}}}%
7425 {\ifbool{FV@breakafterinrun}%
7426 {\PackageError{fvextra}%
7427 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}%
7428 {Conflicting breakbeforeinrun and breakafterinrun for "\detokenize{##1}"}}%
7429 {}}%
7430 \fi
7431 \g@addto@macro\FV@BreakAfter@Def{%
7432 \@namedef{FV@BreakAfter@Token\detokenize{##1}}{}}%
7433 \FV@BreakAfter@Process
7434 }%
7435 \FV@EscChars
7436 \expandafter\FV@BreakAfter@Process\FV@BreakAfter\FV@Undefined
7437 \endgroup
7438 \FV@BreakAfter@Def
7439 \FV@BreakAfterPrep@PygmentsHook
7440 \fi
7441 }
7442 % \end{macrocode}
7443 % \end{macro}
7444 %
7445 %
7446 %
7447 % \begin{macro}{\FV@Break@AnyToken@UTF}
7448 % Instead of just adding each token to |\FV@BreakBuffer| with a preceding break, also check for multi-byte code points and capture the remaining bytes when they are encountered.
7449 % \begin{macrocode}
7450 \def\FV@Break@AnyToken@UTF#1{%
7451 \ifcsname FV@U8:\detokenize{#1}\endcsname
7452 \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname
7453 \let\FV@UTF@octets@after\FV@Break@AnyToken@UTF@i
7454 \else
7455 \let\FV@Break@Next\FV@Break@AnyToken@UTF@i
7456 \fi
7457 \FV@Break@Next{#1}%
7458 }
7459 \def\FV@Break@AnyToken@UTF@i#1{%
7460 \def\FV@CurrentToken{#1}%
7461 \ifx\FV@CurrentToken\FV@ActiveSpaceToken\relax
7462 \expandafter\@firstoftwo
7463 \else
7464 \expandafter\@secondoftwo
7465 \fi
7466 {\let\FV@LastToken\FV@CurrentToken
7467 \FV@BreakBuffer@Append{#1}\FV@Break@Scan}%
7468 {\ifx\FV@LastToken\FV@ActiveSpaceToken
7469 \expandafter\@firstoftwo
7470 \else
7471 \expandafter\@secondoftwo
7472 \fi
7473 {\let\FV@LastToken\FV@CurrentToken
7474 \FV@BreakBuffer@Append{#1}\FV@Break@Scan}%
7475 {\let\FV@LastToken\FV@CurrentToken
7476 \FV@BreakBuffer@Append{\FancyVerbBreakAnywhereBreak#1}\FV@Break@Scan}}}
7477 % \end{macrocode}
7478 % \end{macro}
7479 %
7480 %
7481 %
7482 % \begin{macro}{\FV@Break@BeforeAfterToken@UTF}
7483 % Due to the way that the flow works, |#1| will sometimes be a single byte and sometimes be a multi-byte UTF-8 code point. As a result, it is vital use use |\detokenize| in the UTF-8 leading byte checks; |\string| would only deal with the first byte. It is also important to keep track of the distinction between |\FV@Break@Next#1| and |\FV@Break@Next{#1}|. In some cases, a multi-byte sequence is being passed on as a single argument, so it must be enclosed in curly braces; in other cases, it is being re-inserted into the scanning stream and curly braces must be avoided lest they be interpreted as part of the original text.
7484 % \begin{macrocode}
7485 \def\FV@Break@BeforeAfterToken@UTF#1{%
7486 \ifcsname FV@U8:\detokenize{#1}\endcsname
7487 \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname
7488 \let\FV@UTF@octets@after\FV@Break@BeforeAfterToken@UTF@i
7489 \else
7490 \let\FV@Break@Next\FV@Break@BeforeAfterToken@UTF@i
7491 \fi
7492 \FV@Break@Next{#1}%
7493 }
7494 \def\FV@Break@BeforeAfterToken@UTF@i#1{%
7495 \ifcsname FV@BreakBefore@Token\detokenize{#1}\endcsname
7496 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@UTF
7497 \else
7498 \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7499 \let\FV@Break@Next\FV@Break@AfterTokenBreak@UTF
7500 \else
7501 \let\FV@Break@Next\FV@Break@BeforeAfterTokenNoBreak@UTF
7502 \fi
7503 \fi
7504 \FV@Break@Next{#1}%
7505 }
7506 \def\FV@Break@BeforeAfterTokenNoBreak@UTF#1{%
7507 \FV@BreakBuffer@Append{#1}%
7508 \def\FV@LastToken{#1}%
7509 \FV@Break@Scan}
7510 \def\FV@Break@BeforeTokenBreak@UTF#1{%
7511 \def\FV@CurrentToken{#1}%
7512 \ifbool{FV@breakbeforeinrun}%
7513 {\ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7514 \ifx\FV@CurrentToken\FV@ActiveSpaceToken
7515 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7516 \else
7517 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak}%
7518 \fi
7519 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF
7520 \def\FV@RescanToken{#1}%
7521 \else
7522 \ifx\FV@CurrentToken\FV@ActiveSpaceToken
7523 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak#1}%
7524 \else
7525 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak#1}%
7526 \fi
7527 \let\FV@Break@Next\FV@Break@Scan
7528 \def\FV@LastToken{#1}%
7529 \fi}%
7530 {\ifx\FV@CurrentToken\FV@LastToken\relax
7531 \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7532 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF
7533 \def\FV@RescanToken{#1}%
7534 \else
7535 \FV@BreakBuffer@Append{#1}%
7536 \let\FV@Break@Next\FV@Break@Scan
7537 \def\FV@LastToken{#1}%
7538 \fi
7539 \else
7540 \ifcsname FV@BreakAfter@Token\detokenize{#1}\endcsname
7541 \ifx\FV@CurrentToken\FV@ActiveSpaceToken
7542 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7543 \else
7544 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak}%
7545 \fi
7546 \let\FV@Break@Next\FV@Break@BeforeTokenBreak@AfterRescan@UTF
7547 \def\FV@RescanToken{#1}%
7548 \else
7549 \ifx\FV@CurrentToken\FV@ActiveSpaceToken
7550 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak#1}%
7551 \else
7552 \FV@BreakBuffer@Append{\FancyVerbBreakBeforeBreak#1}%
7553 \fi
7554 \let\FV@Break@Next\FV@Break@Scan
7555 \def\FV@LastToken{#1}%
7556 \fi
7557 \fi}%
7558 \FV@Break@Next}
7559 \def\FV@Break@BeforeTokenBreak@AfterRescan@UTF{%
7560 \expandafter\FV@Break@AfterTokenBreak@UTF\expandafter{\FV@RescanToken}}
7561 \def\FV@Break@AfterTokenBreak@UTF#1{%
7562 \def\FV@LastToken{#1}%
7563 \@ifnextchar\FV@FVSpaceToken%
7564 {\ifx\FV@LastToken\FV@ActiveSpaceToken
7565 \expandafter\@firstoftwo
7566 \else
7567 \expandafter\@secondoftwo
7568 \fi
7569 {\FV@Break@AfterTokenBreak@UTF@i{#1}}%
7570 {\FV@BreakBuffer@Append{#1}%
7571 \FV@Break@Scan}}%
7572 {\FV@Break@AfterTokenBreak@UTF@i{#1}}}
7573 \def\FV@Break@AfterTokenBreak@UTF@i#1{%
7574 \ifbool{FV@breakafterinrun}%
7575 {\ifx\FV@LastToken\FV@ActiveSpaceToken
7576 \FV@BreakBuffer@Append{#1\FancyVerbSpaceBreak}%
7577 \else
7578 \FV@BreakBuffer@Append{#1\FancyVerbBreakAfterBreak}%
7579 \fi
7580 \let\FV@Break@Next\FV@Break@Scan}%
7581 {\FV@BreakBuffer@Append{#1}%
7582 \expandafter\ifx\csname @let@token\endcsname\bgroup\relax
7583 \let\FV@Break@Next\FV@Break@AfterTokenBreak@Group@UTF
7584 \else
7585 \let\FV@Break@Next\FV@Break@AfterTokenBreak@UTF@ii
7586 \fi}%
7587 \FV@Break@Next}
7588 \def\FV@Break@AfterTokenBreak@UTF@ii#1{%
7589 \ifcsname FV@U8:\detokenize{#1}\endcsname
7590 \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname
7591 \let\FV@UTF@octets@after\FV@Break@AfterTokenBreak@UTF@ii
7592 \else
7593 \def\FV@NextToken{#1}%
7594 \ifx\FV@LastToken\FV@NextToken
7595 \else
7596 \ifx\FV@LastToken\FV@ActiveSpaceToken
7597 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7598 \else
7599 \FV@BreakBuffer@Append{\FancyVerbBreakAfterBreak}%
7600 \fi
7601 \fi
7602 \let\FV@Break@Next\FV@Break@Scan
7603 \fi
7604 \FV@Break@Next#1}
7605 \def\FV@Break@AfterTokenBreak@Group@UTF#1{%
7606 \ifstrempty{#1}%
7607 {\FV@BreakBuffer@Append{{}}%
7608 \@ifnextchar\bgroup
7609 {\ifx\FV@LastToken\FV@ActiveSpaceToken
7610 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7611 \else
7612 \FV@BreakBuffer@Append{\FancyVerbBreakAfterBreak}%
7613 \fi
7614 \FV@Break@Group}%
7615 {\FV@Break@AfterTokenBreak@Group@UTF@i}}%
7616 {\ifx\FV@LastToken\FV@ActiveSpaceToken
7617 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7618 \else
7619 \FV@BreakBuffer@Append{\FancyVerbBreakAfterBreak}%
7620 \fi
7621 \FV@Break@Group{#1}}}
7622 \def\FV@Break@AfterTokenBreak@Group@UTF@i#1{%
7623 \ifcsname FV@U8:\detokenize{#1}\endcsname
7624 \expandafter\let\expandafter\FV@Break@Next\csname FV@U8:\detokenize{#1}\endcsname
7625 \let\FV@UTF@octets@after\FV@Break@AfterTokenBreak@Group@UTF@i
7626 \else
7627 \def\FV@NextToken{#1}%
7628 \ifx\FV@LastToken\FV@NextToken
7629 \else
7630 \ifx\FV@LastToken\FV@ActiveSpaceToken
7631 \FV@BreakBuffer@Append{\FancyVerbSpaceBreak}%
7632 \else
7633 \FV@BreakBuffer@Append{\FancyVerbBreakAfterBreak}%
7634 \fi
7635 \fi
7636 \let\FV@Break@Next\FV@Break@Scan
7637 \fi
7638 \FV@Break@Next#1}
7639 % \end{macrocode}
7640 % \end{macro}
7641 %
7642 %
7643 % End the conditional creation of the pdfTeX |UTF| macros:
7644 % \begin{macrocode}
7645 \fi
7646 % \end{macrocode}
7647 %
7648 %
7649 %
7650 % \paragraph{Line processing before scanning}\hfill\\
7651 %
7652 % \begin{macro}{\FV@makeLineNumber}
7653 % The \pkg{lineno} package is used for formatting wrapped lines and inserting break symbols. We need a version of \pkg{lineno}'s |\makeLineNumber| that is adapted for our purposes. This is adapted directly from the example |\makeLineNumber| that is given in the \pkg{lineno} documentation under the discussion of internal line numbers. The |\FV@SetLineBreakLast| is needed to determine the internal line number of the last segment of the broken line, so that we can disable the right-hand break symbol on this segment. When a right-hand break symbol is in use, a line of code will be processed twice: once to determine the last internal line number, and once to use this information only to insert right-hand break symbols on the appropriate lines. During the second run, |\FV@SetLineBreakLast| is disabled by |\let|ting it to |\relax|.
7654 % \begin{macrocode}
7655 \def\FV@makeLineNumber{%
7656 \hss
7657 \FancyVerbBreakSymbolLeftLogic{\FancyVerbBreakSymbolLeft}%
7658 \hbox to \FV@BreakSymbolSepLeft{\hfill}%
7659 \rlap{\hskip\linewidth
7660 \hbox to \FV@BreakSymbolSepRight{\hfill}%
7661 \FancyVerbBreakSymbolRightLogic{\FancyVerbBreakSymbolRight}%
7662 \FV@SetLineBreakLast
7663 }%
7664 }
7665 % \end{macrocode}
7666 % \end{macro}
7667 %
7668 %
7669 % \begin{macro}{\FV@RaggedRight}
7670 % We need a copy of the default |\raggedright| to ensure that everything works with classes or packages that use a special definition.
7671 % \begin{macrocode}
7672 \def\FV@RaggedRight{%
7673 \let\\\@centercr
7674 \@rightskip\@flushglue\rightskip\@rightskip\leftskip\z@skip\parindent\z@}
7675 % \end{macrocode}
7676 % \end{macro}
7677 %
7678 %
7679 % \begin{macro}{\FV@LineWidth}
7680 % This is the effective line width within a broken line.
7681 % \begin{macrocode}
7682 \newdimen\FV@LineWidth
7683 % \end{macrocode}
7684 % \end{macro}
7685 %
7686 %
7687 % \begin{macro}{\FV@SaveLineBox}
7688 % This is the macro that does most of the work. It was inspired by Marco Daniel's code at \url{http://tex.stackexchange.com/a/112573/10742}.
7689 %
7690 % This macro is invoked when a line is too long. We modify |\FV@LineWidth| to take into account |breakindent| and |breakautoindent|, and insert |\hbox|es to fill the empty space. We also account for |breaksymbolindentleft| and |breaksymbolindentright|, but \emph{only} when there are actually break symbols. The code is placed in a |\parbox|. Break symbols are inserted via \pkg{lineno}'s |internallinenumbers*|, which does internal line numbers without continuity between environments (the |linenumber| counter is automatically reset). The beginning of the line has negative |\hspace| inserted to pull it out to the correct starting position. |\strut|s are used to maintain correct line heights. The |\parbox| is followed by an empty |\hbox| that takes up the space needed for a right-hand break symbol (if any). |\FV@BreakByTokenAnywhereHook| is a hook for using |breakbytokenanywhere| when working with Pygments. Since it is within |internallinenumbers*|, its effects do not escape.
7691 % \begin{macrocode}
7692 \def\FV@SaveLineBox#1{%
7693 \savebox{\FV@LineBox}{%
7694 \advance\FV@LineWidth by -\FV@BreakIndent
7695 \hbox to \FV@BreakIndent{\hfill}%
7696 \ifbool{FV@breakautoindent}%
7697 {\let\FV@LineIndentChars\@empty
7698 \FV@GetLineIndent#1\FV@Sentinel
7699 \savebox{\FV@LineIndentBox}{\FV@LineIndentChars}%
7700 \hbox to \wd\FV@LineIndentBox{\hfill}%
7701 \advance\FV@LineWidth by -\wd\FV@LineIndentBox
7702 \setcounter{FV@TrueTabCounter}{0}}%
7703 {}%
7704 \ifdefempty{\FancyVerbBreakSymbolLeft}{}%
7705 {\hbox to \FV@BreakSymbolIndentLeft{\hfill}%
7706 \advance\FV@LineWidth by -\FV@BreakSymbolIndentLeft}%
7707 \ifdefempty{\FancyVerbBreakSymbolRight}{}%
7708 {\advance\FV@LineWidth by -\FV@BreakSymbolIndentRight}%
7709 \parbox[t]{\FV@LineWidth}{%
7710 \FV@RaggedRight
7711 \leftlinenumbers*
7712 \begin{internallinenumbers*}%
7713 \let\makeLineNumber\FV@makeLineNumber
7714 \noindent\hspace*{-\FV@BreakIndent}%
7715 \ifdefempty{\FancyVerbBreakSymbolLeft}{}{%
7716 \hspace*{-\FV@BreakSymbolIndentLeft}}%
7717 \ifbool{FV@breakautoindent}%
7718 {\hspace*{-\wd\FV@LineIndentBox}}%
7719 {}%
7720 \FV@BreakByTokenAnywhereHook
7721 \strut\FV@InsertBreaks{\FancyVerbFormatText}{#1}\nobreak\strut
7722 \end{internallinenumbers*}
7723 }%
7724 \ifdefempty{\FancyVerbBreakSymbolRight}{}%
7725 {\hbox to \FV@BreakSymbolIndentRight{\hfill}}%
7726 }%
7727 }
7728 \def\FV@BreakByTokenAnywhereHook{}
7729 % \end{macrocode}
7730 % \end{macro}
7731 %
7732 %
7733 % \begin{macro}{\FV@ListProcessLine@Break}
7734 % This macro is based on the original |\FV@ListProcessLine| and follows it as closely as possible. |\FV@LineWidth| is reduced by |\FV@FrameSep| and |\FV@FrameRule| so that text will not overrun frames. This is done conditionally based on which frames are in use. We save the current line in a box, and only do special things if the box is too wide. For uniformity, all text is placed in a |\parbox|, even if it doesn't need to be wrapped.
7735 %
7736 % If a line is too wide, then it is passed to |\FV@SaveLineBox|. If there is no right-hand break symbol, then the saved result in |\FV@LineBox| may be used immediately. If there is a right-hand break symbol, then the line must be processed a second time, so that the right-hand break symbol may be removed from the final segment of the broken line (since it does not continue). During the first use of |\FV@SaveLineBox|, the counter |FancyVerbLineBreakLast| is set to the internal line number of the last segment of the broken line. During the second use of |\FV@SaveLineBox|, we disable this (|\let\FV@SetLineBreakLast\relax|) so that the value of |FancyVerbLineBreakLast| remains fixed and thus may be used to determine when a right-hand break symbol should be inserted.
7737 % \begin{macrocode}
7738 \expandafter\let\expandafter\FV@iffp\csname fp_compare:nNnTF\endcsname
7739 \def\FV@ListProcessLine@Break#1{%
7740 \hbox to \hsize{%
7741 \kern\leftmargin
7742 \hbox to \linewidth{%
7743 \FV@LineWidth\linewidth
7744 \ifx\FV@RightListFrame\relax\else
7745 \advance\FV@LineWidth by -\FV@FrameSep
7746 \advance\FV@LineWidth by -\FV@FrameRule
7747 \fi
7748 \ifx\FV@LeftListFrame\relax\else
7749 \advance\FV@LineWidth by -\FV@FrameSep
7750 \advance\FV@LineWidth by -\FV@FrameRule
7751 \fi
7752 \ifx\FV@Tab\FV@TrueTab
7753 \let\FV@TrueTabSaveWidth\FV@TrueTabSaveWidth@Save
7754 \setcounter{FV@TrueTabCounter}{0}%
7755 \fi
7756 \sbox{\FV@LineBox}{%
7757 \let\FancyVerbBreakStart\relax
7758 \let\FancyVerbBreakStop\relax
7759 \FancyVerbFormatLine{%
7760 %\FancyVerbHighlightLine %<-- Default definition using \rlap breaks breaking
7761 {\FV@ObeyTabs{\FancyVerbFormatText{#1}}}}}%
7762 \ifx\FV@Tab\FV@TrueTab
7763 \let\FV@TrueTabSaveWidth\relax
7764 \fi
7765 \ifnum
7766 \FV@iffp{\the\wd\FV@LineBox}>{\FV@LineWidth}%
7767 {1}% width greater than line
7768 {\FV@iffp{\the\wd\FV@LineBox}<{0}{1}{0}}% width overflows
7769 >0
7770 \relax
7771 \setcounter{FancyVerbLineBreakLast}{0}%
7772 \ifx\FV@Tab\FV@TrueTab
7773 \let\FV@Tab\FV@TrueTab@UseWidth
7774 \setcounter{FV@TrueTabCounter}{0}%
7775 \fi
7776 \FV@SaveLineBox{#1}%
7777 \ifdefempty{\FancyVerbBreakSymbolRight}{}{%
7778 \let\FV@SetLineBreakLast\relax
7779 \setcounter{FV@TrueTabCounter}{0}%
7780 \FV@SaveLineBox{#1}}%
7781 \FV@LeftListNumber
7782 \FV@LeftListFrame
7783 \FV@BGColor@List{%
7784 \FancyVerbFormatLine{%
7785 \FancyVerbHighlightLine{\usebox{\FV@LineBox}}}}%
7786 \FV@RightListFrame
7787 \FV@RightListNumber
7788 \ifx\FV@Tab\FV@TrueTab@UseWidth
7789 \let\FV@Tab\FV@TrueTab
7790 \fi
7791 \else
7792 \let\FancyVerbBreakStart\relax
7793 \let\FancyVerbBreakStop\relax
7794 \FV@LeftListNumber
7795 \FV@LeftListFrame
7796 \FV@BGColor@List{%
7797 \FancyVerbFormatLine{%
7798 \FancyVerbHighlightLine{%
7799 \parbox[t]{\FV@LineWidth}{%
7800 \noindent\strut\FV@ObeyTabs{\FancyVerbFormatText{#1}}\strut}}}}%
7801 \FV@RightListFrame
7802 \FV@RightListNumber
7803 \fi}%
7804 \hss}\FV@bgcoloroverlap\baselineskip\z@\lineskip\z@}
7805 % \end{macrocode}
7806 % \end{macro}
7807 %
7808 %
7809 %
7810 %
7811 % \subsection{Pygments compatibility}
7812 % \label{sec:impl:pygments}
7813 %
7814 % This section makes line breaking compatible with \href{http://pygments.org/}{Pygments}, which is used by several packages including \pkg{minted} and \pkg{pythontex} for syntax highlighting. A few additional line breaking options are also defined for working with Pygments.
7815 %
7816 % \begin{macro}{\FV@BreakBeforePrep@Pygments}
7817 % Pygments converts some characters into macros to ensure that they appear literally. As a result, |breakbefore| and |breakafter| would fail for these characters. This macro checks for the existence of breaking macros for these characters, and creates breaking macros for the corresponding Pygments character macros as necessary.
7818 %
7819 % The argument that the macro receives is the detokenized name of the main Pygments macro, with the trailing space that detokenization produces stripped. All macro names must end with a space, because the breaking algorithm uses detokenization on each token when checking for breaking macros, and this will produce a trailing space.
7820 % \begin{macrocode}
7821 \def\FV@BreakBeforePrep@Pygments#1{%
7822 \ifcsname FV@BreakBefore@Token\@backslashchar\endcsname
7823 \@namedef{FV@BreakBefore@Token#1Zbs }{}%
7824 \fi
7825 \ifcsname FV@BreakBefore@Token\FV@underscorechar\endcsname
7826 \@namedef{FV@BreakBefore@Token#1Zus }{}%
7827 \fi
7828 \ifcsname FV@BreakBefore@Token\@charlb\endcsname
7829 \@namedef{FV@BreakBefore@Token#1Zob }{}%
7830 \fi
7831 \ifcsname FV@BreakBefore@Token\@charrb\endcsname
7832 \@namedef{FV@BreakBefore@Token#1Zcb }{}%
7833 \fi
7834 \ifcsname FV@BreakBefore@Token\detokenize{^}\endcsname
7835 \@namedef{FV@BreakBefore@Token#1Zca }{}%
7836 \fi
7837 \ifcsname FV@BreakBefore@Token\FV@ampchar\endcsname
7838 \@namedef{FV@BreakBefore@Token#1Zam }{}%
7839 \fi
7840 \ifcsname FV@BreakBefore@Token\detokenize{<}\endcsname
7841 \@namedef{FV@BreakBefore@Token#1Zlt }{}%
7842 \fi
7843 \ifcsname FV@BreakBefore@Token\detokenize{>}\endcsname
7844 \@namedef{FV@BreakBefore@Token#1Zgt }{}%
7845 \fi
7846 \ifcsname FV@BreakBefore@Token\FV@hashchar\endcsname
7847 \@namedef{FV@BreakBefore@Token#1Zsh }{}%
7848 \fi
7849 \ifcsname FV@BreakBefore@Token\@percentchar\endcsname
7850 \@namedef{FV@BreakBefore@Token#1Zpc }{}%
7851 \fi
7852 \ifcsname FV@BreakBefore@Token\FV@dollarchar\endcsname
7853 \@namedef{FV@BreakBefore@Token#1Zdl }{}%
7854 \fi
7855 \ifcsname FV@BreakBefore@Token\detokenize{-}\endcsname
7856 \@namedef{FV@BreakBefore@Token#1Zhy }{}%
7857 \fi
7858 \ifcsname FV@BreakBefore@Token\detokenize{'}\endcsname
7859 \@namedef{FV@BreakBefore@Token#1Zsq }{}%
7860 \fi
7861 \ifcsname FV@BreakBefore@Token\detokenize{"}\endcsname
7862 \@namedef{FV@BreakBefore@Token#1Zdq }{}%
7863 \fi
7864 \ifcsname FV@BreakBefore@Token\FV@tildechar\endcsname
7865 \@namedef{FV@BreakBefore@Token#1Zti }{}%
7866 \fi
7867 \ifcsname FV@BreakBefore@Token\detokenize{@}\endcsname
7868 \@namedef{FV@BreakBefore@Token#1Zat }{}%
7869 \fi
7870 \ifcsname FV@BreakBefore@Token\detokenize{[}\endcsname
7871 \@namedef{FV@BreakBefore@Token#1Zlb }{}%
7872 \fi
7873 \ifcsname FV@BreakBefore@Token\detokenize{]}\endcsname
7874 \@namedef{FV@BreakBefore@Token#1Zrb }{}%
7875 \fi
7876 }
7877 % \end{macrocode}
7878 % \end{macro}
7879 %
7880 % \begin{macro}{\FV@BreakAfterPrep@Pygments}
7881 % \begin{macrocode}
7882 \def\FV@BreakAfterPrep@Pygments#1{%
7883 \ifcsname FV@BreakAfter@Token\@backslashchar\endcsname
7884 \@namedef{FV@BreakAfter@Token#1Zbs }{}%
7885 \fi
7886 \ifcsname FV@BreakAfter@Token\FV@underscorechar\endcsname
7887 \@namedef{FV@BreakAfter@Token#1Zus }{}%
7888 \fi
7889 \ifcsname FV@BreakAfter@Token\@charlb\endcsname
7890 \@namedef{FV@BreakAfter@Token#1Zob }{}%
7891 \fi
7892 \ifcsname FV@BreakAfter@Token\@charrb\endcsname
7893 \@namedef{FV@BreakAfter@Token#1Zcb }{}%
7894 \fi
7895 \ifcsname FV@BreakAfter@Token\detokenize{^}\endcsname
7896 \@namedef{FV@BreakAfter@Token#1Zca }{}%
7897 \fi
7898 \ifcsname FV@BreakAfter@Token\FV@ampchar\endcsname
7899 \@namedef{FV@BreakAfter@Token#1Zam }{}%
7900 \fi
7901 \ifcsname FV@BreakAfter@Token\detokenize{<}\endcsname
7902 \@namedef{FV@BreakAfter@Token#1Zlt }{}%
7903 \fi
7904 \ifcsname FV@BreakAfter@Token\detokenize{>}\endcsname
7905 \@namedef{FV@BreakAfter@Token#1Zgt }{}%
7906 \fi
7907 \ifcsname FV@BreakAfter@Token\FV@hashchar\endcsname
7908 \@namedef{FV@BreakAfter@Token#1Zsh }{}%
7909 \fi
7910 \ifcsname FV@BreakAfter@Token\@percentchar\endcsname
7911 \@namedef{FV@BreakAfter@Token#1Zpc }{}%
7912 \fi
7913 \ifcsname FV@BreakAfter@Token\FV@dollarchar\endcsname
7914 \@namedef{FV@BreakAfter@Token#1Zdl }{}%
7915 \fi
7916 \ifcsname FV@BreakAfter@Token\detokenize{-}\endcsname
7917 \@namedef{FV@BreakAfter@Token#1Zhy }{}%
7918 \fi
7919 \ifcsname FV@BreakAfter@Token\detokenize{'}\endcsname
7920 \@namedef{FV@BreakAfter@Token#1Zsq }{}%
7921 \fi
7922 \ifcsname FV@BreakAfter@Token\detokenize{"}\endcsname
7923 \@namedef{FV@BreakAfter@Token#1Zdq }{}%
7924 \fi
7925 \ifcsname FV@BreakAfter@Token\FV@tildechar\endcsname
7926 \@namedef{FV@BreakAfter@Token#1Zti }{}%
7927 \fi
7928 \ifcsname FV@BreakAfter@Token\detokenize{@}\endcsname
7929 \@namedef{FV@BreakAfter@Token#1Zat }{}%
7930 \fi
7931 \ifcsname FV@BreakAfter@Token\detokenize{[}\endcsname
7932 \@namedef{FV@BreakAfter@Token#1Zlb }{}%
7933 \fi
7934 \ifcsname FV@BreakAfter@Token\detokenize{]}\endcsname
7935 \@namedef{FV@BreakAfter@Token#1Zrb }{}%
7936 \fi
7937 }
7938 % \end{macrocode}
7939 % \end{macro}
7940 %
7941 %
7942 % \begin{macro}{breakbytoken}
7943 % When Pygments is used, do not allow breaks within \href{http://pygments.org/docs/tokens/}{Pygments tokens}. So, for example, breaks would not be allowed within a string, but could occur before or after it. This has no affect when Pygments is not in use, and is only intended for \pkg{minted}, \pkg{pythontex}, and similar packages.
7944 % \begin{macrocode}
7945 \newbool{FV@breakbytoken}
7946 \define@booleankey{FV}{breakbytoken}%
7947 {\booltrue{FV@breakbytoken}}%
7948 {\boolfalse{FV@breakbytoken}\boolfalse{FV@breakbytokenanywhere}}
7949 % \end{macrocode}
7950 % \end{macro}
7951 %
7952 % \begin{macro}{breakbytokenanywhere}
7953 % |breakbytoken| prevents breaks \emph{within} tokens. Breaks outside of tokens may still occur at spaces. This option also enables breaks between immediately adjacent tokens that are not separated by spaces. Its definition is tied in with |breakbytoken| so that |breakbytoken| may be used as a check for whether either option is in use; essentially, |breakbytokenanywhere| is treated as a special case of |breakbytoken|.
7954 % \begin{macrocode}
7955 \newbool{FV@breakbytokenanywhere}
7956 \define@booleankey{FV}{breakbytokenanywhere}%
7957 {\booltrue{FV@breakbytokenanywhere}\booltrue{FV@breakbytoken}}%
7958 {\boolfalse{FV@breakbytokenanywhere}\boolfalse{FV@breakbytoken}}
7959 % \end{macrocode}
7960 % \end{macro}
7961 %
7962 % \begin{macro}{\FancyVerbBreakByTokenAnywhereBreak}
7963 % This is the break introduced when |breakbytokenanywhere=true|. Alternatives would be |\discretionary{}{}{}| or |\linebreak[0]|.
7964 % \begin{macrocode}
7965 \def\FancyVerbBreakByTokenAnywhereBreak{\allowbreak{}}
7966 % \end{macrocode}
7967 % \end{macro}
7968 %
7969 %
7970 % \begin{macro}{texcomments}
7971 % |texcomments| retokenizes the contents of Pygments comment tokens using the catcodes in place before the verbatim context.
7972 % \begin{macrocode}
7973 \newbool{FV@texcomments}
7974 \define@booleankey{FV}{texcomments}%
7975 {\booltrue{FV@texcomments}}%
7976 {\boolfalse{FV@texcomments}}
7977 % \end{macrocode}
7978 % \end{macro}
7979 %
7980 %
7981 % \begin{macro}{\VerbatimPygments}
7982 % This is the command that activates Pygments features. It must be invoked before \verb|\begin{Verbatim}|, etc., but inside a |\begingroup...\endgroup| so that its effects do not escape into the rest of the document (for example, within the beginning of an environment). It takes two arguments: The Pygments macro that literally appears (|\PYG| for \pkg{minted} and \pkg{pythontex}), and the Pygments macro that should actually be used (|\PYG| for \pkg{minted}, |\PYG|\meta{style\_name} for \pkg{pythontex}). The two are distinguished because it can be convenient to highlight everything using the same literal macro name, and then |\let| it to appropriate values to change styles, rather than redoing all highlighting to change styles. It modifies |\FV@PygmentsHook|, which is at the beginning of |\FV@FormattingPrep@PreHook|, to make the actual changes at the appropriate time.
7983 % \begin{macrocode}
7984 \def\VerbatimPygments#1#2{%
7985 \FV@AddToHook{\FV@PygmentsHook}{\FV@VerbatimPygments{#1}{#2}}}
7986 % \end{macrocode}
7987 % \end{macro}
7988 %
7989 %
7990 % \begin{macro}{\FV@VerbatimPygments}
7991 % This does all the actual work. Again, |#1| is the Pygments macro that literally appears, and |#2| is the macro that is actually to be used.
7992 %
7993 % The |breakbefore| and |breakafter| hooks are redefined. This requires some trickery to get the detokenized name of the main Pygments macro without the trailing space that detokenization of a macro name produces.
7994 %
7995 % In the non-|breakbytoken| case, |#1| is redefined to use |#2| internally, bringing in |\FancyVerbBreakStart| and |\FancyVerbBreakStop| to allow line breaks.
7996 %
7997 % In the |breakbytoken| cases, an |\hbox| is used to prevent breaks within the macro (breaks could occur at spaces even without |\FancyVerbBreakStart|). The |breakbytokenanywhere| case is similar but a little tricky. |\FV@BreakByTokenAnywhereHook|, which is inside |\FV@SaveLineBox| where line breaking occurs, is used to define |\FV@BreakByTokenAnywhereBreak| so that it will ``do nothing'' the first time it is used and on subsequent invocations become |\FancyVerbBreakByTokenAnywhereBreak|. Because the hook is within the |internallinenumbers*| environment, the redefinition doesn't escape, and the default global definition of |\FV@BreakByTokenAnywhereBreak| as |\relax| is not affected. We don't want the actual break to appear before the first Pygments macro in case it might cause a spurious break after leading whitespace. But we must have breaks \emph{before} Pygments macros because otherwise lookahead would be necessary.
7998 %
7999 % An intermediate variable |\FV@PYG| is defined to avoid problems in case |#1|$=$|#2|. There is also a check for a non-existent |#2| (|\PYG|\meta{style\_name} may not be created until a later compile in the \pkg{pythontex} case); if |#2| does not exist, fall back to |#1|. For the existence check, |\ifx...\relax| must be used instead of |\ifcsname|, because |#2| will be a macro, and will typically be created with |\csname...\endcsname| which will |\let| the macro to |\relax| if it doesn't already exist.
8000 %
8001 % |\FV@PYG@Redefed| is |\let| to the Pygments macro that appears literally (after redefinition), so that it can be detected elsewhere to allow for special processing, such as in |breakautoindent|.
8002 %
8003 % Escape (|esc|) tokens are always retokenized. Comment (|c|) tokens are retokenized if the |texcomments| option is true. Reference for token abbreviations: \url{https://github.com/pygments/pygments/blob/master/pygments/token.py}.
8004 % \begin{macrocode}
8005 \def\FV@VerbatimPygments#1#2{%
8006 \ifx#2\relax
8007 \let\FV@PYG@orig=#1\relax
8008 \else
8009 \let\FV@PYG@orig=#2\relax
8010 \fi
8011 \let#1\FV@PYG
8012 \ifbool{FV@breaklines}%
8013 {\edef\FV@PYG@name{\expandafter\FV@DetokMacro@StripSpace\detokenize{#1}}%
8014 \def\FV@BreakBeforePrep@PygmentsHook{%
8015 \expandafter\FV@BreakBeforePrep@Pygments\expandafter{\FV@PYG@name}}%
8016 \def\FV@BreakAfterPrep@PygmentsHook{%
8017 \expandafter\FV@BreakAfterPrep@Pygments\expandafter{\FV@PYG@name}}%
8018 \ifbool{FV@breakbytoken}%
8019 {\ifbool{FV@breakbytokenanywhere}%
8020 {\def\FV@BreakByTokenAnywhereHook{%
8021 \def\FV@BreakByTokenAnywhereBreak{%
8022 \let\FV@BreakByTokenAnywhereBreak\FancyVerbBreakByTokenAnywhereBreak}}%
8023 \def\FV@PYG@retoked##1##2{%
8024 \FV@BreakByTokenAnywhereBreak
8025 \leavevmode\hbox{\FV@PYG@orig{##1}{##2}}}}%
8026 {\def\FV@PYG@retoked##1##2{%
8027 \leavevmode\hbox{\FV@PYG@orig{##1}{##2}}}}}%
8028 {\def\FV@PYG@retoked##1##2{%
8029 \ifbool{FV@PYG@doretok}%
8030 {\FV@PYG@orig{##1}{##2}}%
8031 {\FV@PYG@orig{##1}{\FancyVerbBreakStart##2\FancyVerbBreakStop}}}}}%
8032 {\let\FV@PYG@retoked\FV@PYG@orig}%
8033 \let\FV@PYG@Redefed\FV@PYG}
8034 \let\FV@BreakByTokenAnywhereBreak\relax
8035 \def\FV@DetokMacro@StripSpace#1 {#1}
8036 \newbool{FV@PYG@doretok}
8037 \begingroup
8038 \catcode`\+=12
8039 \gdef\FV@PYG@setretok#1+{%
8040 \ifx\relax#1\@empty
8041 \else
8042 \ifstrequal{#1}{c}%
8043 {\ifbool{FV@texcomments}{\booltrue{FV@PYG@doretok}}{}}%
8044 {\ifstrequal{#1}{esc}{\booltrue{FV@PYG@doretok}}{}}%
8045 \expandafter\FV@PYG@setretok
8046 \fi}
8047 \endgroup
8048 \def\FV@PYG#1#2{%
8049 \expandafter\FV@PYG@i\expandafter{\detokenize{#1}}{#2}}
8050 \def\FV@PYG@i#1#2{%
8051 \begingroup
8052 \boolfalse{FV@PYG@doretok}%
8053 \FV@PYG@setretok#1+\relax+%
8054 \ifbool{FV@PYG@doretok}%
8055 {\FVExtraDetokenizeVArg{%
8056 \FVExtraRetokenizeVArg{\FV@PYG@retoked{#1}}{\FancyVerbRestoreCodes}}{#2}}%
8057 {\FV@PYG@retoked{#1}{#2}}%
8058 \endgroup}%
8059 % \end{macrocode}
8060 % \end{macro}
8061 %
8062 %
8063 % \iffalse
8064 %</package>
8065 % \fi
8066 %% \Finale
8067 \endinput