Debian Codegraph @

texmf-dist/doc/latex/saveenv/saveenv.tex

raw
1 %%! TEX program = lualatex
2 \ProvidesFile{saveenv.tex} [2022/12/23 v0.0.1 ]
3 \documentclass{l3doc}
4 \EnableCrossrefs
5 \CodelineIndex
6 \RecordChanges
7 \fvset{gobble=0,tabsize=4,frame=single,numbers=left,numbersep=3pt}
8 \usepackage{hyperref}
9 \usepackage{csquotes}
10
11 %\MakeOuterQuote{"}
12 \begin{document}
13 \GetFileInfo{\jobname.tex}
14 \DoNotIndex{\begin,\end,\detokenize,\ExplSyntaxOn}
15
16 \iffalse\ExplSyntaxOff\fi % (fix some syntax highlighting issue)
17
18 \title{\textsf{saveenv} --- Save environment content verbatim
19 \thanks{This file describes version \fileversion, last revised \filedate.}
20 }
21 \author{user202729%
22 %\thanks{E-mail: (not set)}
23 }
24 \date{Released \filedate}
25
26 \maketitle
27
28
29 \let\backtick=`
30 \MakeShortVerb{`} % something wrong with \DefineShortVerb
31
32 \begin{abstract}
33 Allow users to collect environment content verbatim.
34 \end{abstract}
35
36 \section{Motivation}
37
38 This package provides tools to create your own verbatim environments, and works in all values of \cs{endlinechar}.
39
40 \section{Comparison to existing similar packages}
41
42 \begin{itemize}
43 \item \pkg{scontents} package is similar; however it does not allow programmer to programmatically retrieve the macro content, only do a limited number of actions with them (execute, typeset as verbatim code, etc.).
44 \item \pkg{fancyvrb}, \pkg{xsimverb} and \pkg{verbatimcopy} provides some internal macro for defining similar environments, however they're internal and/or too specialized (writes to file etc.).
45 \item \pkg{filecontentsdef} and \pkg{newverbs} provides very similar facilities (\env{filecontentsdefmacro} environment and |\Collectverbenv| command respectively); however it requires |\endlinechar=13| and does not support |\ExplSyntaxOn| environment.
46 \item \pkg{verbatim} provides facilities to define custom verbatim environment that processes the content line-by-line, however the interface is more complicated.
47
48 For comparison, the following code
49
50 \begin{verbatim}
51 \ExplSyntaxOn
52 \makeatletter
53 \cs_generate_variant:Nn \tl_build_gput_right:Nn {No}
54 \NewDocumentEnvironment{}{}{
55 \@bsphack
56
57 \endlinechar=`\^^M ~ \let\do\@makeother \dospecials
58 \@makeother \^^I
59 %\cctab_select:N \c_other_cctab
60 % the above cannot be used, as \verbatim@ relies on
61 % the environment name having letter catcode
62
63 \catcode `\^^M \active
64
65 \tl_build_gbegin:N \__all_data
66 \def\verbatim@processline{
67 \tl_build_gput_right:No \__all_data{\the\verbatim@line ^^J}
68 }
69 \verbatim@
70 }{
71 \tl_build_gend:N \__all_data
72 \str_gset:NV \__all_data \__all_data
73
74 % can do something else with \__all_data here
75
76 \@esphack
77 }
78 \makeatother
79 \ExplSyntaxOff
80 \end{verbatim}
81
82 defines an environment that saves the data similar to \env{saveenv} environment described below (with the overhead of |\tl_build_*| functions),
83 but inside |\ExplSyntaxOn| environment it generates a spurious space at the beginning of the string.
84 \end{itemize}
85
86
87
88 \section{Main environment}
89
90 \DescribeEnv{saveenv}
91 Environment that saves its body.
92
93 For example, the following code
94 \begin{verbatim}
95 \begin{saveenv}{\data}
96 123
97 456
98 \end{saveenv}
99 \end{verbatim}
100 will save the string \texttt{123\meta{newline}456\meta{newline}} globally into |\data|.
101
102 Remark: it is consistent to keep the trailing newline, as
103 \begin{verbatim}
104 \begin{saveenv}{\data}
105 \end{saveenv}
106 \end{verbatim}
107 will make |\data| empty, and
108 \begin{verbatim}
109 \begin{saveenv}{\data}
110
111 \end{saveenv}
112 \end{verbatim}
113 will make |\data| consist of a single \meta{newline}.
114
115 The braces around |{\data}| is optional; however, in the unlikely case if \cs{endlinechar} has the \enquote{letter} catcode, it might be absorbed and gives unexpected result.
116
117 A newline is represented as a token with charcode 10 (|^^J|) and catcode other.
118
119 \textbf{Note} that this is unusual, for comparison \pkg{filecontentsdef} stores it as an active |^^M|.
120
121 In the current implementation, it can be used in functions such as \cs{iow_now:Nn} and newline characters will appear as newline.
122 (the number 10 is hard coded in the implementation of |\iow_now:Nn|,
123 nevertheless I'm not sure if this behavior is guaranteed. Manually replacing them with |\iow_newline:| tokens and |x|-expand the result
124 would be guaranteed to work)
125
126 The assignment is global, and done before the macro \cs{endsaveenv} is executed.
127
128 The data is represented as an \pkg{expl3}-string, that is, a sequence of tokens with catcode 12 (for non-space characters) or 10 (for space character).
129
130 In other words, the token list is equal to its own |\detokenize|.
131
132 \DescribeEnv{saveenvghost}
133 Similar to above; however the content inside is still typesetted/executed, and the Sync\TeX\ information is preserved.
134
135 Note that this environment is implemented by reading the whole file from the beginning, therefore there might be some performance hit for large files and multiple usages of the environment. Use sparingly.
136
137 \DescribeEnv{saveenvkeeplast}
138 Similar to above; however the final newline and the space characters before \texttt{\cs{end}\{\meta{environment name}\}} are preserved.
139
140 For example, the example above with \texttt{saveenv} replaced with \texttt{saveenvkeeplast}
141 will save the string \texttt{123\meta{newline}456\meta{newline}} into |\data| instead.
142
143 \section{Reinsert environments}
144
145 Sometimes it's desirable to execute something (e.g. do some local assignments) after the group ends.
146
147 There are a few options:
148 \begin{itemize}
149 \item use |\aftergroup|,
150 \item close the group, execute the code and open a new one (remember to preserve the value of |\@currenvir|),
151 \item use one of the environments below.
152 \end{itemize}
153
154 \DescribeEnv{saveenvreinsert}
155 Environment that takes two arguments, the \meta{str var} to be set and the code to be put after the group end.
156
157 Usage example: If the following code is executed
158 \begin{verbatim}
159 \begin{saveenvreinsert}{\data}{\myfunction {args etc.}}
160 123
161 456
162 \end{saveenvreinsert}
163 some other content
164 \end{verbatim}
165 the effect would be identical to as if |\data| is set to \texttt{123\meta{newline}456\meta{newline}},
166 then the following code is executed
167 \begin{verbatim}
168 \myfunction {args etc.}some other content
169 \end{verbatim}
170 If the second argument is empty, this environment is identical to the \env{saveenv} environment.
171
172 Note that in the example above |\myfunction| is executed after the group ends,
173 while any code in the second block of the |\NewDocumentEnvironment| definition
174 would be executed before the group ends (such as in the example below), thus any local assignment will not persist.
175
176 \DescribeEnv{saveenvkeeplastreinsert}
177 Same as above, but the trailing whitespaces (after the last newline) are preserved.
178
179 \section{Extending the environments}
180
181 All of the environments are extensible.
182
183 Follow the instruction in \url{https://tex.stackexchange.com/q/14683/250119} to define an environment based on an existing one.
184
185 For example, the following definition
186
187 \begin{verbatim}
188 \ExplSyntaxOn
189 \NewDocumentEnvironment{custom}{}{
190 \saveenv \data
191 }{
192 \endsaveenv
193 \tl_show:N \data
194 }
195 \end{verbatim}
196 will define an environment \env{custom} that prints out the content inside the environment using |\tl_show:N|.
197
198 Note that in this case |\tl_show:N \data| is executed before the group ends.
199
200 \section{Limitation}
201
202 \begin{itemize}
203 \item This package does not process the content line-by-line. Therefore:
204 \begin{itemize}
205 \item The memory consumption might be larger than approaches that process the content line-by-line, in case you only need to do something with the line.
206
207 Nevertheless, in modern computers, the overhead is negligible.
208 \item In case it's desired to typeset the content afterwards, it's difficult to preserve the Sync\TeX\ data.
209
210 (although with Lua\LaTeX\ it's possible, see \pkg{rescansync} package)
211 \end{itemize}
212
213 \item Currently nested environments with the same name are not supported (unlike \pkg{scontents} package).
214
215 \item Note that there must be nothing after the |\begin|\texttt{\{\meta{environment name}\}} or the |\end|\texttt{\{\meta{environment name}\}} line.
216
217 \item Note that |\end|\texttt{\{\meta{environment name}\}} must not be in the middle of any line.
218
219 \item The \cs{endlinechar} must not be tokenized in advance (or if it is, its catcode must be 12/other).
220 This might happen when for example your environment looks ahead for optional argument.
221 See \url{https://tex.stackexchange.com/q/649331/250119} for an example and one way how to fix it.
222
223 Alternatively, you can
224 \begin{itemize}
225 \item manually remove the end line token,
226 \item set |\endlinechar=-1\relax| inside the environment before calling \cs{saveenv}.
227 \end{itemize}
228 The value of |\endlinechar| will automatically be reset after the group of the environment is closed.
229 \end{itemize}
230
231
232
233 \StopEventually{%
234 \PrintChanges
235 \PrintIndex
236 }
237 \Finale
238 \end{document}
239