%\iffalse
%<*copyright>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% tile-graphic.sty package,                             %%
%% Copyright (C) 2020                                    %%
%%   dpstory@uakron.edu                                  %%
%%   dpstory@acrotex.net                                 %%
%%                                                       %%
%% This program can redistributed and/or modified under  %%
%% the terms of the LaTeX Project Public License         %%
%% Distributed from CTAN archives in directory           %%
%% macros/latex/base/lppl.txt; either version 1.2 of the %%
%% License, or (at your option) any later version.       %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%</copyright>
%<package>\NeedsTeXFormat{LaTeX2e}[1997/12/01]
%<package>\ProvidesPackage{tile-graphic}
%<package> [2020/05/27 v1.0.1 Tile a graphic (dps)]
%<*driver>
\documentclass{ltxdoc}
\usepackage[colorlinks,hyperindex=false]{hyperref}
\usepackage{array,calc}
\usepackage{fancyvrb}
\usepackage{graphicx}
%\def\texorpdfstring#1#2{#1}
%\pdfstringdefDisableCommands{\let\\\textbackslash}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\InputIfFileExists{aebdocfmt.def}{\PackageInfo{tile-graphic}{Inputting aebdocfmt.def}}
    {\def\IndexOpt{\DescribeMacro}\def\IndexKey{\DescribeMacro}\let\setupFullwidth\relax
     \PackageInfo{tile-graphic}{aebdocfmt.def cannot be found}}
