summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/wtref/wtref.sty
blob: 856852d69d9d4c683b277516c69b397638469a63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
%%
%% This is file `wtref.sty'.
%%
%% Copyright 2016-2020 Takuto ASAKURA (wtsnjp)
%%   GitHub:   https://github.com/wtsnjp
%%   Twitter:  @wtsnjp
%%
%% This package is distributed under the MIT License.
%%

% Package declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{wtref}
  [2020/06/15 v0.4.0 A small extension for LaTeX2e's cross-referencing]
\def\wtrf@pkgname{WTRef}
\RequirePackage{xparse,xkeyval}

% Messages
\def\wtrf@info{\PackageInfo\wtrf@pkgname}
\def\wtrf@warn{\PackageWarningNoLine\wtrf@pkgname}
\def\wtrf@error{\PackageError\wtrf@pkgname}

% New ifs and counts
\newif\if@wtrf@use@scope@
\newif\if@wtrf@use@namespace@
\newif\if@wtrf@set@namespace@

% Utilities
\def\wtrf@name@def#1{\expandafter\def\csname #1\endcsname}
\def\wtrf@name@edef#1{\expandafter\edef\csname #1\endcsname}
\def\wtrf@name@use#1{\csname #1\endcsname}
\def\wtrf@if@reftype@not@exists#1{\@ifundefined{wtrf@#1@namespace}}

% Check commands
\def\wtrf@check@counter#1{%
  \@ifundefined{c@#1}{%
    \wtrf@error
      {Undefined LaTeX counter `#1'}
      {Check the spelling or declare new LaTeX counter}%
  }{}}
