summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/docshots/docshots.dtx
blob: 9163bc10a1ff63a834f2fb7d17944e32d416710f (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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
% \iffalse meta-comment
% (The MIT License)
%
% Copyright (c) 2021-2022 Yegor Bugayenko
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the 'Software'), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
% \fi

% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}

% \GetFileInfo{docshots.dtx}
% \DoNotIndex{\endgroup,\begingroup,\let,\else,\s,\n,\r,\\,\1,\fi}

% \iffalse
%<*driver>
\ProvidesFile{docshots.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{docshots}
%<*package>
[2022-11-20 0.3.1 TeX Samples Next to Their PDF Snapshots in DTX]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[tt=false, type1=true]{libertine}
\usepackage{microtype}
\AddToHook{env/verbatim/begin}{\microtypesetup{protrusion=false}}
\usepackage[dtx]{docshots}
\usepackage{href-ul}
\usepackage{xcolor}
\PageIndex
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
	\DocInput{docshots.dtx}
	\PrintChanges
	\PrintIndex
\end{document}
%</driver>
% \fi

% \title{|docshots|: \LaTeX{} Package that Renders \\ \TeX{} Samples Next to Their \\ PDF Snapshots\thanks{The sources are in GitHub at \href{https://github.com/yegor256/docshots}{yegor256/docshots}}}
% \author{Yegor Bugayenko \\ \texttt{yegor256@gmail.com}}
% \date{\filedate, \fileversion}
%
% \maketitle
%
% \textbf{\color{red}NB!}
% This package doesn't work on Windows!
% Also, you must run \TeX{} processor with |--shell-escape| option.
% Also, you must have
% |pdlaftex|,
% \href{https://www.perl.org}{Perl},
% \href{https://www.ghostscript.com}{Ghostscript},
% and \href{https://ctan.org/pkg/pdfcrop}{pdfcrop}
% installed.

% \section{Introduction}
%
% When you want to demonstrate to the readers of your documentation
% how to use certain \TeX{} commands, the best way would be
% to show exactly how the entire document will be rendered in PDF,
% using a subprocess that would render it (via |pdflatex|, for example).
% To \href{https://tex.stackexchange.com/questions/661027}{my best}
% knowledge, there were no packages that would allow
% you do exactly this. That's why I created this simple package.

% For example, this code:
%\iffalse%
%<*verb>%
%\fi%
\begin{verbatim}
\begin{docshot}
\documentclass{article}
\usepackage{xcolor}
\pagestyle{empty}
\begin{document}
  Hello, {\color{orange}\LaTeX}!
\end{document}
\end{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi
% is rendered as such:
% \begin{docshot}
% \documentclass{article}
% \usepackage{xcolor}
% \pagestyle{empty}
% \begin{document}
%   Hello, {\color{orange}\LaTeX}!
% \end{document}
% \end{docshot}

% Here is a more complex example:
% \begin{docshot}
% \documentclass{article}
% \usepackage{tikz}
% \pagestyle{empty}
% \begin{document}
% \begin{tikzpicture}
% \node [circle,draw] (v0) {$v_0$};
% \node [circle,draw=orange,thick,
%   below right of=v0] (v1) {$v_1$};
% \draw [->] (v0) -- (v1);
% \end{tikzpicture}
% \end{document}
% \end{docshot}

% The picture you see on the left side is rendered by a subprocess
% executing |pdflatex| with the |.tex| content taken from the source file.
% After a successful processing of \TeX{} sources, we use
% \href{https://ctan.org/pkg/pdfcrop}{pdfcrop} to trim the document.

% We execute |pdflatex| with |-interaction=batchmode| and |-halt-on-error| options.
% This means that
% \TeX{} processing will stop at the first error. Check your \TeX{} log
% to understand what went wrong.

% When you render a text instead of a single picture, it's recommended to use
% \href{https://ctan.org/pkg/geometry}{geometry} package to change the size
% of the page:
% \begin{docshot}
% \documentclass{article}
% \usepackage[paperwidth=2in,
%   paperheight=2.3in]{geometry}
% \begin{document}
% ``There is no sadder thing than
% a young pessimist, except an old
% pessimist'' --- \emph{Mark Twain}
% \end{document}
% \end{docshot}
% You can also use |\pagestyle{empty}| as was done in previous docshots.

% \section{Package Options}

% \DescribeMacro{pdflatex}
% The default command line tool for turning |.tex| into
% |.pdf| is |pdflatex|. However, you can change that by using |pdflatex| package option,
% for example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass{article}
\usepackage[pdflatex=/usr/local/bin/pdflatex]{docshot}
\begin{document}
\begin{docshot}
Hello, world!
\end{docshot}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{gs}
% The default location of Ghostscript is just |gs|.
% You can change that by using |gs| package option,
% for example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[gs=/usr/bin/ghostscript]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{pdfcrop}
% The default location of |pdfcrop| is just |pdfcrop|.
% You can change that by using |pdfcrop| package option,
% for example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[pdfcrop=/bin/pdfcrop]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{margin}
% When we crop the PDF rendered, we leave a margin around the content. The
% default value may be changed by the package option |margin|:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[margin=10]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{nocrop}
% You may disable cropping with the help of |nocrop| option:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[nocrop]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{hspace}
% The horizontal distance between the image and its verbatim \TeX{} source
% may be configured via |hspace| package option:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[hspace=1em]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{left}
% \DescribeMacro{right}
% The default width of the image may be changed by |left| option, while
% the width of the verbatim \TeX{} source may be modified by |right| option:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[left=2in,right=.5\linewidth]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{dtx}
% If you use this package inside |.dtx| documentation, add |dtx| package option. Thanks
% to this option all comment symbols will be removed from line starts:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[dtx]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{tmpdir}
% The default location of temp files is |_docshots|. You can change this using |tmpdir| option:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[tmpdir=/tmp/foo]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{runs}
% By default, we run |pdflatex| just once for each docshot. You can change this number using the package
% option |runs|. This may be helpful if you need Bib\TeX{} processing, for example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[runs=3]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{small}
% \DescribeMacro{tiny}
% You don't have too much freedom in formatting of verbatim texts, but you can make
% their font a bit smaller using |small| package option. You can also make it very
% small using |tiny| option:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[small]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{log}
% With |log| option you can make us print all possible logs to the main \TeX{} log.
% By default, we don't do this and you won't see the output of |pdflatex| compilation, for example.
% Just use it like this:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage[log]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{inputminted}
% By default, we render the verbatim text using |\VerbatimInput| command. You
% can change that and make us use |\inputminted|
% from \href{https://ctan.org/pkg/minted}{minted} package instead, for example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage{minted}
\setminted[java]{frame=lines,framesep=2mm}
\usepackage[inputminted=java]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{lstinputlisting}
% By default, we render the verbatim text using |\VerbatimInput| command. You
% can change that and make us use |\lstinputlisting| from
% \href{https://ctan.org/pkg/listings}{listings} package instead, for example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\usepackage{listings}
\lstset{basicstyle=\small}
\usepackage[lstinputlisting]{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \section{Fine-tuning Options}

% \DescribeMacro{\docshotOptions}
% By default, we render the verbatim text using |\VerbatimInput| command with
% no options. You can add your options using |\docshotOptions| command:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\docshotOptions{firstline=4}
\begin{docshot}
...
\end{docshot}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% The options will be cleaned up by the first render of a docshot.

% \section{Prerequisites}

% \DescribeMacro{\docshotPrerequisite}
% \changes{0.0.3}{2022/10/14}{The command is added to enable copying of supplementary files into docshots snippets processing directory.}
% If you need some files to be present next to the |.tex| snippet while
% it's rendered by |pdflatex|, you can use |\docshotPrerequisite| with
% a single mandatory argument. The argument is the name of a file you need
% to be copied from current directory to the temporary directory, where
% all snippets are rendered. The command can be used either in the body
% of the document or in the preamble --- it doesn't matter, as long as
% it shows up before the docshot that needs the prerequisite. For example:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass{article}
\usepackage{docshot}
\docshotPrerequisite{duck.jpg}
\begin{document}
\begin{docshot}
  \documentclass{article}
  \usepackage{graphicx}
  \pagestyle{empty}
  \begin{document}
    This is my favorite picture of a duck:
    \includegraphics[width=2in]{duck.jpg}
  \end{document}
\end{docshot}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi

% \DescribeMacro{\docshotAfter}
% If you need something to happen after each |pdflatex| run of a docshot, you may use
% |\docshotAfter| command right before |docshot| environment. For example, you have a bibliography file that
% you want to be visible for all snippets and you want all of them to
% run \href{https://ctan.org/pkg/biber}{biber} in order to process citations:
%\iffalse
%<*verb>
%\fi
\begin{verbatim}
\documentclass{article}
\usepackage{docshot}
\docshotPrerequisite{main.bib}
\begin{document}
\docshotAfter{biber $2}
\begin{docshot}
  \documentclass{acmart}
  \usepackage[natbib=true]{biblatex}
  \addbibresource{main.bib}
  \pagestyle{empty}
  \begin{document}
    You must read the book of \citet{knuth1984}.
    \printbibliography
  \end{document}
\end{docshot}
\end{document}
\end{verbatim}
%\iffalse
%</verb>
%\fi
% The script you specify in the first argument of |\docshotAfter| will get
% three arguments when it runs:
% \begin{description}\setlength\itemsep{0em}
% \item[|\$1|] the cycle of |pdflatex| processing (1, 2, ...),
% \item[|\$2|] the hash of the snippet,
% \item[|\$3|] the name of the |.tex| file.
% \end{description}
% |$2| is basically equals to |$1| with an
% attached |.tex| suffix. |\docshotAfter| applies only to the first |docshot|
% environment that goes after it! You must specify |\docshotAfter| before
% each |docshot| where you want such post-processing to happen.

% \StopEventually{}

% \section{Implementation}
% \changes{0.0.1}{2022/10/09}{Initial version}

% First, we include a few packages:
%    \begin{macrocode}
\RequirePackage{iexec}
\RequirePackage{fancyvrb}
\RequirePackage{xcolor}
\RequirePackage{graphicx}
\RequirePackage{tikz}
%    \end{macrocode}

% Then, we process package options:
% \changes{0.0.4}{2022/10/18}{Package options "lstinputlisting" and "inputminted" introduced to enable printing of verbatim text either via listings or minted packages.}
% \changes{0.0.5}{2022/10/24}{Package option "log" added, which enables detailed logging via exec. By default, there is no logging at all.}
% \changes{0.2.0}{2022/10/26}{New package option "nocrop" added to allow disabling of "pdfcrop" execution.}
%    \begin{macrocode}
\RequirePackage{pgfopts}
\RequirePackage{ifluatex}
\RequirePackage{ifxetex}
\def\docshots@log{}
\pgfkeys{
  /docshots/.cd,
  dtx/.store in=\docshots@dtx,
  log/.code=\def\docshots@log{log},
  nocrop/.code=\def\docshots@nocrop{},
  lstinputlisting/.store in=\docshots@lstinputlisting,
  inputminted/.store in=\docshots@inputminted,
  tmpdir/.store in=\docshots@tmpdir,
  tmpdir/.default=_docshots\ifxetex-xe\else\ifluatex-lua\fi\fi,
  small/.store in=\docshots@small,
  tiny/.store in=\docshots@tiny,
  runs/.store in=\docshots@runs,
  runs/.default=1,
  pdflatex/.store in=\docshots@pdflatex,
  pdflatex/.default=pdflatex,
  gs/.store in=\docshots@gs,
  gs/.default=gs,
  pdfcrop/.store in=\docshots@pdfcrop,
  pdfcrop/.default=pdfcrop,
  margin/.store in=\docshots@margin,
  margin/.default=5,
  hspace/.store in=\docshots@hspace,
  hspace/.default=.05\linewidth,
  left/.store in=\docshots@left,
  left/.default=.3\linewidth,
  right/.store in=\docshots@right,
  right/.default=.55\linewidth,
  tmpdir,pdflatex,gs,pdfcrop,margin,hspace,left,right,runs
}
\ProcessPgfOptions{/docshots}
%    \end{macrocode}

% Then, we print the version of |pdflatex| to \TeX{} log:
%    \begin{macrocode}
\iexec[\docshots@log,quiet]{"\docshots@pdflatex" --version}%
%    \end{macrocode}

% Then, we print the version of \href{https://ctan.org/pkg/pdfcrop}{pdfcrop} to \TeX{} log:
%    \begin{macrocode}
\ifdefined\docshots@nocrop\else%
  \iexec[\docshots@log,quiet]{"\docshots@pdfcrop" --version}%
\fi%
%    \end{macrocode}

% Then, we print the version of |ghostscript| to \TeX{} log:
%    \begin{macrocode}
\iexec[\docshots@log,quiet]{"\docshots@gs" --version}%
%    \end{macrocode}

% Then, we make a directory where all temporary files will be kept:
%    \begin{macrocode}
\iexec[null]{mkdir -p "\docshots@tmpdir/\jobname"}%
%    \end{macrocode}

% \begin{macro}{\docshots@mdfive}
% \changes{0.1.1}{2022/10/26}{New supplementary command added to calculate MD5 sum of a file.}
% Then, we define a command for MD5 hash calculating of a file:
%    \begin{macrocode}
\RequirePackage{pdftexcmds}
\makeatletter\newcommand\docshots@mdfive[1]{\pdf@filemdfivesum{#1}}\makeatother
%    \end{macrocode}
% \end{macro}

% \begin{macro}{docshot}
% Then, we define |docshot| environment:
%    \begin{macrocode}
\makeatletter\newenvironment{docshot}
{\VerbatimEnvironment\begin{VerbatimOut}
  {\docshots@tmpdir/\jobname/verbatim.tex}}
{\end{VerbatimOut}%
%    \end{macrocode}
% If we are in |dtx| mode, leading percent characters must be removed:
% \changes{0.3.1}{2022/11/20}{A bug fixed, now handling of leading percentage symbols is done right.}
%    \begin{macrocode}
  \ifdefined\docshots@dtx%
    \iexec[null]{perl -i -0777pe "s/(\\n|^)\\x{25}\\s?/\\1/g"
      \docshots@tmpdir/\jobname/verbatim.tex}%
  \fi%
%    \end{macrocode}
% We calculate MD5 hashsum of the file content:
%    \begin{macrocode}
  \def\hash{\docshots@mdfive{\docshots@tmpdir/\jobname/verbatim.tex}}%
%    \end{macrocode}
% If the PDF with the required name already exists, we ignore this step.
% Otherwise, we copy |verbatim.tex| into new file and run |pdflatex|:
% \changes{0.1.0}{2022/10/26}{The output is saved to a hash-named file, for better uniqueness of temporary files.}
%    \begin{macrocode}
  \IfFileExists{\docshots@tmpdir/\jobname/\hash.pdf}
    {\message{docshots: Won't render,
      the PDF '\docshots@tmpdir/\jobname/\hash.pdf' already exists^^J}}
    {\iexec[\docshots@log,quiet]{cp "\docshots@tmpdir/\jobname/verbatim.tex"
      "\docshots@tmpdir/\jobname/\hash.tex"}%
    \foreach \n in {1,...,\docshots@runs}{%
      \iexec[\docshots@log,
        stdout=\docshots@tmpdir/\jobname/\hash.stdout,
        exit=\docshots@tmpdir/\jobname/\hash.ret,
        quiet,trace]{cd "\docshots@tmpdir/\jobname" &&
        "\docshots@pdflatex"
        -interaction=errorstopmode
        -halt-on-error
        -shell-escape
        \hash.tex}%
      \ifnum\n=1\else%
        \message{docshots: pdflatex run no.\n^^J}%
      \fi%
      \IfFileExists{\docshots@tmpdir/\jobname/after.sh}
        {\iexec[\docshots@log,quiet]{chmod a+x
          "\docshots@tmpdir/\jobname/after.sh"}
        \iexec[\docshots@log,quiet]{cd "\docshots@tmpdir/\jobname" &&
          ./after.sh \n\space \hash\space \hash.tex}}
        {}}}%
%    \end{macrocode}
% Here we delete |after.sh| which may exist here after the last
% compilation of |pdflatex|:
%    \begin{macrocode}
  \iexec[\docshots@log,quiet]{rm -f "\docshots@tmpdir/\jobname/after.sh"}
%    \end{macrocode}
% If a cropped version of the PDF with the required name already exists, we ignore this step.
% Otherwise, we ask |pdfcrop| to crop the PDF:
%    \begin{macrocode}
  \IfFileExists{\docshots@tmpdir/\jobname/\hash.crop.pdf}
    {\message{docshots: Won't pdfcrop,
      the PDF '\docshots@tmpdir/\jobname/\hash.crop.pdf'
      already exists^^J}}
    {\ifdefined\docshots@nocrop
      \iexec[\docshots@log,quiet]{cp
        "\docshots@tmpdir/\jobname/\hash.pdf"
        "\docshots@tmpdir/\jobname/\hash.crop.pdf"}%
      \else%
      \iexec[\docshots@log,quiet]{"\docshots@pdfcrop"
        --margins 0
        "\docshots@tmpdir/\jobname/\hash.pdf"
        "\docshots@tmpdir/\jobname/\hash.crop.pdf"}%
      \fi}%
%    \end{macrocode}
% We configure |fancyvrb|:
%    \begin{macrocode}
  \fvset{numbers=left,numbersep=3pt}%
  \fvset{frame=leftline,framerule=.2pt,rulecolor=\color{gray}}%
  \fvset{samepage=true}%
  \fvset{baselinestretch=1}%
  \ifdefined\docshots@small%
    \fvset{fontsize=\small}%
  \fi%
  \ifdefined\docshots@tiny%
    \fvset{fontsize=\scriptsize}%
  \fi%
%    \end{macrocode}
% Finally, we render the two-column content:
%    \begin{macrocode}
  \begingroup%
  \par%
  \tikz[baseline=(a.north)]
    \node (a) [draw=gray,inner sep=\docshots@margin,
      line width=.2pt]
    {\includegraphics[width=\docshots@left]
      {\docshots@tmpdir/\jobname/\hash.crop.pdf}};%
  \hspace{\docshots@hspace}%
  \begin{minipage}[t]{\docshots@right}%
    \vspace{0pt}%
    \ifdefined\docshots@lstinputlisting%
      \expandafter\lstinputlisting\expandafter[\docshots@opts]
        {\docshots@tmpdir/\jobname/\hash.tex}%
    \else\ifdefined\docshots@inputminted%
      \expandafter\inputminted\expandafter[\docshots@opts]
        {\docshots@inputminted}
        {\docshots@tmpdir/\jobname/\hash.tex}%
    \else%
      \expandafter\VerbatimInput\expandafter[\docshots@opts]
        {\docshots@tmpdir/\jobname/\hash.tex}%
    \fi\fi%
    \vspace{0pt}%
  \end{minipage}%
  \par%
  \endgroup%
  \docshotOptions{}%
}\makeatother
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\docshotPrerequisite}
% Then, we define |\docshotPrerequisite| command:
%    \begin{macrocode}
\makeatletter\newcommand\docshotPrerequisite[1]{
  \iexec[\docshots@log,quiet]{cp #1 "\docshots@tmpdir/\jobname"}%
  \message{docshots: File '#1' copied to
    '\docshots@tmpdir/\jobname/#1'^^J}%
}\makeatother
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\docshotAfter}
% Then, we define |\docshotAfter| command:
%    \begin{macrocode}
\makeatletter\newcommand\docshotAfter[1]{
  \iexec[\docshots@log,quiet]{/bin/echo -n '\detokenize{#1}'
    > "\docshots@tmpdir/\jobname/after.sh"}%
  \message{docshots: File
    '\docshots@tmpdir/\jobname/after.sh' created^^J}%
}\makeatother
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\docshotOptions}
% Finally, we define |\docshotOptions| command:
% \changes{0.3.0}{2022/11/10}{New command introduced to help specify custom options for verbatim environments.}
%    \begin{macrocode}
\makeatletter
\gdef\docshots@opts{}
\newcommand\docshotOptions[1]{%
  \gdef\docshots@opts{#1}%
}\makeatother
%    \end{macrocode}
% \end{macro}

% \Finale

%\clearpage

%\PrintChanges
%\clearpage
%\PrintIndex