%\pdfstringdefDisableCommands{\let\\\textbackslash}
%\EnableCrossrefs \CodelineIndex
\RecordChanges
\let\pkg\textsf
\let\opt\texttt
\let\key\texttt
\let\app\textsf
\bgroup\ttfamily
\gdef\brpr#1{\char123\relax#1\char125\relax}\egroup
\let\darg\brpr
\def\ameta#1{\ensuremath{\langle\textsl{\texttt{#1}}\rangle}}
\def\meta#1{\textsl{\texttt{#1}}}
\def\SUB#1{\ensuremath{{}_{\mbox{\scriptsize\ttfamily#1}}}}
\def\cs#1{\texttt{\bslash#1}}
\def\EXCL{!}
\let\tops\texorpdfstring
\DeclareRobustCommand{\tmspace}[3]{%
  \ifmmode\mskip#1#2\else\kern#1#3\fi\relax}
\renewcommand{\,}{\tmspace+\thinmuskip{.1667em}}
\let\thinspace\,
\renewcommand{\!}{\tmspace-\thinmuskip{.1667em}}
\let\negthinspace\!
\renewcommand{\:}{\tmspace+\medmuskip{.2222em}}
\let\medspace\:
\newcommand{\negmedspace}{\tmspace-\medmuskip{.2222em}}
\renewcommand{\;}{\tmspace+\thickmuskip{.2777em}}
\let\thickspace\;
\newcommand{\negthickspace}{\tmspace-\thickmuskip{.2777em}}
\makeatletter
\renewcommand{\paragraph}
    {\@startsection{paragraph}{4}{0pt}{6pt}{-3pt}
    {\normalfont\normalsize\bfseries}}
\renewcommand{\subparagraph}
    {\@startsection{subparagraph}{5}{\parindent}{6pt}{-3pt}{\bfseries}}
\renewenvironment{quote}[1][]
   {\def\@rgi{#1}\ifx\@rgi\@empty
    \let\rghtm\@empty\else\def\rghtm{\rightmargin\leftmargin}\fi
    \list{}{\rghtm} %{\rightmargin\leftmargin}%
    \item\relax}
   {\endlist}
\makeatother
\def\ctg#1{\texttt{create-tg.#1}}
\def\packagesuffix{package}
\def\V{\string|}
\def\picBox#1#2#3{{\fboxsep0pt\fbox{\parbox[t][#2]{#1}{\vfill\centering#3\vfill}}}}
\def\picBoxi#1#2#3{{\fboxrule0pt\fboxsep0pt\fbox{\parbox[t][#2]{#1}{\vfill\centering#3\vfill}}}}
\begin{document}
\def\CMD#1{\textbackslash#1}
  \GetFileInfo{tile-graphic.sty}
  \title{\textsf{tile-graphic}: Break a graphic into tiles}
  \author{D. P. Story\\
    Email: \texttt{dpstory@acrotex.net}}
  \date{processed \today}
\value{secnumdepth}=3
\value{tocdepth}=3
  \maketitle
  \tableofcontents
  \let\Email\texttt
  \DocInput{tile-graphic.dtx}
\IfFileExists{\jobname.ind}{\newpage\setupFullwidth\par\PrintIndex}{\paragraph*{Index} The index goes here.\\Execute
    \texttt{makeindex -s gind.ist -o tile-graphic.ind tile-graphic.idx} on the command line and recompile
    \texttt{tile-graphic.dtx}.}
\IfFileExists{\jobname.gls}{\PrintChanges}{\paragraph*{Change History} The list of changes goes here.\\Execute
    \texttt{makeindex -s gglo.ist -o tile-graphic.gls tile-graphic.glo} on the command line and recompile
    \texttt{tile-graphic.dtx}.}
\end{document}
%</driver>
% \fi
% \MakeShortVerb{|}
% \StopEventually{}
%
% \DoNotIndex{\def,\edef,\gdef,\xdef,\global,\long,\let}
% \DoNotIndex{\expandafter,\string,\the,\ifx,\else,\fi}
% \DoNotIndex{\csname,\endcsname,\relax,\begingroup,\endgroup}
% \DoNotIndex{\DeclareTextCommand,\DeclareTextCompositeCommand}
% \DoNotIndex{\space,\@empty,\special}
% \DoNotIndex{\x,\y,\z}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \section{Documentation}\label{s:docs}
%
% \def\pici{\marginpar{\raggedleft\bgroup\offinterlineskip\parindent0pt
% \vtop{%
% \picBox{20pt}{25pt}{\texttt{01}}\picBox{20pt}{25pt}{\texttt{02}}\picBox{20pt}{25pt}{\texttt{03}}\\%
% \picBox{20pt}{25pt}{\texttt{04}}\picBox{20pt}{25pt}{\texttt{05}}\picBox{20pt}{25pt}{\texttt{06}}\\%
% \picBox{20pt}{25pt}{\texttt{07}}\picBox{20pt}{25pt}{\texttt{08}}\picBox{20pt}{25pt}{\texttt{09}}\\%
% \picBox{20pt}{25pt}{\texttt{10}}\picBox{20pt}{25pt}{\texttt{11}}\picBox{20pt}{25pt}{\texttt{12}}\\[6pt]%
% {\parbox{60pt}{\small\centering Tiled graphic\\($4\times3$)}}%
% }%
% \egroup}}
% \def\picii{\marginpar{\raggedleft\bgroup\offinterlineskip\parindent0pt
% \vtop{%
% \picBox{20pt}{25pt}{\texttt{01}}\\\picBox{20pt}{25pt}{\texttt{02}}\\\picBox{20pt}{25pt}{\texttt{03}}\\%
% \picBox{20pt}{25pt}{\texttt{04}}\\\picBox{20pt}{25pt}{\texttt{05}}\\\picBox{20pt}{25pt}{\texttt{06}}\\%
% \picBoxi{20pt}{25pt}{$\vdots\,\vdots\,\vdots$}\\%
% \picBox{20pt}{25pt}{\texttt{10}}\\\picBox{20pt}{25pt}{\texttt{11}}\\\picBox{20pt}{25pt}{\texttt{12}}\\[6pt]%
% {\parbox{20pt}{\small\centering\strut\kern0pt\hbox to0pt{\hss{Pages}\hss}\\\strut\hbox to0pt{\hss($4\times3$)\hss}}}%
% }%
% \egroup}}
%
% \subsection{A brief description}  The\pici\space document author opens the \ctg{tex} file and specifies three
% arguments of the \cs{setTileParams} command: a number $n$ that corresponds to the number of rows; a number
% $m$ that represents the number of columns; and the path to a graphic. Compiling \ctg{tex}, when the option \opt{wrttofiles} is specified, results
% in the specified graphic being broken down into a series of $n\times m$ \textit{tiled} graphics. The order of the creation
% of the tiles is top to bottom, left to right: The first row is the top most row, and the graphic is tiled across
% the row, from left to right, the next row, is the one just below the top row, and the graphic is tiled across that
% row, from left to right, and so on. Refer to the crude diagram to the left.\medskip
%
%\begin{center}
%\fbox{\begin{minipage}{.67\linewidth}
%\textbf{\textcolor{red}{Warning:}} This package uses the \pkg{shellesc} package, which requires
%the \texttt{--shell-escape} switch. Use this package only if you trust the author of this package.
%\end{minipage}}
%\end{center}
% \subsection{Applications}
%   The \pkg{tile-graphic} package can be used to produce tiled graphics, which can be
%   consumed by the \pkg{dps} and \pkg{acrosort} packages.
%
% \subsection{The \texttt{create-tg.tex} file}
% Because of the extensive use of \cs{ShellEscape}, \ctg{tex} is similar to a
% BAT (batch) file. When you compile \ctg{tex}, one result is \ctg{pdf}; \ctg{pdf} is
% a one page summary titled \textbf{Tile Graphic Report}. In addition to producing a report,
% there is the actual result the report refers to: the production of tiled graphic files (PDFs).
%
% Tiled graphics are obtained by compiling \ctg{tex},
% after setting three parameters; using this package, therefore, is very simple.
% Below is the verbatim listing of the \ctg{tex}, found in the \texttt{examples} folder.
%\begin{Verbatim}[xleftmargin=\leftmargini,codes={\catcode`\%=9},commandchars={!@^}]
% \documentclass{article}
% !makebox[0pt][r]@!rmfamily(1)!enspace^\usepackage[!ameta@options^]{tile-graphic}
% !makebox[0pt][r]@!rmfamily(2)!enspace^\setTileParams[!ameta@ig-opts^]{!ameta@nRows^}{!ameta@nCols^}{!ameta@path^}
% \begin{document}
% !makebox[0pt][r]@!rmfamily(3)!enspace^\tileTheGraphic
% \end\darg{document}
%\end{Verbatim}
% When \texttt{create-tg.tex} is compiled, the DVI (in the case of \LaTeX) or the PDF (in all other cases) produces
% a single page document the title of which is ``\textbf{Tile Graphic Report}.'' The file also produces, depending
% on the options used, various \emph{separate} PDFs consisting of tiled graphics.
%
% \paragraph*{Discussion.} We discuss each of the numbered lines slightly out of order.
%\begin{enumerate}
% \item[(2)] \cs{setTileParams[\ameta{ig-opts}]\darg{\ameta{nRows}}\darg{\ameta{nCols}}\darg{\ameta{path}}}\DescribeMacro\setTileParams\\
% The command and its arguments are placed in the preamble.
% \begin{description}
%   \item[\normalfont\ameta{ig-opts}] This optional argument is passed to the underlying optional argument of the \cs{includegraphics} command.
%     Normally, there is no optional options passed.
%
%   \item[\normalfont\ameta{nRows}] This argument declares the number of rows you want to break the graphic into.
%
%   \item[\normalfont\ameta{nCols}] This argument declares the number of columns you want to break the graphic into.
%
%   \item[\normalfont\ameta{path}]  The path to the graphic. The graphic is any
%       file format supported by the PDF creator. For a
%       \texttt{latex->dvips->\ameta{\upshape ps2pdf\string|distiller}} workflow, the graphic
%       should be an EPS file; in all other workflows, the graphic can be a PDF
%       (or some other supported graphical format). For example, if the graphic
%       is in the \texttt{graphics} folder of the source file, then \ameta{path}
%       might read \texttt{graphics/mygraphic}, where \texttt{mygraphic.pdf} (for example) is
%       in the \texttt{graphics} folder.
% \end{description}
%   With no package options, compiling \ctg{tex} produces a document a single page document
%   with a message, seen in \hyperref[fig:noopts]{Figure~\ref*{fig:noopts}}.
%
%   \begin{figure}[htb]\centering
%   \includegraphics[width=.67\linewidth]{docs/graphics/noopts}
%   \caption{No options}\label{fig:noopts}
%   \end{figure}
% \item[(1)] \cs{usepackage[\ameta{options}]\darg{tile-graphic}}\picii\\
% For the package declaration, the \ameta{options} are described below.
% \begin{description}
%   \item[\opt{wrttofiles}] When \ctg{tex} is compiled with the \opt{wrttofiles} option \emph{only}
%   and with a \ameta{path} argument of \texttt{graphics/mygraphic},
%   $\ameta{nRows}\times\ameta{nCols}$ individual tile PDF files named
%   \begin{quote}
%     \texttt{mygraphic\_01.pdf}, \texttt{mygraphic\_02.pdf}, \dots
%   \end{quote} are created and placed in the \texttt{graphics} folder.
%   If no subfolder is specified, files are placed in the source file's folder. The source file that is compiled
%   (\texttt{create-tg.pdf}) contains the \textbf{Tile Graphic Report}.
%
%   \item[\opt{!wrttofiles}] (Convenience option) This option reverts to the default, no separate tile PDFs are produced.
%
%   \item[\opt{packagefiles}]
%   When this option is specified, $\ameta{nRows}\times\ameta{nCols}$ pages are  produced and ``packaged'' in a separate
%   PDF document named \texttt{\ameta{basename}\_\packagesuffix.pdf}, where \ameta{basename} is the base name of the graphic.
%   See the crude illustration to the left.
%
%   \item[\opt{!packagefiles}] (Convenience option) Reverses the \opt{packagefiles} option,
%   as a result, no package file is created.
%
%   \item[\opt{pdfcreator=\ameta{{\upshape pdflatex\string|lualatex\string|xelatex\string|ps2pdf\string|distiller}}}]\leavevmode\\
%   When the option \opt{wrttofiles} or \opt{packagefiles}
%   is specified, compiling \ctg{tex} produces one or more PDF files. These PDFs are produced
%   by \app{pdflatex}, \app{lualatex}, \app{xelatex}, \app{ps2pdf}, or \app{distiller} by executing a
%   series of \cs{ShellEscape} commands. The PDF creator
%   application used is determined by the value of the \opt{pdfcreator} key. The default is \texttt{pdflatex}.
%
%   Using the \cs{ShellEscape} command is system dependent. The \pkg{tile-graphic} package is set up for
%   the \app{Windows} OS; it uses \textsf{del}, \textsf{copy}, and \textsf{move}. Refer to
%   \hyperref[s:custcmds]{Section~\ref*{s:custcmds}} for more information for modifying these
%   system commands.
%
%   \pkg{tile-graphic} also uses \cs{ShellEscape} to compile certain dynamically created TEX files. There are hooks
%   to modify the compile commands that can be used at your discretion. Again, refer to
%   \hyperref[s:custcmds]{Section~\ref*{s:custcmds}} for more information.
% \end{description}
% \item[(3)] \cs{tileTheGraphic}\DescribeMacro\tileTheGraphic\relax\space is placed in the body of the document. It is this command that does all the work,
% based on the parameters of \cs{setTileParam}.
%\end{enumerate}
%
%\paragraph*{Required packages.} The following package are required: \pkg{shellesc}, \pkg{web},
%\pkg{graphicx}, and \pkg{multido}.
%
%\paragraph*{Try it, you'll like it.} The first thing to do, once the package is installed, is to try the
% compiling the file \ctg{tex}. There are three graphics files, one at the top level of the \texttt{examples}
% folder, one in the \texttt{choo} subfilder, and one in the \texttt{postscript} subfolder. Try both graphics (only one graphic at a time, however). Try all possible combinations
% of the basic options \opt{wrttofiles}, \opt{!wrttofiles}, \opt{packagefiles}, and \opt{!packagefiles}; you can even try
% the various values of \opt{pdfcreator}.
%
%\subsection{Description of the workflow}
% Be aware that there are two instances, or stages, of compiling:
% \begin{quote}
% \begin{enumerate}
%   \item[Stage 1.]\label{Item:one} Compiling \ctg{tex}, usually, this is initiated in some {\LaTeX} editor. At this stage,
%   you can compile with \app{latex.exe}, \app{pdflatex.exe}, \app{lualatex.exe}, or \app{xelatex.exe}.
%   When \DescribeMacro\setTileParams\cs{setTileParams} is expanded, it reads the graphic file,
%   \ameta{path}, using \cs{includegraphics} to determine its size. Therefore, the graphic must
%   be one that is supported by the pdf creator at this point. For example if the graphic is a PDF, yet
%   you use \app{latex.exe} to compile \ctg{tex}, you get an error because \app{latex.exe} does not support
%   PDF inclusion.
%   \item[Stage 2.]\label{Item:two} Compiling of one or more files to produce tiled graphics, this step is initiated
%   by the \cs{ShellEscape} command. The application used to compile at this stage is determined
%   by the \key{pdfcreator} key. When the \opt{packagefiles} option is taken, the \DescribeMacro\setTileParams
%   \cs{setTileParams} is expanded again and the graphic file, \ameta{path}, is included to determine
%   dimension of the graphic. Again, the graphic must be a format supported by the PDF creator as selected
%   by the \key{pdfcreator} key, which may be different from the compiler of the first case above.
% \end{enumerate}
% \end{quote}
% Additional thoughts on  the workflow.
% \begin{itemize}
%   \item \key{pdfcreator=\ameta{\upshape pdflatex\string|lualatex\string|xelatex}}\\[3pt]
%   This is the easiest and simplest case. Usually the PDF creators for the two stages of compiling are the same
%   (refer to Stages~\hyperref[Item:one]{1} and~\hyperref[Item:two]{2}, above). These are direct-to-PDF applications, the workflow
%   is relatively quick, smooth, and automated. All AUX files are are deleted except for the AUX files of the
%   main file \ctg{tex}.
%
%   \item \key{pdfcreator=\ameta{\upshape ps2pdf\string|distiller}}\\[3pt]
%   The graphic file format must be EPS for \hyperref[Item:two]{Stage~2}; however, if you are not
%   using \app{latex.exe} in \hyperref[Item:one]{Stage~1}, the graphic must be one that is supported
%   by the PDF creator used. Thus you may have two copies of the graphic of different formats. You're
%   not going to do that, are you? Usually the PDF creators for the two stages of compiling are the same, that way
%   you need not have two formats for the graphic.
%   \begin{itemize}
%       \item \key{pdfcreator=\ameta{\upshape ps2pdf}}\\[3pt]
%         The workflow is \texttt{latex\,->\,dvips\,->\,ps2pdf}
%       \item \key{pdfcreator=\ameta{\upshape distiller}}\\[3pt]
%           The workflow is \texttt{latex\,->\,dvips\,->\,distiller}.
%           There is a difference between using \app{distiller} and \app{ps2pdf}, in the latter case, the PS
%           file can be a relative path; in the former case, it must be a full (or absolute) path, this is a
%           slight complication. For this bad boy, we provide the \DescribeMacro\fullPathToSource
%           \cs{fullPathToSource}, refer to \hyperref[s:custcmds]{Section~\ref*{s:custcmds}} for more details.
% \end{itemize}
% \end{itemize}
% \subsection{The Configuration File}
% A configuration file, named \marginpar{\raggedleft\small\texttt{tg.cfg}}\texttt{tg.cfg},
% is input at the end of the package. You might make any customizations there, where you
% customize for the pdf creator you always use, or, if you use several, you can make
% customizations for each by using the \cs{ifcase} structure below.
%\begin{Verbatim}[xleftmargin=\leftmargini,codes={\catcode`\%=9},commandchars=!()]
%\ifcase\tg@case\relax
%  \def\tg@pdfcreator@app{pdflatex}
%  !quad!ameta(customizations-for-pdflatex)
%\or
%  \def\tg@pdfcreator@app{lualatex}
%  !quad!ameta(customizations-for-lualatex)
%\or
%  \def\tg@pdfcreator@app{xelatex}
%  !quad!ameta(customizations-for-xelatex)
%\or
%  \def\tg@pdfcreator@app{ps2pdf}
%  !quad!ameta(customizations-for-ps2pdf)
%\or
%  \def\tg@pdfcreator@app{acrodist}
%  !quad!ameta(customizations-for-acrodist)
%\fi
%\end{Verbatim}
%See the file \marginpar{\raggedleft\small\texttt{tg.cfg.txt}}\texttt{tg.cfg.txt} for this structure.
%Rename it to \texttt{tg.cfg} if found to be useful.
% $$
%     \rule[2pt]{.25\linewidth}{.4pt}\, \textbf{End of Documentation}\,\rule[2pt]{.25\linewidth}{.4pt}
% $$
%  \section{The package code}
%
%  Having finished the barest of documentation, we begin the markup of this package.
%  \changes{v1.0}{2020/05/27}{Set version number to v1.0 to publish package}
%  \subsection{Options}
%   We bring in the \pkg{xkeyval} package so we can gather our options using
%   it's commands, rather than the default \pkg{keyval} commands.

%    \begin{macrocode}
\RequirePackage{xkeyval}
%    \end{macrocode}
% \leavevmode\IndexOpt{wrttofiles}\IndexOpt[\protect\EXCL]{!wrttofiles} When \opt{wrttofiles} is specfied,
% $\ameta{nRows}\times\ameta{nCols}$ PDF files are created. When \opt{!wrttofiles} is used, these files
% just mentions are not created.
%    \begin{macrocode}
\newif\if@wrttofiles\@wrttofilesfalse
\DeclareOptionX{wrttofiles}{\@wrttofilestrue}
\DeclareOptionX{!wrttofiles}{\@wrttofilesfalse}
%    \end{macrocode}
%    \leavevmode\IndexOpt{packagefiles} When specified, a single ``package'' PDF is created containing
%    $\ameta{nRos}\times\ameta{nCols}$ pages of tiled graphics
%    Likewise, the option \IndexOpt[\protect\EXCL]{!packagefiles}\opt{!packagefiles} reverses \opt{packagefiles} to return
%    to the default state, files are not packaged.
%    \begin{macrocode}
\newif\if@packagefiles \@packagefilestrue
\DeclareOptionX{packagefiles}{\@packagefilestrue}
\DeclareOptionX{!packagefiles}{\@packagefilesfalse}
%    \end{macrocode}
% \leavevmode\IndexOpt{pdfcreator}\hskip-\marginparsep\texttt{=\ameta{{\upshape pdflatex\string|lualatex\string|xelatex\string|ps2pdf\string|distiller}}}
%    \begin{macrocode}
\define@choicekey*+{tile-graphic.sty}{pdfcreator}[\val\nr]%
  {pdflatex,lualatex,xelatex,ps2pdf,distiller}{%
  \edef\tg@case{\nr}%
  \ifcase\nr\relax
    \def\tg@pdfcreator@app{pdflatex}\or
    \def\tg@pdfcreator@app{lualatex}\or
    \def\tg@pdfcreator@app{xelatex}\or
    \def\tg@pdfcreator@app{ps2pdf}\or
    \def\tg@pdfcreator@app{acrodist}\fi
}{\PackageWarning{tile-graphics}%
  {Bad choice for pdfcreator, permissible values\MessageBreak
  are pdflatex, lualatex, xelatex, ps2pdf, and\MessageBreak
  distiller.\MessageBreak Using the default pdflatex}}
\def\tg@pdfcreator@app{pdflatex}\def\tg@case{0}
%    \end{macrocode}
%    Process the options
%    \begin{macrocode}
\ProcessOptionsX\relax
\edef\tg@restoreCats{%
  \catcode`\noexpand\"=\the\catcode`\"\relax
  \catcode`\noexpand\,=\the\catcode`\,\relax
  \catcode`\noexpand\_=\the\catcode`\_\relax
}
\@makeother\"\@makeother\,\@makeother\_
%    \end{macrocode}
% \subsection{Required Packages}
% The \pkg{web} package is use to set the page dimensions, it also brings in \pkg{hyperref}.
% We use \pkg{graphicx} package to have access to \cs{includegraphics}. The \cs{multido} package
% is used to work across the face of the graphic by row, top to bottom, to clip off little bounding boxes of the graphic.
% \pkg{shellesc} is required for the \opt{wrttofiles} option.
%    \begin{macrocode}
\RequirePackage{shellesc}
\RequirePackage{web}
\RequirePackage{graphicx}
\RequirePackage{multido}
%    \end{macrocode}
%    \subsection{Registers and other preliminaries}
%    We declare a new if, \DescribeMacro\if@wrtorpkg\cs{if@wrtorpkg}, which is true if either \cs{if@wrttofiles}
%    or \cs{if@packagefiles} is true. This is a convenience to the coding.
%    \begin{macrocode}
\newif\if@wrtorpkg \@wrtorpkgfalse
\if@wrttofiles
  \@wrtorpkgtrue
\else
  \if@packagefiles
    \@wrtorpkgtrue
  \fi
\fi
\newif\iftg@direct \tg@directtrue
\newif\iftgfolder  \tgfolderfalse
%    \end{macrocode}
%      \leavevmode\DescribeMacro\iftg@direct
%      \cs{iftg@direct} is false if \key{pdfcreator=\ameta{ps2pdf\string|distiller}}.
%    \begin{macrocode}
\ifnum\tg@case>\tw@\relax\tg@directfalse\fi
%    \end{macrocode}
%    A few comments on the \cs{ifpassthruTG} switch. This package performs some tricks.
%    Initially, \cs{ifpassthruTG} is true, and certain portions of the code are executed. When \texttt{create-tg.tex}
%    is compiled with \cs{passthruTGtrue}, it writes to the current folder the file \texttt{package-data.cut} containing the single command \cs{passthruTGfalse}.
%    During this compile, \texttt{create-tg.tex} makes a copy of itself named \texttt{\ameta{basename}\_\packagesuffix.tex}.
%    It is this tile that is compiled by \cs{ShellEscape}, and when compiled, reads \texttt{package-data.cut}, since it now exists,
%    and \cs{ifpassthruTG} is set to false, at which point a different set of code lines are executed.
%    \begin{macrocode}
\newif\ifpassthruTG \passthruTGtrue
%    \end{macrocode}
%    Some miscellaneous lengths, boxes, and utility commands.
%    \begin{macrocode}
\newdimen\tg@dima
\newdimen\tg@dimb
\newbox\tg@box
\newcount\tg@Cnt
\def\tg@rmslash#1/{\def\tgInFolder{#1}}%
\let\tgInFolder\@empty
%    \end{macrocode}
%\goodbreak
% \subsection{Some customization commands}\label{s:custcmds}
% \paragraph*{System commands.} These are \DescribeMacro\sysdel\cs{sysdel},
% \DescribeMacro\syscopy\cs{syscopy}, and \DescribeMacro\sysmove\cs{sysmove}. The
% default is to use \app{Windows} OS commands. Changes for UNIX/Linux or MacOS are easy enough
% by declaring these three commands in the preamble (or in \texttt{tg.cfg}), their arguments are the names of
% the corresponding system commands for deleting, copying, and moving files.
%    \begin{macrocode}
\def\sysdel#1{\def\tg@sysdel{#1\space}}
\sysdel{del}
\def\syscopy#1{\def\tg@syscopy{#1\space}}
\syscopy{copy}
\def\sysmove#1{\def\tg@sysmove{#1\space}}
\sysmove{move}
%    \end{macrocode}
%    \paragraph*{Changing the switches in Stage 2.} Here is a link to the \hyperref[Item:two]{Stage~2} reference.
%    There are two TEX files compiled: (1) the package file, and (2)
%    the individual tiles graphic files. We provide a hook to modify the command line
%    switches of the \texttt{pdfcreator}. \DescribeMacro\pkgappArgs\cs{pkgappArgs} is the hook
%    into the creation of the package file. Use \texttt{\#1} to represent the \texttt{pdfcreator}
%    application. For example, \verb~\pkappArgs{#1 -halt-on-error}~ produces a command line
%    of \begin{quote}\app{pdflatex -halt-on-error  --shell-escape \ameta{targeted-package-file.tex}}\end{quote}
%    For a \texttt{pdfcreator} of \app{pdflatex}.
%    The \texttt{--shell-escape} is automatically included.
%    \begin{macrocode}
\def\pkgappArgs#1{\def\pkg@ppArgs##1{#1}}
\pkgappArgs{#1}
%    \end{macrocode}
%    We also separately compile each of the separate tile graphics. \DescribeMacro\tileappArgs
%    \cs{tileappArgs} is similar to \cs{pkgappArgs}. If we declare \verb~\tileappArgs{#1 -halt-on-error}~, then the command line
%    becomes,
%    \begin{quote}\app{pdflatex -halt-on-error \ameta{targeted-tile-graphic.tex}}\end{quote}
%    Here, \texttt{--shell-escape} is not automatically included as it is not needed for this step.
%    \begin{macrocode}
\def\tileappArgs#1{\def\tile@ppArgs##1{#1}}
\tileappArgs{#1}
%    \end{macrocode}
%
%    \subparagraph*{Some unverified tricks.} What if your system does not recognize \app{pdflatex},
%    \app{lualatex}, and so on; perhaps they are know by different names. You can use these hooks
%    to fix that. For example,
%     \begin{quote}\ttfamily
%       \string\pkgappArgs\darg{\ameta{alt-pdflatex}.exe \ameta{other-switches}}
%     \end{quote}
%     Or, perhaps the path to this application is not on the system paths, hence \app{pdflatex}
%     is not recognized, in this case, try
%     \begin{quote}\ttfamily
%       \string\pkgappArgs\darg{\ameta{path-to}/\ameta{alt-pdflatex}.exe \ameta{other-switches}}
%     \end{quote}
%     that ``should'' work.\par\medskip\noindent
%    When \key{pdfcreator=\ameta{ps2pdf\string|distiller}}, we provide two hooks for this workflow:
%    \DescribeMacro\latexappArgs\cs{latexappArgs} and \DescribeMacro\dvipsappArgs\cs{dvipsappArgs}. These function
%    similar to the ones described above; for example, \verb~\dvipsappArgs{#1 -q*}~ passes the \texttt{-q*} switch
%    (quiet mode) to \app{dvips}. No separate commands for a package compile versus a tile compile, as seen above,
%    are defined, though that could change.
%    \begin{macrocode}
\def\latexappArgs#1{\def\latex@ppArgs##1{#1}}
\latexappArgs{#1}
\def\dvipsappArgs#1{\def\dvips@ppArgs##1{#1}}
\dvipsappArgs{#1}
%    \end{macrocode}
%    \leavevmode\DescribeMacro\definePath\hskip-\marginparsep\texttt{\darg{\ameta{\cs{cmd}}}\darg{\ameta{path}}}
%    A command taken from \pkg{eforms} that normalizes the argument before
%    defining \cs{cmd}.
%    \begin{macrocode}
\providecommand{\definePath}[1]{\def\ef@ctrlName{#1}%
    \hyper@normalise\ef@definePath}
\def\ef@definePath#1{\expandafter\xdef\ef@ctrlName{#1}}
%    \end{macrocode}
%    \subparagraph*{Support for \key{pdfcreator=distiller}.}\leavevmode\medskip\par\noindent
%    \leavevmode\DescribeMacro\fullPathToSource\hskip-\marginparsep\texttt{\darg{\ameta{path}}}
%    Set the full path to the source file (\texttt{create-tg.tex}). This command is only needed
%    when \opt{pdfcreator=distiller}.
%    \begin{macrocode}
\def\fullPathToSource{\definePath{\tg@fullPathToSource}}
\let\tg@fullPathToSource\@empty
%    \end{macrocode}
% \paragraph*{After creation hooks.} We provide additional hooks, the first is placed
% just after a tile file is created, and second one is placed just after the package file is created.
%
% \medskip\noindent
%\DescribeMacro\afterTileCreationHook\hskip-\marginparsep\texttt
%{\darg{\ameta{\cs{cmds}}}} The \ameta{\cs{cmds}} can be any valid {\LaTeX} commands,
%conceptually, the commands may contain \cs{ShellEscape} commands. The hook is placed
%just after a tile file is created as a PDF.\par\medskip\noindent
%\DescribeMacro\afterPkgCreationHook\hskip-\marginparsep\texttt
%{\darg{\ameta{\cs{cmds}}}} Similar to \cs{afterTileCreationHook}, but for the creation
%of the tile package.
%    \begin{macrocode}
\def\afterTileCreationHook#1{\def\@fterTileCreationHook{#1}}
\let\@fterTileCreationHook\relax
%    \end{macrocode}
%    The definition of   \cs{afterPkgCreationHook}.
%    \begin{macrocode}
\def\afterPkgCreationHook#1{\def\@fterPkgCreationHook{#1}}
\let\@fterPkgCreationHook\relax
%    \end{macrocode}
%\subparagraph*{Examples.} This example makes each \emph{tile} PDF file into a tile EPS file.
%We use the utility executable \app{pdftops}, which may be available on your {\TeX} system.
%In the preamble or above the \cs{tileTheGraphic} command, place the following commands.
%\begin{Verbatim}[xleftmargin=\parindent,codes={\catcode`\%=9},commandchars={*()}]
%\afterTileCreationHook{*%
%  \ShellEscape{pdftops -eps tile-template.pdf
%    tile-template.eps}*%
%  \ShellEscape{copy tile-template.eps \tgTileBaseIndx.eps}*%
%  \iftgfolder
%    \ShellEscape{move \tgTileBaseIndx.eps \tgInFolder}*%
%  \fi
%}
%\afterPkgCreationHook{*%
%  \typeout{!! Package creation: \tgBaseName_package.pdf !!}}
%\end{Verbatim}
%\subparagraph*{Commentary.} At insertion point of the hooks in the code stream, the
%tile file is named \texttt{tile-template}.
%The declared \cs{afterTileCreationHook} converts each tile file,
%to an EPS file of the same name. Then, it copies \texttt{tile-temp.eps} to
%\DescribeMacro\tgTileBaseIndx\cs{tgTileBaseIndx.eps}. Next, we test whether
%this file came from a subfolder (using the switch \DescribeMacro\iftgfolder
%\cs{iftgfolder}). Finally, if the file belongs in the folder, \DescribeMacro\tgInFolder\cs{tgInFolder},
%we move it there. For the \cs{afterPkgCreationHook}, we do nothing other than to write some
%text to the terminal, we use the base name of the graphic \DescribeMacro\tgBaseName
%\cs{tgBaseName}.
%\medskip\par\noindent
%    \DescribeMacro\packagesuffix\hskip-\marginparsep\texttt{\darg{\ameta{name}}}
%    The name used as suffix to the packaged tiled files. Originally, this was \texttt{packaged},
%    but I am changing it to \texttt{package}, to be in conformance with the \pkg{acrosort} package.
%    \changes{v1.0.1}{2020/05/27}{Replace suffix of packaged with just package, but control the
%    naming with \string\cs{packagesuffix}.}
%    \begin{macrocode}
\newcommand{\packagesuffix}{package}
%    \end{macrocode}
% \subsection{\tops{\protect\cs{setTileParams}}{\textbackslash{setTileParams}}: A preamble command}
% \leavevmode\DescribeMacro\setTileParams\hskip-\marginparsep
%   \texttt{[\ameta{ig-opts}]\darg{\ameta{nRows}}\darg{\ameta{nCols}}\darg{\ameta{path}}} The parameters
%   are described in Section~\ref{s:docs} (\textbf{Documentation}).
%    \begin{macrocode}
\newcommand{\setTileParams}[4][]{%
%    \end{macrocode}
%     We require the entries in \texttt{\#2} and \texttt{\#3} (\ameta{nRows} and \ameta{nCols})
%     to be nonnegative natural numbers (1, 2, 3, \dots), so we pass through a dimension register
%     and into a count register, this should make what is entered a natural number.
%     If not greater or equal to 1, we complain, and set to the number 2.
%    \begin{macrocode}
  \tg@dima #3\p@\relax
  \ifdim\tg@dima < \p@
    \PackageWarning{tile-graphic}
      {Number of columns must be positive,\MessageBreak
       setting number of columns to 2}\tg@dima\tw@\p@\fi
  \edef\nCols{\strip@pt\tg@dima}%
  \tg@Cnt\nCols\relax
  \edef\n@Cols{\the\tg@Cnt}\edef\nCols{\the\tg@Cnt}%
  \tg@dima #2\p@\relax
  \ifdim\tg@dima < \p@
    \PackageWarning{tile-graphic}
      {Number of rows must be positive,\MessageBreak
       setting number of rows to 2}\tg@dima\tw@\p@\fi
  \edef\nRows{\strip@pt\tg@dima}%
  \tg@Cnt\nRows\relax
  \edef\n@Rows{\the\tg@Cnt}\edef\nRows{\the\tg@Cnt}%
  \multiply\tg@Cnt \nCols\relax
  \edef\nFilesCreated{\the\tg@Cnt}%
  \def\pathToPic{#4}%
%    \end{macrocode}
% Parse the path to obtain the parts of the path, area, base, and extension.
%    \begin{macrocode}
  \filename@parse{#4}%
  \edef\tg@dir{\filename@area}%
  \ifx\tg@dir\@empty\tgfolderfalse\else
    \expandafter\tg@rmslash\tg@dir
    \tgfoldertrue
  \fi
  \edef\tg@base{\filename@base}%
  \edef\tgBaseName{\filename@base}%
  \edef\tg@ext{\filename@ext}%
%    \end{macrocode}
% Get graphic dimensions, dimensions needed for {\TeX} (pt) and PDF (bp)
%    \begin{macrocode}
  \setbox\tg@box\hbox{\includegraphics[draft,#1]{#4}}%
  \setlength\tg@dima{\the\wd\tg@box}%
  \tg@dima=.99626\tg@dima
  \divide\tg@dima \nCols
  \edef\bpWdtile{\strip@pt\tg@dima}%
  \setlength\tg@dima{\the\ht\tg@box}%
  \tg@dima=.99626\tg@dima
  \divide\tg@dima \nRows
  \edef\bpHttile{\strip@pt\tg@dima}%
  \setbox\tg@box\box\voidb@x
  \tg@dima=\bpHttile pt \relax
  \edef\tg@HT{\the\tg@dima}
  \tg@dima=\bpWdtile pt \relax
  \edef\tg@WD{\the\tg@dima}
%    \end{macrocode}
% Set margins and screen size using the \pkg{web} package. If \cs{ifpassthruTG} is true,
% we use reasonably size dimensions to display the \textbf{Tile Graphic Report}; otherwise,
% we use dimensions based on the size of the graphic determined by \ameta{path}.
%    \begin{macrocode}
  \ifpassthruTG
    \web@MargScrDimOpts{.25in}{.25in}{24pt}{.25in}{5in}{6in}
  \else
    \web@MargScrDimOpts{0pt}{0pt}{0pt}{0pt}{\tg@HT}{\tg@WD}
  \fi
%    \end{macrocode}
% \paragraph*{Bounding box calculations for the tiles}\leavevmode\par\medskip\noindent
%^^A%
% The $y$-coordinate calculations: If \cs{nRows} is $3$, for example, we calculate $4$ $y$-coodinates,
% \cs{y1}, \cs{y2}, \cs{y3}, \cs{y4}, from bottom to top.
%    \begin{macrocode}
  \@tempcnta\n@Rows\relax
  \advance\@tempcnta\@ne
  \edef\n@Rows{\the\@tempcnta}
  \@tempcnta\z@
  \tg@dima0pt
  \tg@dimb=\tg@HT\relax
  \@whilenum \@tempcnta < \n@Rows \do {%
    \advance\@tempcnta\@ne
    \csarg\edef{y\the\@tempcnta}{\strip@pt\tg@dima}
    \advance\tg@dima \tg@dimb
  }
%    \end{macrocode}
% The $x$-coordinate calculations If \cs{nCols} is $2$, for example, we calculate $3$ $x$-coodinates,
% \cs{x1}, \cs{x2}, \cs{x3}, from left to right.
%    \begin{macrocode}
  \@tempcnta\n@Cols\relax
  \advance\@tempcnta\@ne
  \edef\n@Cols{\the\@tempcnta}
  \@tempcnta\z@
  \tg@dima0pt
  \tg@dimb=\tg@WD\relax
  \@whilenum \@tempcnta < \n@Cols \do {%
    \advance\@tempcnta\@ne
    \csarg\edef{x\the\@tempcnta}{\strip@pt\tg@dima}
    \advance\tg@dima \tg@dimb
  }
%    \end{macrocode}
%    Having finished the calculations, we then execute \cs{tg@wrtthefiledoc}.
%    \begin{macrocode}
  \if@wrtorpkg\expandafter
    \tg@wrtthefiledoc\fi
}
%    \end{macrocode}
%    \DescribeMacro\tg@wrtthefiledoc
%    This command writes the \texttt{tile-template.tex} file to the source
%    file folder. It will read,
%\begin{Verbatim}[xleftmargin=\leftmargini,codes={\catcode`\%=9}]
%\RequirePackage{tmp}
%\documentclass{article}
%\usepackage{web}
%\usepackage{graphicx}
%\let\WriteBookmarks\relax
%\margins{0pt}{0pt}{0pt}{0pt}
%\screensize{\tg@HT}{\tg@WD}
%\parindent0pt\parskip0pt
%\begin{document}
%\tgInputContent
%\end{document}
%\end{Verbatim}
%This is the file that is compiled using \cs{ShellEscape} to create the individual
%tile files. This document contains a trick, the use of the \pkg{tmp} package, which
%is written dynamically just before this file is compiled. The \pkg{tmp} package is
%created by \cs{tg@wrttmppkg}, defined next. The \pkg{tmp} package defines the
%command \DescribeMacro\tgInputContent\cs{tgInputContent} in the body of the document.
%    \begin{macrocode}
\def\tg@wrtthefiledoc{\newwrite\wrttiledoc
  \long\def\IWTD##1{\immediate\write\wrttiledoc{##1}}
  \immediate\openout \wrttiledoc tile-template.tex
  \IWTD{%
    \string\RequirePackage{tmp}^^J%
    \string\documentclass{article}^^J%
    \string\usepackage{web}^^J%
    \string\usepackage{graphicx}^^J%
    \string\let\string\WriteBookmarks\string\relax^^J%
    \string\margins{0pt}{0pt}{0pt}{0pt}^^J%
    \string\screensize{\tg@HT}{\tg@WD}^^J%
    \string\parindent0pt\string\parskip0pt^^J%
    \string\begin{document}^^J%
    \string\tgInputContent^^J%
    \string\end{document}}%
  \immediate\closeout \wrttiledoc
}
%    \end{macrocode}
%    \leavevmode\DescribeMacro\tg@wrttmppkg\hskip-\marginparsep\texttt{\darg{\ameta{basename}}\darg{\ameta{indx}}}
%    Write the \pkg{tmp} package dynamically: \ameta{basename} is the base name of the graphic; \ameta{indx} is the
%    index of the tile (\texttt{01}, \texttt{02}, \texttt{03}, \dots). The action of this package
%    is to define \cs{tgInputContent} to input the file
%    \begin{quote}\ttfamily
%     \ameta{basename}\_\ameta{indx}.cut
%    \end{quote}
%    \begin{macrocode}
\def\tg@wrttmppkg#1#2{\def\CommentCutFile{tmp.sty}%
  \immediate\openout\CommentStream \CommentCutFile
    \immediate\write\CommentStream{\string
      \def\string\tgInputContent{\string
      \InputIfFileExists{#1_#2.cut}%
      {}{\string\null}}}%
  \immediate\closeout\CommentStream
}
%    \end{macrocode}
% \subsection{\tops{\protect\cs{tileTheGraphic}}{\textbackslash{tileTheGraphic}}: A document body command}
%     The file \texttt{create-tb.tex} contains the single command \cs{tileTheGraphic} in the body
%     of the document. It has not arguments.
%    \begin{macrocode}
\InputIfFileExists{package-data.cut}{}{}%
%    \end{macrocode}
%   \DescribeMacro\tg@msgi
%    is the content of \texttt{create-tg} to deliver the \textbf{Tile Graphic Report}. It may be redefined.
%    This content command is expanded in \cs{tileTheGraphic}, defined below.
%    \begin{macrocode}
\def\tg@msgi{%
    \textbf{Tile Graphic Report}\medskip\par
    \textbf{Options:}
    \begin{itemize}
    \if@wrttofiles
      \item The \texttt{wrttofiles} option  taken,
        {\nFilesCreated} files written ({\nRows}~rows, {\nCols}~cols):
        \begin{quote}
        \texttt{\tg@base\_01.pdf}, \texttt{\tg@base\_02.pdf}, \dots.
        \end{quote}
        \ifx\tg@dir\@empty Files saved to source file folder. \else
        Files saved to the \texttt{\tgInFolder} folder.\fi
    \else
      \item The \texttt{!wrttofiles} option taken, no files written.
    \fi
    \if@packagefiles
    \item The \texttt{packagefiles} option taken,
      package file saved as \texttt{\tg@base\_\packagesuffix.pdf}.
      The package contains {\nFilesCreated} pages of tiled graphics.
      \ifx\tg@dir\@empty
        The package file saved to source file folder.
      \else
        The package file saved to the \texttt{\tgInFolder} folder.
      \fi
    \else
    \item The \texttt{!packagefiles} option taken,
      no package file created.
    \fi
    \end{itemize}
    \if@wrtorpkg\else
      Try using the \texttt{wrttofiles} and/or
      \texttt{packagefiles} option(s) to obtain a nontrivial result.
    \fi
}
%    \end{macrocode}
%\leavevmode\DescribeMacro\tileTheGraphic (No arguments) This is the command that tiles
%the graphic.
%    \begin{macrocode}
\def\tileTheGraphic{\begingroup\let\@nu\@nameuse
%    \end{macrocode}
%    If the \opt{!packagefiles} option is in effect, we set \cs{ifpassthruTG} to false
%    so we can execute the \cs{else} part within \texttt{create-tg.tex} (as opposed
%    to in \texttt{\ameta{basename}\_\packagesuffix.tex}).
%    \begin{macrocode}
  \if@packagefiles\else\global\passthruTGfalse\expandafter
    \tg@msgi % provide content
  \fi
  \ifpassthruTG
    \tg@msgi % provide content
  \else
%    \end{macrocode}
%    Okay, we are here either because we are compiling this file either from within
%    \texttt{\ameta{basename}\_\packagesuffix.tex} or from within the source file \texttt{create-tg.tex}
%    with the \opt{!packagefiles} option is in effect.
%    \begin{macrocode}
    \global\let\tg@IndxToks\@empty
%    \end{macrocode}
%    Nested \cs{multido} loop to create grid
%    \begin{macrocode}
    \if@wrtorpkg\expandafter\tg@domultido\fi
  \fi
  \endgroup
  \xdef\nFilesCreated{\the\tg@Cnt}%
  \edef\x{\if@wrtorpkg\noexpand\compileTileFiles\fi}\x
}
%    \end{macrocode}
%   \leavevmode\DescribeMacro\tg@domultido Called by \cs{tileTheGraphic}. Consists of
%    nested \cs{multido} loops. The command both creates the package file and the individual
%    tile graphics, depending on the options. The command \cs{tg@IndexToks} creates a token list
%    of indices \verb~{01}{02}{03}...~ that is later used in a \cs{@tfor} loop.
%    \begin{macrocode}
\def\tg@domultido{%
    \tg@Cnt\z@
    \multido{\iR=\nRows+-1}{\nRows}{%
      \multido{\iC=1+1}{\nCols}{%
        \global\advance\tg@Cnt\@ne
        \ifnum\tg@Cnt<10\relax
          \edef\x{0\the\tg@Cnt}\else
          \edef\x{\the\tg@Cnt}\fi
          \edef\y{\noexpand\g@addto@macro\noexpand
            \tg@IndxToks{{\x}}}\y
          \@tempcntb\iC
          \advance\@tempcntb\@ne
          \edef\oX{\the\@tempcntb}%
          \@tempcntb\iR
          \advance\@tempcntb\@ne
          \edef\oY{\the\@tempcntb}%
%    \end{macrocode}
%     We write the CUT files.
%    \begin{macrocode}
          \wrtTileCuts
%    \end{macrocode}
%     Include the graphic with the appropriate viewport and clip; however,
%     we executed the temporary command \cs{z} if the option \cs{packagefiles}
%     is in effect.
%    \begin{macrocode}
          \edef\z{\noexpand\parbox{\tg@WD}{\noexpand
            \includegraphics[width=\tg@WD,%
              viewport=\@nu{x\iC} \@nu{y\iR} \@nu{x\oX} \@nu{y\oY},%
              clip]{\pathToPic}}}\if@packagefiles\expandafter
              \z\expandafter\newpage\fi
      }% inner multido
    }% outer multido
}
%    \end{macrocode}
%    \leavevmode\DescribeMacro\wrtTileCuts (Called by \cs{tg@domultido}) The CUT files created are the body content
%    of the \texttt{tile-template.tex} file. The CUTs are also used by the package file routine.
%    The content of these CUT files has the following form:
%\begin{Verbatim}[xleftmargin=\parindent,codes={\catcode`\%=9},commandchars=!()]
%\parbox{132.23935pt}{\includegraphics[width=132.23935pt,
%   viewport=0 114.23943 132.23935 228.47885,clip]{!ameta(pathToPic)}}
%\end{Verbatim}
%where the values of \texttt{width} and \texttt{viewport} were calculated by the \cs{setTileParams}
%in the preamble.
%    \begin{macrocode}
\def\wrtTileCuts{%
  \def\CommentCutFile{\tg@base_\x.cut}%
  \immediate\openout\CommentStream=\CommentCutFile
  \immediate\write\CommentStream{\string
    \parbox{\tg@WD}{\string
    \includegraphics[width=\tg@WD,%
    viewport=\@nu{x\iC} \@nu{y\iR} \@nu{x\oX} \@nu{y\oY},%
    clip]{\pathToPic}}}\immediate\closeout\CommentStream
}
%    \end{macrocode}
%\leavevmode\DescribeMacro\compileTileFiles (Called from \cs{tileTheGraphic}) This command performs
%the \cs{ShellEscape} steps. It is executed only if the \opt{wrttofiles} or \opt{packagefiles} option
%is taken (or both).
%    \begin{macrocode}
\def\compileTileFiles{%
  \ifpassthruTG
    \if@packagefiles
%    \end{macrocode}
%    \paragraph*{Package the tile files.} This code is executed by \texttt{create-tg.tex} (because \cs{passthruTG} is true).
%    We write the \texttt{package-data.cut} file, later input by the \pkg{tmp} package, which puts \cs{passthruTG}
%    to false.
%    \begin{macrocode}
      \def\CommentCutFile{package-data.cut}%
      \immediate\openout\CommentStream \CommentCutFile
        \immediate\write\CommentStream{\string\passthruTGfalse}%
      \immediate\closeout\CommentStream
%    \end{macrocode}
%    Then copy \cs{create-tg.tex} to \texttt{\ameta{basename}\_\packagesuffix.tex}. Keep in mind
%    that when we compile \cs{tg@base\_\packagesuffix.tex} which uses the \pkg{tile-graphic} package,
%    \cs{passthruTG} is false. When \texttt{\ameta{basename}\_\packagesuffix.tex} gets here,
%    this block of code is skipped over.
%    \begin{macrocode}
      \ShellEscape{\tg@syscopy \jobname.tex
        \tg@base_\packagesuffix.tex}%
%    \end{macrocode}
%    and compile with the \texttt{--shell-escape} switch,
%    \begin{macrocode}
      \iftg@direct
        \ShellEscape{\pkg@ppArgs{\tg@pdfcreator@app} --shell-escape
          \tg@base_\packagesuffix.tex}%
      \else
        \ShellEscape{\latex@ppArgs{latex} --shell-escape
          \tg@base_\packagesuffix.tex}%
        \ShellEscape{\dvips@ppArgs{dvips} \tg@base_\packagesuffix.dvi}%
        \ifnum\tg@case=\thr@@
          \ShellEscape{\pkg@ppArgs{\tg@pdfcreator@app}
          \tg@base_\packagesuffix.ps}%
        \else
          \ShellEscape{\pkg@ppArgs{\tg@pdfcreator@app} /N /Q
          "\tg@fullPathToSource/\tg@base_\packagesuffix.ps"}%
        \fi
        \ShellEscape{\tg@sysdel \tg@base_\packagesuffix.dvi
          \tg@base_\packagesuffix.ps}%
      \fi
%    \end{macrocode}
%    Insert the after-package-creation-hook \DescribeMacro\@fterPkgCreationHook
%    \cs{@fterPkgCreationHook}
%    \begin{macrocode}
      \@fterPkgCreationHook
%    \end{macrocode}
%    clean up,
%    \begin{macrocode}
      \ShellEscape{\tg@sysdel \tg@base_\packagesuffix.tex
        \tg@base_\packagesuffix.log \tg@base_\packagesuffix.aux}%
%    \end{macrocode}
%    and move into the folder from which the graphic resides, if necessary.
%    \begin{macrocode}
      \iftgfolder
        \ShellEscape{\tg@sysmove
          \tg@base_\packagesuffix.pdf \tgInFolder}\fi
    \fi
  \else
%    \end{macrocode}
%    \paragraph*{Create the tile files.} This block is compiled if \cs{passThruTG}
%    is false and the \opt{wrttofiles} option is taken.
%    The block gets compiled in two instances:
%    \begin{enumerate}
%     \item[(1)] by \texttt{\ameta{basename}\_\packagesuffix.tex} if the \opt{packagefiles} and
%        \opt{wrttofiles} options are taken;
%     \item[(2)] by \texttt{create-tg.tex} if \opt{!packagefiles} and
%        \opt{wrttofiles} options are taken. (Recall that if \opt{!packagefiles}
%        is taken, then \cs{ifpassThruTG} is set to false earlier in the code stream.)
%    \end{enumerate}
%    \begin{macrocode}
    \if@wrttofiles
      \edef\@tforexp{\noexpand
        \@tfor\noexpand\Indx:=\tg@IndxToks}%
%    \end{macrocode}
%      For each token in \cs{tg@IndexToks} (\verb!{01}{02}{03}...)!, we compile
%      the dynamic file \texttt{tile-template.tex}.
%    \begin{macrocode}
      \@tforexp \do {%
        \edef\tgTileBaseIndx{\tg@base_\Indx}%
%    \end{macrocode}
%      Create the \pkg{tmp} package with parameters \ameta{basename} and \ameta{indx}.
%    \begin{macrocode}
        \edef\x{\noexpand\tg@wrttmppkg{\tg@base}{\Indx}}\x
%    \end{macrocode}
%      Compile this turkey,
%    \begin{macrocode}
        \iftg@direct
          \ShellEscape{\tile@ppArgs{\tg@pdfcreator@app}
            tile-template.tex}%
        \else
          \ShellEscape{\latex@ppArgs{latex} --shell-escape
            tile-template.tex}%
          \ShellEscape{\dvips@ppArgs{dvips} tile-template.dvi}%
          \ifnum\tg@case=\thr@@
            \ShellEscape{\pkg@ppArgs{\tg@pdfcreator@app}
              tile-template.ps}%
          \else
            \ShellEscape{\pkg@ppArgs{\tg@pdfcreator@app} /N /Q
            "\tg@fullPathToSource/tile-template.ps"}%
          \fi
          \ShellEscape{\tg@sysdel tile-template.dvi
            tile-template.ps}%
        \fi
%    \end{macrocode}
%    Insert the after-tile-creation-hook \DescribeMacro\@fterTileCreationHook
%    \cs{@fterTileCreationHook}
%    \begin{macrocode}
        \@fterTileCreationHook
        \ShellEscape{\tg@syscopy tile-template.pdf
          \tgTileBaseIndx.pdf}%
%    \end{macrocode}
%      clean up,
%    \begin{macrocode}
        \ShellEscape{\tg@syscopy tile-template.pdf
          \tgTileBaseIndx.pdf}%
      }% do
%    \end{macrocode}
%      and move to another folder if necessary
%    \begin{macrocode}
      \iftgfolder
        \ShellEscape{\tg@sysmove \tg@base_*.pdf \tgInFolder}\fi
    \fi
  \fi
%    \end{macrocode}
%      finished! Just clean up all aux files.
%    \begin{macrocode}
  \ShellEscape{\tg@sysdel \tg@base_*.cut package-data.cut}%
  \ShellEscape{\tg@sysdel tmp.sty tile-template.*}%
}
%    \end{macrocode}
%      Letting \cs{WriteBookmarks} to \cs{relax} prevents \pkg{hyperref} from
%      complaining about the OUT file is not up to date. No bookmarks are created.
%      Also load the configuration file \marginpar{\raggedleft\small\texttt{tg.cfg}}\texttt{tg.cfg},
%      if it exists.
%    \begin{macrocode}
\let\WriteBookmarks\relax
\InputIfFileExists{tg.cfg}{}{}
\tg@restoreCats
\parindent0pt
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%\Finale
\endinput