\def\wtrf@check@command#1{%
  \@ifundefined{#1}{}{% else
    \wtrf@warn{Redefining \expandafter\string\csname #1\endcsname}}}

% Trimming spaces
\@tempcnta=\catcode`\Q
\catcode`\Q=3
\def\wtrf@trim@spaces#1{%
  \romannumeral-`\q\wtrf@trim@trim@\noexpand#1Q Q}
\long\def\wtrf@trim@trim@#1 Q{\wtrf@trim@trim@@#1Q}
\long\def\wtrf@trim@trim@@#1Q#2{#1}
\catcode`\Q=\@tempcnta
\def\wtrf@trim@spaces@noexp#1{%
  \unexpanded\expandafter\expandafter\expandafter
    {\wtrf@trim@spaces{#1}}}
\def\wtrf@trim@spaces@in#1{%
  \edef#1{\expandafter\wtrf@trim@spaces@noexp\expandafter{#1}}}

% Define keys for \newref command
\define@key[wtrf]{ns}{namespace}{%
  \@wtrf@set@namespace@true
  \def\wtrf@namespace@temp{#1}}
\define@key[wtrf]{ns}{nonamespace}[\@empty]{%
  \@wtrf@use@namespace@false}
\define@key[wtrf]{ns}{scope}{%
  \wtrf@check@counter{#1}%
  \@wtrf@use@scope@true
  \def\wtrf@scope@temp{#1}}

% Defining the \newref command
\NewDocumentCommand{\newref}{om}{%
  \let\wtrf@namespace@temp\@empty
  \@wtrf@use@namespace@true
  \@wtrf@set@namespace@false
  \@wtrf@use@scope@false
  \IfNoValueF{#1}{\setkeys[wtrf]{ns}{#1}}%
  \wtrf@@newref{#2}}
\@onlypreamble\newref
\def\wtrf@@newref#1{%
  \@for\wtrf@temp@ref@type:=#1\do{%
    \wtrf@trim@spaces@in\wtrf@temp@ref@type
    \expandafter\wtrf@newref@pre\expandafter{\wtrf@temp@ref@type}}}
\def\wtrf@newref@pre#1{%
  \wtrf@if@reftype@not@exists{#1}{%
    \wtrf@newref{#1}%
  }{% else
    \wtrf@error
      {ref type `#1' already exists}
      {You can declare ref type only once}%
  }}
\def\wtrf@newref#1{%
  % namespace
  \if@wtrf@use@namespace@
    \ifx\wtrf@namespace@temp\@empty
      \@wtrf@set@namespace@false
      \def\wtrf@namespace@temp{#1}%
    \fi
    \wtrf@name@edef{wtrf@#1@namespace}{\wtrf@namespace@temp:}%
  \else
    \wtrf@name@def{wtrf@#1@namespace}{}%
  \fi
  % scope
  \if@wtrf@use@scope@
    \wtrf@name@edef{wtrf@#1@scope@cmd}{the\wtrf@scope@temp}%
    \wtrf@name@def{wtrf@#1@scope}{\csname\wtrf@name@use{wtrf@#1@scope@cmd}\endcsname:}%
  \else
    \wtrf@name@def{wtrf@#1@scope}{}%
  \fi
  % define keys for setting styles
  \define@key[wtrf]{#1-style}{refcmd}{\wtrf@name@def{wtrf@#1@refcmd}####1{##1}}%
  \define@key[wtrf]{#1-style}{last sep}[\wtrf@name@use{wtrf@#1@sep}]{%
    \wtrf@name@def{wtrf@#1@last@sep}{##1}}%
  \define@cmdkeys[wtrf]{#1-style}[wtrf@#1@]{sep, prefix, suffix}%
  % default key settings
  \setkeys[wtrf]{#1-style}{%
    refcmd=\ref{##1},
    sep={,\space}, last sep,
    prefix={}, suffix={}}%
  % the \label-like command
  \wtrf@check@command{#1label}%
  \expandafter\DeclareExpandableDocumentCommand\csname #1label\endcsname{m}{%
    \label{\wtrf@name@use{wtrf@#1@namespace}\wtrf@name@use{wtrf@#1@scope}##1}}%
  % the \ref-like command
  \wtrf@check@command{#1ref}%
  \expandafter\DeclareExpandableDocumentCommand\csname #1ref\endcsname{om}{%
    \if@wtrf@use@scope@
      \IfNoValueTF{##1}
        {\wtrf@name@use{wtrf@#1ref}{\wtrf@name@use{wtrf@#1@scope}}{##2}}
        {\wtrf@name@use{wtrf@#1ref}{##1:}{##2}}%
    \else
      \wtrf@name@use{wtrf@#1ref}{}{##2}%
    \fi}
  % construction
  \wtrf@name@def{wtrf@#1ref}##1##2{%
    \wtrf@name@use{wtrf@#1@prefix}%
    \wtrf@construct##2,\wtrf@qmark\wtrf@nil{##1}{#1}%
    \wtrf@name@use{wtrf@#1@suffix}}
  % reset \wtrf@namespace@temp
  \if@wtrf@set@namespace@\else
    \let\wtrf@namespace@temp\@empty
  \fi}

% The constructer for \ref-like commands
\def\wtrf@qmark{\wtrf@qmark}
\def\wtrf@head#1#2\wtrf@nil{#1}
%% \wtrf@construct<head>,<rest>\wtrf@nil{<scope>}{<ref type>}
\def\wtrf@construct#1,#2\wtrf@nil#3#4{%
  \wtrf@name@use{wtrf@#4@refcmd}{%
    \wtrf@name@use{wtrf@#4@namespace}#3\wtrf@trim@spaces@noexp{#1}}%
  \expandafter\ifx\wtrf@head#2\wtrf@nil\wtrf@qmark\else
    \wtrf@construct@#2,\wtrf@qmark\wtrf@nil{#3}{#4}%
  \fi}
\def\wtrf@construct@#1,#2\wtrf@nil#3#4{%
  \expandafter\ifx\wtrf@head#2\wtrf@nil\wtrf@qmark
    \wtrf@name@use{wtrf@#4@last@sep}%
    \wtrf@name@use{wtrf@#4@refcmd}{%
      \wtrf@name@use{wtrf@#4@namespace}#3\wtrf@trim@spaces@noexp{#1}}%
  \else
    \wtrf@name@use{wtrf@#4@sep}%
    \wtrf@name@use{wtrf@#4@refcmd}{%
      \wtrf@name@use{wtrf@#4@namespace}#3\wtrf@trim@spaces@noexp{#1}}%
    \wtrf@construct@#2,\wtrf@qmark\wtrf@nil{#3}{#4}%
  \fi}

% Defining \setrefstyle command
\NewDocumentCommand{\setrefstyle}{mm}{%
  \@for\wtrf@temp@ref@type:=#1\do{%
    \wtrf@trim@spaces@in\wtrf@temp@ref@type
    \expandafter\wtrf@setrefstyle\expandafter{\wtrf@temp@ref@type}{#2}}}
\def\wtrf@setrefstyle#1#2{%
  \wtrf@if@reftype@not@exists{#1}{%
    \wtrf@error
      {ref type `#1' does not exist}
      {Declare ref type before using it}%
  }{% else
    \setkeys[wtrf]{#1-style}{#2}%
  }}

% All done
\endinput

%% EOF