-
Notifications
You must be signed in to change notification settings - Fork 0
94 lines (75 loc) · 2.33 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
88
89
90
91
92
93
94
\documentclass{article}
%\usepackage{trimspaces}
\usepackage{unravel}
\unravelsetup{max-action=1000, max-input=1000, max-output=200}
\long\def\beginunravel#1\endunravel{\unravel{#1}}
\def\endunravel{}
\makeatletter
\catcode`\Q=3
\newcommand\trim@spaces[1]{%
\romannumeral-`\q\trim@trim@\noexpand#1Q Q%
}
\long\def\trim@trim@#1 Q{\trim@trim@@#1Q}
\long\def\trim@trim@@#1Q#2{#1}
\catcode`\Q=11
\makeatother
\begin{document}
\ttfamily
\makeatletter
\leavevmode
\beginunravel
%\tracingall
\trim@spaces{ a }
%\tracingnone
\endunravel
\makeatother
\end{document}
%% start
\trim@spaces{␣a␣}
\newcommand\trim@spaces[1]{%
\romannumeral-`\q\trim@trim@\noexpand#1Q Q%
}
% expand \trim@spaces, #1 = "␣a␣"
\romannumeral-`\q\trim@trim@\noexpand␣a␣Q Q%
% expand \romannumeral, got a minus sign and an integer denotation `\q
% keep on expanding to find the trailing optional space
% See "texdoc texbytopic", sec. 7.2.2 Denotations: characters and
% sec. 7.2.7 Trailing spaces.
\romannumeral-`\q\trim@trim@@\noexpand␣a␣Q Q
\long\def\trim@trim@#1 Q{\trim@trim@@#1Q}
% expand \trim@trim@, #1 = "\noexpand␣a" (delimiter "␣Q" dropped)
\romannumeral-`\q\trim@trim@@\noexpand␣aQ Q
\long\def\trim@trim@@#1Q#2{#1}
% expand \trim@trim@@, #1 = "\noexpand␣a" (delimiter "Q" dropped), #2 = "Q"
% #2 is undelimited, thus the space right before it is skipped
\romannumeral-`\q\noexpand␣a
% expand \noexpand
\romannumeral-`\q␣a
% "\romannumearl-`\q␣" expands to nothing
% "-`\q" denotes a negative integer "-113" (113 is the character code of q)
% "\romannumeral<negative integer>" expands to nothing
a
%% done
%% start
\trim@spaces{a}
\newcommand\trim@spaces[1]{%
\romannumeral-`\q\trim@trim@\noexpand#1Q Q%
}
% expand \trim@spaces, #1 = "a"
\romannumeral-`\q\trim@trim@\noexpand aQ Q%
% expand \romannumeral
\romannumeral-`\q\trim@trim@@\noexpand aQ Q
\long\def\trim@trim@#1 Q{\trim@trim@@#1Q}
% expand \trim@trim@, #1 = "\noexpand aQ" (delimiter " Q" dropped)
\romannumeral-`\q\trim@trim@@\noexpand aQ
% expand \trim@trim@@, #1 = "\noexpand a", #2 = "Q"
\romannumeral-`\q\noexpand a
% expand \noexpand
%
% If "a" is expandable, "\noexpand a" expands to a temp, internal unexpandable
% token "\notexpanded:a".
% See "texdoc interface3", sec. 24.7 Description of all possible tokens.
\romannumeral-`\q a
% "\romannumearl-`\q" expands to nothing
a
%% done