-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfancyvrbref.sty
More file actions
87 lines (75 loc) · 3.6 KB
/
Copy pathfancyvrbref.sty
File metadata and controls
87 lines (75 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
\ProvidesPackage{fancyvrbref}[2025-06-03 v1.2 Referencing lines in fancyvrb/minted]
\NeedsTeXFormat{LaTeX2e}[2020-10-01] % For LaTeX hooks.
\RequirePackage{fancyvrb}
\RequirePackage{etoolbox} % For \patchcmd.
\RequirePackage{aliascnt} % For FancyVerbRefLine counter.
% Add hyperref anchors for each line.
\AddToHook{package/hyperref/after}{%
\appto\FV@ListParameterHook{%
% fancyvrb frame option and fvextra breaklines option set \baselineskip to zero (immediately or after two lines).
% Remember \baselineskip at the beginning of environment to use for \Hy@raisedlink via hook.
\edef\FVR@old@baselineskip{\the\baselineskip}%
\appto\HyperRaiseLinkHook{%
\setlength\HyperRaiseLinkLength\FVR@old@baselineskip%
}
}
\patchcmd\FV@ListProcessLine
{\kern\leftmargin}
{% Copied & modified from hyperref's \phantomsection.
\Hy@MakeCurrentHrefAuto{FancyVerbLine*}% Modified from section*. Can't use FancyVerbLine counter because it resets for every environment and doesn't give globally unique anchor names.
\Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
\kern\leftmargin}
{}{\fail}
\AddToHook{package/fvextra/after}{%
% minted uses fvextra, which overrides \FV@ListProcessLine.
% TODO: Why also need these if \FV@ListProcessLine is overridden?
% Same as above.
\patchcmd\FV@ListProcessLine@Break
{\kern\leftmargin}
{\Hy@MakeCurrentHrefAuto{FancyVerbLine*}%
\Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
\kern\leftmargin}
{}{\fail}
% Same as above.
\patchcmd\FV@ListProcessLine@NoBreak
{\kern\leftmargin}
{\Hy@MakeCurrentHrefAuto{FancyVerbLine*}%
\Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
\kern\leftmargin}
{}{\fail}
}
% Unnecessary, because \autoref gets the FancyVerbLine counter from \Hy@MakeCurrentHrefAuto above and hyperref already defines the name for it.
% \def\FancyVerbRefLineautorefname{line}
}
% Add cleveref support.
\newaliascnt{FancyVerbRefLine}{FancyVerbLine}
\def\theFancyVerbRefLine{\arabic{FancyVerbRefLine}} % No \rmfamily\tiny by default like \theFancyVerbLine.
% Copied from within latex's \refstepcounter.
\def\FVR@currentlabel#1{%
\protected@edef\@currentlabel
{\csname p@#1\expandafter\endcsname\csname the#1\endcsname}}%
% Copied & modified from cleveref's \refstepcounter@noarg.
\def\FVR@cref@currentlabel#1{%
% \cref@old@refstepcounter{#1}% Removed!
\cref@constructprefix{#1}{\cref@result}%
\@ifundefined{cref@#1@alias}%
{\def\@tempa{#1}}%
{\def\@tempa{\csname cref@#1@alias\endcsname}}%
\protected@edef\cref@currentlabel{%
[\@tempa][\arabic{#1}][\cref@result]%
\csname p@#1\endcsname\csname the#1\endcsname}}%
\let\FVR@old@refstepcounter\FV@refstepcounter%
\def\FV@refstepcounter#1{%
\FVR@old@refstepcounter{#1}%
\FVR@currentlabel{FancyVerbRefLine}% Override \@currentlabel from \FVR@old@refstepcounter, such that \theFancyVerbRefLine consistently applies to both \ref and \cref.
\edef\@currentcounter{FancyVerbRefLine}% LaTeX2e format since 2024-11-01 causes hyperref to not redefine \refstepcounter. First aid for cleveref derives \cref@currentlabel from \@currentcounter.
\let\@currentlabelname\@empty% For \nameref to not use some previous name.
\@ifpackageloaded{cleveref}{%
\FVR@cref@currentlabel{FancyVerbRefLine}% Set \cref@currentlabel. Only relevant before LaTeX2e format 2024-11-01, later this gets overridden via \@currentcounter anyway.
}{}%
}
\AddToHook{package/cleveref/after}{%
% Unnecessary.
% \crefname{FancyVerbLine}{line}{lines}
\crefname{FancyVerbRefLine}{line}{lines}
}