-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverbatim.tex
207 lines (190 loc) · 6.49 KB
/
verbatim.tex
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
%%%% ============= TEX WIZARDRY ============ %%%%
% boilerplate
\long\def\gobble#1{}
\long\def\firstofone#1{#1}
\def\makeletter#1{\catcode`#1=11\relax}
\def\makeother#1{\catcode`#1=12\relax}
\def\makeactive#1{\catcode`#1=13\relax}
\def\ifnot#1{#1\else\expandafter\expandafter\fi\iffalse\iftrue\fi}
% display-verbatim boxes and lengths
\makeletter{@}
\newbox\tb@x
\def\settowidth#1#2{\setbox\tb@x\hbox{{#2}}#1\wd\tb@x\setbox\tb@x\box\voidb@x}
\makeother{@}
\newdimen\verblen
\newdimen\verbtw
\newdimen\mynegw\settowidth\mynegw{ }
\def\negspace{\hskip-\mynegw\relax}
% display environment
\def\begindisplay{\begingroup\vskip\baselineskip}
\def\enddisplay{\vskip\baselineskip\endgroup}
% safety macros
\def\makepipeother{\makeother{|}}
\def\makepipeactive{\makeactive{|}}
\def\pipe{\bgroup\string|\egroup}
% obey spaces/lines/blank lines
\def\showspaces{%
\begingroup%
\lccode`~=` \relax%
\lowercase{\endgroup\obeyspaces\let~=\ \relax}%
}
\newif\ifblankline
\def\obeyblanklines{%
\begingroup%
\lccode`~=`\^^M\relax%
\lowercase{\endgroup\def~}%
{\endgraf\ifblankline\vskip\baselineskip\fi\blanklinetrue}%
\makeactive{\^^M}%
\everypar{\blanklinefalse}%
}
% define |...| and ||...|| verbatim environments
\newif\ifvisiblespaces
\makepipeactive
\def|{%
\ifmmode%
\char`|\makepipeother%
\else%
\begingroup%
\let\do\makeother\dospecials%
\ifnot\ifvisiblespaces\obeyspaces\fi%
\obeylines%
\expandafter\dopipe%
\fi%
}
\def\dopipe#1{%
\ifx|#1%
\makepipeother\expandafter\displaypipe%
\else%
\inlinepipe#1\relax%
\fi%
}
% |...| environment
\long\def\inlinepipe#1|{\tt#1\endgroup\visiblespacesfalse}
% ||...|| environment
\makepipeother
\long\def\displaypipe#1||{%
\settowidth\verblen{#1}
\verbtw=\hsize
\advance\verbtw by -\verblen
\divide\verbtw by 2
\parindent=\verbtw
\vskip\baselineskip%
\showspaces%
\obeyblanklines%
\tt#1%
\vskip\baselineskip%
\endgroup%
\visiblespacesfalse%
\noindent\firstofone%
}
\makepipeactive
% @|...| and @||...|| for visible spaces
\def\atchar{\char`\@\relax}
\makeactive{@}
\begingroup
\gdef @{\futurelet\next\doat}
\gdef\doat{%
\ifx\^^M\next%
\expandafter\gobble%
\else%
\ifx|\next%
\expandafter\visiblespacestrue%
\else%
\atchar%
\fi%
\fi%
}
\endgroup
% \bverb...\everb and \bverbatim...\everbatim
% as substitutes for |...| and ||...||
\def\bverb{%
\begingroup%
\makepipeother%
\let\do\makeother\dospecials%
\ifnot\ifvisiblespaces\obeyspaces\fi%
\tt%
\negspace\negthinspace\dobverb%
}
\def\bverbs{
\visiblespacestrue%
\begingroup%
\makepipeother%
\let\do\makeother\dospecials%
\ifnot\ifvisiblespaces\obeyspaces\fi%
\tt%
\negspace\dobverbs%
}
\def\bverbatim{%
\begingroup
\makepipeother
\let\do\makeother\dospecials%
\ifnot\ifvisiblespaces\obeyspaces\fi%
\showspaces%
\obeyblanklines%
\par\vskip-\baselineskip\relax%
\begindisplay%
\negspace\negthinspace\tt%
\dobverbatim%
}
\begingroup
\catcode`\!=0
\makeother{\\}
!makeother{ }
!gdef!dobverbs #1\everbs{#1!everb}
!obeyspaces
!gdef!dobverb#1\everb{#1!everb}
!gdef!dobverbatim#1\everbatim{#1!everbatim}
!endgroup
\def\everb{\endgroup\visiblespacesfalse}
\def\everbatim{\enddisplay\endgroup\visiblespacesfalse}
%%%% ========= COMMENTS AND USAGE ========== %%%%
\centerline{\font\large=cmr10 at 18pt \large Plain\TeX\ Verbatim}
\vskip2\baselineskip{}
\centerline{Benjamin T. Shepard}
\vskip1ex{}
\centerline{July 9, 2022}
\vskip2\baselineskip{}
\openup0.4ex{}
An inline verbatim such as |this is \inline| can be made with the delimiters {\tt\pipe...\pipe}.
If visible spaces are desired, use
\bverb @|...|\everb instead, which will produce @|this is \inline|.
A display verbatim such as
||this is \display||
can be made with {\tt\pipe\pipe...\pipe\pipe}.
If visible spaces are desired, use {\tt @\pipe\pipe...\pipe\pipe} instead, which will produce
@||this is \display.||
Every normal \TeX\ character (except {\tt\pipe}) as well as all of the special characters can be used inside this environment, as shown in the following example:
||!$@$#%^&*()-+=\ /.,;'`~th@<is$ ]\bye *{ #a '`1\rho > *\tes<t* \undefined .??["||
Normally, the pipe character {\tt\pipe} can only be used inside the display verbatim {\tt\pipe\pipe...\pipe\pipe}.
However, the macros |\makepipeother| and |\makepipeactive| turn the character {\tt\pipe} into an innocent character and into an active character, respectively.
There is also the macro |\pipe| which expands to {\tt\string\bgroup\string\string\pipe\string\egroup}.
These commands allow for use of the pipe literal {\tt\pipe} in horizontal mode. For example, both
||{\tt th\pipe{}s is a pipe}||
and
\begindisplay
\tt
\string\makepipeother\string{\string\tt\ th\string|s is a pipe\string}\string\makepipeactive
\enddisplay
will display {\tt th\pipe{}s is a pipe}.
Of course, these macros cannot be used inside the {\tt\pipe...\pipe} or {\tt\pipe\pipe...\pipe\pipe} environments.
In contrast, the character {\tt @} is able to be used normally, provided that it is not proceeded by the pipe character; \TeX\ will recognize the sequence {\tt @\pipe} as the beginning of a verbatim environment.
These verbatim environments obey spaces, lines, and blank lines; this allows for code insertion such as
||void dfs(int p){
if(o[p]) return;
o[p]=1;
c[p]=t++;
for(int i=0; i<s[p].size(); ++i){
dfs(s[p][i]);
}
}||
which will compile as expected.
As a backup (in case {\tt\pipe} is needed), the four commands |\bverb...\everb| and |\bverbatim...\everbatim| are provided as substitutes for {\tt\pipe...\pipe} and {\tt\pipe\pipe...\pipe\pipe}. Furthermore, the commands |\bverb| and |\bverbs| gobble the first space of their argument. However, these environments cannot be used with |@|; instead, use |\bverbs...\everbs| and |\bverbatims...\everbatims|. Use this command in front of either environment for visible spaces. For example, the code
||\bverbs this is a \test\everbs||
will display \bverbs this is a \test\everbs.
These commands can also be used to display the {\tt\pipe} character: the code
||\bverb th|s is a pipe\everb||
will display \bverb th|s is a pipe\everb, as expected. When the |\bverb| macro is executed, \TeX\ turns every available character innocent until it scans the exact sequence |\everb|.
\vskip3ex{}
To fix: long lines of code do not wrap correctly.
||Oops, the very long line of code is here, it does not fit into the line width unfortunately.||
\bye