Debian Codegraph @

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

raw
1 %% PSTOOL.TEX
2 %
3 % This work may be distributed and/or modified under the
4 % conditions of the LaTeX Project Public License, either
5 % version 1.3c or (at your option) any later version.
6 % The latest version of this license is in:
7 % <http://www.latex-project.org/lppl.txt>
8 %
9 % This work has the LPPL maintenance status `maintained'.
10 %
11 % The Current Maintainer of this work is Will Robertson.
12
13
14 \makeatletter
15 \documentclass{ltxdoc}
16 \usepackage[rm,medium]{titlesec}
17
18 \usepackage{xcolor}
19 \usepackage[colorlinks,linktocpage]{hyperref}
20 \usepackage{expl3,l3regex}
21
22 \usepackage{enumitem}
23 \setlist{noitemsep}
24
25 \usepackage{tocloft,varwidth}
26 \setcounter{tocdepth}{1}
27 \def\tocwidthA{0.45}
28 \def\tocwidthB{0.45}
29 \def\cftpartfont{\scshape}
30 \def\cftsecfont{\small}
31 \cftbeforesecskip=0pt
32 \def\cftpartleader{}
33 \def\cftpartafterpnum{\cftparfillskip}
34 \def\cftsecleader{}
35 \def\cftsecafterpnum{\cftparfillskip}
36
37 \let\pkg\textsf
38 \def\pkgopt#1{\texttt{[#1]}}
39
40 \def\PDF{\textsc{pdf}}
41 \def\PS{\textsc{ps}}
42 \def\DVI{\textsc{dvi}}
43 \def\EPS{\textsc{eps}}
44
45 \usepackage{pstool}
46 \usepackage[T1]{fontenc}
47 \usepackage{microtype}
48 \usepackage{lmodern}
49 \usepackage[sc,osf]{mathpazo}
50 \linespread{1.1}
51 \frenchspacing
52
53 \def\partname{Part}
54
55 \GetFileInfo{pstool.sty}
56 \begin{document}
57 {\addtocontents{toc}{\protect\begin{varwidth}[t]{\tocwidthA\linewidth}}}
58
59 \title{The \pkg{pstool} package}
60 \author{
61 \normalsize Concept by Zebb Prime\\
62 \normalsize Package by Will Robertson\footnote{\texttt{[email protected]}}}
63 \date{\fileversion\qquad\filedate}
64
65 \maketitle
66
67 \begin{abstract}
68 This package defines the \cmd\psfragfig\ user command for including \EPS\ files that use \pkg{psfrag} features in a pdf\/\LaTeX\ document. The command \cmd\pstool\ can be used to define other commands with similar behaviour.
69 \end{abstract}
70
71 \tableofcontents
72
73 \part{User documentation}
74
75 \section{Introduction}
76
77 While directly producing \PDF\ output with pdf\/\LaTeX\ is a great improvement in many ways over the `old method' of \DVI$\to$\PS$\to$\PDF, it loses the ability to interface with a generic PostScript workflow, used to great effect in numerous packages, most notably \pkg{PSTricks} and \pkg{psfrag}.
78
79 Until now, the best way to use these packages while running pdf\/\LaTeX\ has been to use the \pkg{pst-pdf} package, which processes the entire document through a filter, sending the relevant PostScript environments (only) through a single pass of \LaTeX\ producing \DVI$\to$\PS$\to$\PDF. The resulting \PDF\ versions of each graphic are then included into the pdf\/\LaTeX\ document in a subsequent compilation. The \pkg{auto-pst-pdf} package provides a wrapper to perform all of this automatically.
80
81 The disadvantage with this method is that for every document compilation, \emph{every} graphic must be re-processed. The \pkg{pstool} package uses a different approach to allow each graphic to be processed only as needed, speeding up and simplifying the typesetting of the main document.
82
83 At present this package is designed solely as a replacement for \pkg{pst-pdf} in the r\^ole of supporting the \pkg{psfrag} package (which it loads) in pdf\/\LaTeX.
84
85 More flexible usage to provide a complete replacement for \pkg{pst-pdf} (e.g., supporting the \cs{begin\{postscript\}} environment) is not planned at this stage.
86 If you simply need to automatically convert plain \EPS\ files to \PDF, I recommend using the \pkg{epstopdf} package with the \pkgopt{update,prepend} package options (\pkg{epstopdf} and \pkg{pstool} are compatible, but \textbf{only} if \pkg{epstopdf} is loaded first).
87
88 \section{Getting started}
89
90 Processing pdf\/\LaTeX\ documents with \pkg{pstool} requires the `shell escape' feature of pdf\/\TeX\ to be activated.
91 This allows execution of auxiliary commands from within \LaTeX, a feature which is often disabled by default for security reasons.
92 If shell escape is not enabled, a warning will be issued in console output when the package is loaded.
93 Depending how you compile your \LaTeX\ document, shell escape is enabled in different ways.%
94 \footnote{On the command line, use the \texttt{-shell-escape} switch. Otherwise, you're on your own.}
95
96 Load the package as usual; no package options are required by default, but there are a few useful options described later in section~\ref{pkgopt}.
97 Note that you do not need to load \pkg{psfrag} separately.
98 You also do not need to load \pkg{graphicx} separately, but if you do so, ensure that you do \emph{not} include driver information (such as |[pdftex]|); this will play havoc with \pkg{pstool}'s automatic processing stage.
99
100 \section{User commands}
101
102 The low-level generic command provided by this package is
103 \begin{description}
104 \item[\cmd\pstool\ \meta{suffix} \oarg{options} \marg{filename} \marg{input definitions}]
105 \end{description}
106 It converts the graphic \meta{filename}|.eps| to \meta{filename}|.pdf| with \pkg{psfrag} macros in \meta{filename}|.tex| through a unique \DVI$\to$\PS$\to$\PDF\ process for each graphic, using the preamble of the main document.
107 The resulting graphic is then inserted into the document, with \meta{options} consisting of options for \pkg{graphicx} (e.g., \texttt{angle}, \texttt{scale}) or for \pkg{pstool} (as described later in Section~\ref{pkgopt}).
108 Note that these optional arguments take effect in the \emph{processing stage}; if you change the \meta{options}, you must manually re-process the figure.
109 The third argument to \cmd\pstool\ allows arbitrary \meta{input definitions} (such as \cmd\psfrag\ directives) to be inserted before the figure as it is processed.
110
111 By default, \cs{pstool} processes the graphic \meta{filename}|.eps| if \meta{filename}|.pdf| does not already exist, or if the \EPS\ file is \emph{newer} than the \PDF.
112 Additionally, if one or more macro files are associated with the graphic, they are also checked whether they have changed since the \PDF\ was generated.
113 The macro file(s) can be defined per-graphic as for the \cs{psfragfig} command (see below), and/or globally as for the \pkgopt{macro-file=...} package option described in Section~\ref{macrofile}.
114
115 The \cmd\pstool\ command can take an optional |*| or |!| suffix to change its behaviour:
116 \begin{description}
117 \item[\cs{pstool*}] Always process the figure;
118 \item[\cs{pstool!}] Never process the figure.
119 \end{description}
120 The behaviour in all three cases can be overridden globally by the package option \pkgopt{process} as described in section~\ref{process}.
121
122 \subsection{The main \cs{psfragfig} command}
123
124 It is useful to define higher-level commands based on \cmd\pstool\ for including specific types of \EPS\ graphics that take advantage of \pkg{psfrag}. The \pkg{pstool} package defines the following wrapper command \cmd\psfragfig, which also supports the |*| or |!| suffixes described above.
125 \begin{description}
126 \item[{\cmd\psfragfig\ \meta{suffix} \oarg{opts} \marg{filename}}]
127 \end{description}
128 This catch-all macro is designed to support a wide range of graphics naming schemes. It inserts an \EPS\ file named either \meta{filename}|-psfrag.eps| or \meta{filename}|.eps| (in that order of preference), and uses \pkg{psfrag} definitions contained within either \meta{filename}|-psfrag.tex| or \meta{filename}|.tex|.
129 The \cs{psfragfig} command can be used to insert figures produced by the \textsc{Mathematica} package \pkg{MathPSfrag} or the \textsc{Matlab} package \pkg{matlabfrag}. \cmd\psfragfig\ also accepts an optional braced argument:
130 \begin{description}
131 \item[{\cmd\psfragfig~\meta{suffix} \oarg{opts} \marg{filename} \marg{input definitions}}]
132 \end{description}
133 The command behaves as above, but also inserts the arbitrary code \meta{input definitions} into the processing stage; this additional code will usually be used to define new or override existing \pkg{psfrag} commands.
134
135 \section{Package options}
136 \label{pkgopt}
137
138 Package options can be set or overridden at any time with \cmd\pstoolsetup\marg{pstool settings}.
139 As mentioned in the previous section, these options also may be set in the optional argument to \cmd\pstool\ and \cmd\psfragfig, in which case they apply to that figure alone.
140
141 \subsection{Macro file(s)}
142 \label{macrofile}
143
144 As mentioned above, macro files can be used to store commands for processing \pkg{psfrag} graphics.
145 If they change, these macro files can trigger a pre-compilation of the graphics.
146 While usually the macro files will be defined per-graphic (such as \texttt{foo.eps} having a \texttt{foo-psfrag.tex} file), \pkg{pstool} will also load a `master' macro file for each graphic if it exists using the \pkgopt{macro-file=...} option.
147
148 The default is \pkgopt{macro-file=\meta{jobname}\texttt{-pstool.tex}}; if this file does not exist then no macro file is loaded.
149 That is, if your document is called \texttt{thesis.tex}, the master macro file will be loaded in each graphic as \texttt{thesis-pstool.tex}, if it exists.
150
151 This option is useful if you have macro definitions in a single file that are used by multiple graphics.
152 By updating the definitions file, the graphics in the document will be automatically updated.
153 (Note that this file can contain plain \LaTeX\ defintions; the \cs{psfrag} commands can still be located in the per-graphic \texttt{.tex} files.)
154
155 To suppress the loading of a master macro file in all cases, use an empty argument for the package option, as in \pkgopt{macro-file=\char`\{\char`\}}.
156
157
158 \subsection{Forcing/disabling graphics processing}
159 \label{process}
160
161 While the suffixes |*| and |!| can be used to force or disable (respectively) the processing of each individual graphic, sometimes we want to do this on a global level. The following package options override \emph{all} \pkg{pstool} macros:
162 \begin{description}
163 \item[\pkgopt{process=auto}] This is the default mode as described in the previous section, in which graphics without suffixes are only (re-)processed if the \EPS\ file is newer or the \PDF\ file does not exist;
164 \item[\pkgopt{process=all}] Suffixes are ignored and all \cmd\pstool\ graphics are processed;
165 \item[\pkgopt{process=none}] Suffixes are ignored and no \cmd\pstool\ graphics are processed.\footnote{If \pkg{pstool} is loaded in a \LaTeX\ document in \DVI\ mode, this is the option that is used since no external processing is required for these graphics.}
166 \end{description}
167
168
169 \subsection{Cropping graphics}
170 The default option \pkgopt{crop=preview} selects the \pkg{preview} package to crop graphics to the appropriate size for each auxiliary process.
171
172 However, when an inserted label protrudes from the natural bounding box of the figure, or when the original bounding box of the figure is wrong, the \pkg{preview} package will not always produce a good result (with parts of the graphic trimmed off the edge). A robust method to solve this problem is to use the \pkg{pdfcrop} program instead.%
173 \footnote{\pkg{pdfcrop} requires a Perl installation under Windows, freely available from \url{http://www.activestate.com/Products/activeperl/index.plex}} This can be activated in \pkg{pstool} with the \pkgopt{crop=pdfcrop} package option.
174
175 \subsection{Temporary files \& cleanup}
176 Each figure that is processed spawns an auxiliary \LaTeX\ compilation through \DVI$\to$\PS$\to$\PDF. This process is named after the name of the figure with an appended string suffix; the default is \pkgopt{suffix=\{-pstool\}}. Most of these suffixed files are ``temporary'' in that they may be deleted once they are no longer needed.
177
178 As an example, if the figure is called |ex.eps|, the files that are created are |ex-pstool.tex|, |ex-pstool.dvi|, \dots. The \pkgopt{cleanup} package option declares via a list of filename suffixes which temporary files are to be deleted after processing.
179
180 The default is \pkgopt{cleanup=\{.tex,\,.dvi,\,.ps,\,.pdf,\,.log\}}. To delete none of the temporary files, choose \pkgopt{cleanup=\{\}} (useful for debugging).
181 Note that if you want cross-referencing to work correctly for labels in figures, etc., then you must not delete the |.aux| file (see Section~\ref{xref}).
182
183
184 \subsection{Interaction mode of the auxiliary processes}
185 Each graphic echoes the output of its auxiliary process to the console window; unless you are trying to debug errors there is little interest in seeing this information. The behaviour of these auxiliary processes are governed globally by the \pkgopt{mode} package option, which takes the following parameters:
186 \begin{description}
187 \item[\pkgopt{mode=batch}] hide almost all of the \LaTeX\ output (\emph{default});
188 \item[\pkgopt{mode=nonstop}] echo all \LaTeX\ output but continues right past any errors; and
189 \item[\pkgopt{mode=errorstop}] prompt for user input when errors are encountered.
190 \end{description}
191 These three package options correspond to the \LaTeX\ command line options \texttt{-interaction=batchmode}, \texttt{=nonstopmode}, and \texttt{=errorstopmode}, respectively. When \pkgopt{mode=batch} is activated, then \pkg{dvips} is also run in `quiet mode'.
192
193
194 \subsection{Auxiliary processing command line options}
195 The command line options passed to each program of the auxiliary processing can be changed with the following package options:
196 \begin{description}
197 \item[\pkgopt{latex-options~~~= ...}]
198 \item[\pkgopt{dvips-options~~~= ...}]
199 \item[\pkgopt{ps2pdf-options~~= ...}] and,
200 \item[\pkgopt{pdfcrop-options~= ...}] (if applicable).
201 \end{description}
202 For the most part these will be unnecessary, although passing the correct options to |ps2pdf| can sometimes be a little obscure.\footnote{The manual is here: \url{http://pages.cs.wisc.edu/~ghost/doc/cvs/Ps2pdf.htm}} For example, I used the following for generating figures in my thesis:%
203 \par{\centering|ps2pdf-options={-dPDFSETTINGS=/prepress}|\par}
204 This forces the `base fourteen' fonts to be embedded within the individual figure files, without which some printers and \textsc{pdf} viewers have trouble with the textual labels. In fact, from v1.3 of \pkg{pstool}, this option is now the default.
205 Note that subsequent calls to \pkgopt{ps2pdf-options=...} will override the \pkg{pstool} default; use |ps2pdf-options={}| to erase |ps2pdf|'s defaults if necessary.
206
207 At some point in the past, the behaviour of |ps2pdf| has changed under Windows.
208 Previously, options to |ps2pdf| needed to be quoted and use |=| to assign its options.
209 Something about this changed, and it appears the best way to set |ps2pdf| options to use the |#| character instead. Therefore, \pkg{pstool} attempts to be clever and replaces all instances of |=| within a |ps2pdf| option into |#| (under Windows only).
210 No quotes are added.
211 Windows uses can therefore continue to use |=| to set |ps2pdf| options and allow \pkg{pstool} to make the substitution; their documents will still compile correctly on macOS or Linux platforms.
212
213
214 \subsection{Compression of bitmap data}
215 In the conversion using |ps2pdf|, bitmap images are stored using either lossy or lossless compression. The default behaviour for \pkg{pstool} is to force lossless compression, because we believe that to be the most commonly desired use case (you don't want scientific graphics rendered with possible compression artifacts). This behaviour can be adjusted using one of these options:\footnote{Technical details are given in section~\ref{sec:bitmapdetails}.}
216 \begin{description}
217 \item[\pkgopt{bitmap=auto}] :~ Do whatever |ps2pdf| does by default, which seems to be to use lossy compression most, if not all, of the time;
218 \item[\pkgopt{bitmap=lossy}]:~ Bitmap images are compressed like \textsc{jpg}; this is only really suitable for photographs;
219 \item[\pkgopt{bitmap=lossless}] (\emph{default}) : Bitmap images are compressed like \textsc{png}; this is suitable for screenshots and generated data such as a surface plot within Matlab.
220 \end{description}
221 These are just special cases of the \pkgopt{ps2pdf-options=...} option, but using \pkgopt{bitmap=...} is much more convenient since the |ps2pdf| options to effect this behaviour are quite verbose.
222 Note that the |auto| and |lossy| outputs differ in quality; |lossy| is lower quality than |auto| even when the latter uses a lossy compression scheme. Adjusting the quality for these options is only possible with relatively complex Ghostscript options.
223
224
225 \subsection{Cross-referencing}
226 To allow graphics that relied on cross-referencing of equation numbers, bibliographic citations, and so on, \pkg{pstool} attempts to transfer data to and from the |.aux| file for each processed graphic. See Section~\ref{xref} for more details.
227 This feature can be disabled for compatibility or performance reasons using the \pkgopt{crossref=false} option.
228 (Its converse, \pkgopt{crossref=true}, is the default in case you wish to set it explicitly.)
229
230 Enabling or disabling the cross-referencing feature can only be performed in the preamble of the document.
231
232 \section{Miscellaneous details}
233
234 \subsection{Conditional preamble or setup commands}
235
236 It can be necessary to use a slightly different preamble for the main document compared to the auxiliary file used to process each graphic individually.
237 To have preamble material be directed at only one or the other, use the \cs{ifpdf} command (automatically loaded from the \pkg{ifpdf} package) as follows:
238 \begin{list}{}\item{}
239 | \ifpdf|\\
240 | %| \emph{main preamble only}\\
241 | \else|\\
242 | %| \emph{graphics preamble only}\\
243 | \fi|
244 \end{list}
245 For example, when using \pkg{beamer} and showing navigation symbols on each slide, you want to suppress these in the \pkg{pstool}-generated graphics (else they'll show up twice!).
246 In this case, the preamble snippet would look something like:
247 \begin{list}{}\item{}
248 | \ifpdf\else|\\
249 | \setbeamertemplate{navigation symbols}{}|\\
250 | \fi|
251 \end{list}
252 It would be possible to provide specific \pkg{pstool} commands or environments to do this; let me know if the \cs{ifpdf} approach doesn't work for you.
253 For larger amount of preamble material that should be omitted for each graphic, the \cmd{\EndPreamble} command (see next) might also help.
254
255 \subsection{The \cmd\EndPreamble\ command}
256
257 The \pkg{pstool} package scans the beginning of the main document to insert its preamble into each graphic that is converted.
258 This feature hasn't been well-tested and there are certain cases in which it is known to fail.
259 (For example, if \verb|\begin{document}| doesn't appear on a line by itself.)
260 If you need to indicate the end of the preamble manually because this scanning has failed, place the command \cmd\EndPreamble\ where-ever you'd like the preamble in the auxiliary processing to end.
261 This is also handy to bypass anything in the preamble that will never be required for the figures but which will slow down or otherwise conflict with the auxiliary processing.
262
263 \subsection{Cross-referencing}
264 \label{xref}
265
266 \pkg{pstool} supports cross-referencing within graphics.
267 That is, you can use |\ref| and |\cite|, etc., within \pkg{psfrag} commands.
268 In fact, references to page numbers within an external figure should now resolve correctly; e.g., you can use |\thepage| within a \pkg{psfrag} command.
269 (I haven't really tested, but this should allow any package that writes information to the |.aux| file to work correctly.)
270
271 The implementation to achieve this is somewhat convoluted and difficult to extend, but the user interface should work just as you would expect, mostly.
272 The main gotcha to keep in mind is that when cross-referencing is used, the graphics will need multiple compilations to resolve all the cross-references properly.
273 Therefore, I recommend when setting such figures up in your document to use the |\psfragfig*| command, which forces graphics compilation every time, and remove the star only when you're sure the graphic is now correct.
274 Alternatively, don't worry about the resolving of the cross-references until the very end, and then load the package with the \pkgopt{process=all} option.
275
276 As the code for processing data through the |.aux| file can have unwanted interactions with other packages,
277 or just be a little slower, you can disable the cross-referencing feature by loading \pkg{pstool} with the \pkgopt{crossref=false} package option.
278
279 \subsection{A note on file paths}
280
281 The \pkg{pstool} package tries to ensure that you can put image files in subdirectories of the main document and the auxiliary processing will still function correctly. In order to ensure this, the external |pdflatex| compilation uses the |-output-directory| feature of pdf\/\TeX. This command line option is definitely supported on all platforms from TeX~Live~2008 and MiKTeX~2.7 onwards, but earlier distributions may not be supported.
282
283 One problem that \pkg{pstool} does not solve on its own is the inclusion of images that do not exist in subdirectories of the main document. For example, |\pstool{../Figures/myfig}| can not process by default because pdf\/\TeX\ usually does not have permission to write into folders that are higher in the heirarchy than the main document. This can be worked around presently in two different ways: (although maybe only for Mac~OS~X and Linux)
284 \begin{enumerate}
285 \item Give |pdflatex| permission to write anywhere with the command:\\
286 |openout_any=a pdflatex ...|
287 \item Create a symbolic link in the working directory to a point higher in the path: |ln -s ../../PhD ./PhD|, for example, and then refer to the graphics through this symbolic link.
288 \end{enumerate}
289
290 \subsection{Technical details on \texttt{ps2pdf}'s bitmap options}
291 \label{sec:bitmapdetails}
292
293 The \pkgopt{bitmap=auto} \pkg{pstool} option does not set any |ps2pdf| options; use this if you wish to set the following |ps2pdf| options manually.
294
295 For both \pkgopt{bitmap=lossless} (default) and \pkgopt{bitmap=lossy}, the following |ps2pdf| options are set:
296 \begin{verbatim}
297 -dAutoFilterColorImages=false
298 -dAutoFilterGrayImages=false
299 \end{verbatim}
300 Then for lossless image encoding, the following options are set:
301 \begin{verbatim}
302 -dColorImageFilter=/FlateEncode
303 -dGrayImageFilter=/FlateEncode
304 \end{verbatim}
305 Instead for lossly encoding, these are the options used:
306 \begin{verbatim}
307 -dColorImageFilter=/DCTEncode
308 -dGrayImageFilter=/DCTEncode
309 \end{verbatim}
310 If there are more |ps2pdf| options that you frequently use, please let me know and it may be a good idea to add \pkg{pstool} wrappers to make them more convenient.
311
312 {\addtocontents{toc}{\protect\end{varwidth}\protect\hfill}}
313 {\addtocontents{toc}{\protect\begin{varwidth}[t]{\protect\tocwidthB\protect\linewidth}}}
314 \clearpage
315 \part{Implementation}
316
317 \section{Package information}
318 The most recent publicly released version of \pkg{pstool} is available at \textsc{ctan}: \url{http://tug.ctan.org/pkg/pstool/}. Historical and developmental versions are available at GitHub: \url{http://github.com/wspr/pstool/}. While general feedback via email is welcomed, specific bugs or feature requests should be reported through the issue tracker: \url{http://github.com/wspr/pstool/issues}.
319
320 \subsection{Licence}
321
322 This package is freely modifiable and distributable under the terms and conditions of the \LaTeX\ Project Public Licence, version 1.3c or greater (your choice).\footnote{\url{http://www.latex-project.org/lppl.txt}} This work consists of the files |pstool.tex| and the derived files |pstool.sty|, |pstool.ins|, and |pstool.pdf|. This work is maintained by \textsc{Will Robertson}.
323
324 \section{Code}
325
326 Note that the following code is typeset in a non-verbatim manner; indentation is controlled automatically by some hastily written macros (and will sometimes not indent as might be done manually). When in doubt, consult the source directly!
327
328 \parindent=0pt
329
330
331 \ExplSyntaxOn
332
333 \ior_new:N \pprint_doc_ior
334 \seq_new:N \l_pprint_line_seq
335 \tl_new:N \l_pprint_tl
336
337 \int_new:N \g_pprint_indent_int
338 \int_new:N \l_pprint_line_indent_int
339 \dim_new:N \l_pprint_dim
340
341 \errorcontextlines=999
342
343 \tl_set:Nx \g_pprint_comment_tl { \cs_to_str:N \% }
344 \definecolor{commentstrcolor}{rgb}{0,0.7,0}
345 \definecolor{numberscolor}{gray}{0.6}
346
347 \def\linecommentstyle{%
348 \sffamily
349 \color{commentstrcolor}%
350 }
351
352 \cs_new:Nn \pprint_scan_setup:
353 {
354 \char_set_catcode_space:N \
355 % \char_set_catcode_other:N \\
356 % \char_set_catcode_space:N \ % space
357 % \char_set_catcode_letter:N \#
358 % \char_set_catcode_letter:N \$
359 % \char_set_catcode_letter:N \^
360 % \char_set_catcode_letter:N \_
361 % \char_set_catcode_letter:N \&
362 }
363
364 %%%%
365
366 \cs_new:Npn \PrettyPrint #1
367 {
368 \int_new:N \g_pprint_docline_int
369 \ior_open:Nn \pprint_doc_ior {#1}
370
371 \cs_generate_variant:Nn \tl_if_eq:nnTF {x}
372
373 \cs_set_eq:NN \___tl_trim_spaces:n \tl_trim_spaces:n
374 \ior_str_map_inline:Nn \pprint_doc_ior
375 {
376 \int_gincr:N \g_pprint_docline_int
377
378 % avoid space trimming inside \seq_set_split:n following:
379 % (MAJOR SIDE-EFFECTS OF COURSE)
380 \cs_set_eq:NN \tl_trim_spaces:n \use:n
381 \exp_args:NNo \seq_set_split:Nnn \l_pprint_line_seq \g_pprint_comment_tl {##1}
382 \cs_set_eq:NN \tl_trim_spaces:n \___tl_trim_spaces:n
383
384 \seq_pop_left:NNT \l_pprint_line_seq \l_pprint_tl
385 {
386 \tl_if_empty:NTF \l_pprint_tl
387 {
388 \pprint_doc_line_comment:
389 }
390 {
391 \pprint_doc_line_code:
392 }
393 }
394 }
395 }
396
397 \cs_generate_variant:Nn \tl_if_eq:nnTF {o}
398 \cs_new:Nn \pprint_doc_line_comment:
399 {
400 \seq_pop_left:NNTF \l_pprint_line_seq \l_pprint_tl
401 % Comment on a line of its own:
402 {
403 \seq_put_right:No \l_pprint_codelines_seq \l_pprint_tl
404 }
405 % A completely empty line:
406 {
407 \pprint_flush_codelines:
408 \vspace{\baselineskip}
409 }
410 }
411
412 \cs_new:Nn \pprint_rescan_comment:n
413 {
414 \exp_args:Nno \tl_rescan:nn
415 {
416 \pprint_scan_setup:
417 }
418 { #1 }
419 }
420
421 \cs_new:Nn \pprint_typeset_startcomment:
422 {
423 \group_begin:
424 \bfseries
425 \g_pprint_comment_tl
426 \group_end:
427 }
428
429 \cs_generate_variant:Nn \regex_extract_once:nnNTF {no}
430 \cs_generate_variant:Nn \tl_if_in:nnT {xx}
431 \seq_new:N \l_tmp_seq
432
433 \seq_new:N \l_pprint_codelines_seq
434
435 \int_new:N \g_pprint_brace_int
436 \cs_new:Nn \pprint_lbrace:
437 {
438 \peek_meaning_remove:NTF \pprint_rbrace:
439 { \char`\{\char`\} }
440 {
441 \int_gincr:N \g_pprint_brace_int
442 \char`\{\pprint_brace_sub:
443 }
444 }
445 \cs_new:Nn \pprint_rbrace:
446 {
447 \pprint_brace_sub:\char`\}
448 \int_gdecr:N \g_pprint_brace_int
449 }
450 \cs_new:Nn \pprint_brace_sub:
451 {
452 {${}\sb{\sb{ \color{red}\mathbf{\int_use:N \g_pprint_brace_int} }}$}
453 }
454
455
456 \cs_new:Nn \pprint_doc_line_code:
457 {
458 \pprint_flush_codelines:
459
460 \makebox[2em][l]{\tiny\color{numberscolor}\int_use:N \g_pprint_docline_int}
461 \exp_args:NNx \tl_replace_all:Nnn \l_pprint_tl { \cs_to_str:N \{ } { \pprint_lbrace: }
462 \exp_args:NNx \tl_replace_all:Nnn \l_pprint_tl { \cs_to_str:N \} } { \pprint_rbrace: }
463 \makebox{\texttt{\l_pprint_tl}}
464
465 \seq_pop_left:NNT \l_pprint_line_seq \l_pprint_tl
466 {
467 \makebox
468 {%
469 \linecommentstyle
470 \pprint_typeset_startcomment:\space
471 \pprint_rescan_comment:n { \l_pprint_tl }
472 }
473 }
474 \par
475 }
476
477 \cs_new:Nn \pprint_flush_codelines:
478 {
479 \seq_if_empty:NF \l_pprint_codelines_seq
480 {
481 \medskip
482 \seq_map_inline:Nn \l_pprint_codelines_seq
483 {
484 \pprint_rescan_comment:n {##1}
485 \space
486 }
487 \seq_clear:N \l_pprint_codelines_seq
488 \smallskip
489 \par
490 }
491 }
492
493 \ExplSyntaxOff
494
495 \PrettyPrint {pstool.sty}
496
497
498 {\addtocontents{toc}{\protect\end{varwidth}}}
499 \end{document}
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521