summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/asyfig/asyfig.sty
blob: 5db9b558868b0898ca77ac891c8451fc18b4b4ef (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
%% LaTeX2e file `asyfig.sty'
%% generated by the `filecontents' environment
%% from source `asyfig' on 2010/03/20.
%%
\ProvidesPackage{asyfig}[2010/03/20 v0.1c
  Commands for using asymptote figures]

% This package is the main user interface for inserting external |asy| figures
% into the document.

\RequirePackage{%
  asyalign,color,ifmtarg,ifpdf,ifplatform,import,
  graphicx,pdftexcmds,suffix,xkeyval}

% Better conditionals than |\newif| provides:
\def\@True{11}
\def\@False{01}
\def\asy@If#1{\if#1\relax\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}

\let\asy@always\@False
\let\asy@never\@False
\let\asy@process\@False

% Package options:
\define@choicekey*{asyfig.sty}{process}[\@tempa\@tempb]{all,none,auto}{%
  \ifcase\@tempb\relax
    \let\asy@always\@True
  \or
    \let\asy@never\@True
  \or
  \fi
}
\ExecuteOptions{process=auto}

\ProcessOptionsX

% \subsection{Auxiliary macros}

\def\asy@splitpath#1/#2/{%
% Recursive macro that is used like\\
% \qquad |\asy@splitpath abc/def/ghi.asy/\@nil/|\\
% It defines |\asy@filename| $\to$ |ghi.asy| and |\asy@path| $\to$ |abc/def/|
  \ifx\@nil#2\relax
% If input is \<anything>|/\@nil/| then we've reached the end:
    \def\asy@filename{#1}%
  \else
% Otherwise we're in the middle of the slash-separated list;
% build up |\asy@path|, and iterate:
    \edef\asy@path{\asy@path#1/}%
    \def\@tempa{\asy@splitpath#2/}%
    \expandafter\@tempa
  \fi
}

\newcommand\asypath[1]{\def\asy@pathprefix{#1}}
\asypath{}

\def\asy@asyfile{\asy@pathprefix\asy@path\asy@filename.asy}
\def\asy@texfile{\asy@pathprefix\asy@path\asy@filename\string_.tex}

\def\asy@cmdsep{\ifwindows \string& \else; \fi}

% \subsection{The main macro}

\newcommand\asyfig[1]{%
  \let\asy@path\@empty
  \asy@splitpath #1/\@nil/%
  \IfFileExists{\asy@asyfile}{%
    \asy@If\asy@process{}{%
    \asy@If\asy@always{%
      \let\asy@process\@True
    }{%
      \IfFileExists{\asy@texfile}{%
        \asy@If\asy@never{}{%
          % compare file dates to see if we want to reprocess:
          \ifnum\pdf@strcmp{\pdf@filemoddate{\asy@texfile}}
                           {\pdf@filemoddate{\asy@asyfile}} < \z@
            \let\asy@process\@True
          \fi
        }%
      }{% if the .tex file doesn't exist, either
        % give an error or process the .asy file:
        \asy@If\asy@never{%
          \PackageError{asyfig}{%
            ^^J\space\space\space\space
            "\asy@pathprefix\asy@path\asy@filename.asy" requires processing%
          }{%
            The generated file that is required to insert the asy graphic,
            ^^J\space\space\space\space
            "\asy@pathprefix\asy@path\asy@filename\string_.tex"^^J%
            does not exist.
            Please process the asy figure manually or de-activate the^^J%
            [process=none] package option.
          }%
        }{%
          \let\asy@process\@True
        }
      }%
    }}%
    \asy@If\asy@process{%
      \edef\@tempa{\asy@pathprefix\asy@path}%
      \pdf@system{%
        echo "^^J====== ASY PROCESS =====^^J"
        \asy@cmdsep
        \ifx\@tempa\@empty\else
          cd \@tempa
          \asy@cmdsep
        \fi
        \ifpdf pdf\fi latex
          -shell-escape
          -interaction=batchmode
          -jobname=\asy@filename-comp
        \unexpanded{%
          "\RequirePackage{asyprocess}\ProcessAsy
           \documentclass{article}
           \begin{document}\ShowAsy
           \end{document}"
        }%
        \asy@cmdsep
        echo "^^J==== ASY END PROCESS ===^^J"
      }%
    }{}%
    \import{\asy@pathprefix\asy@path}{\asy@filename\string_.tex}%
  }{%
    \PackageWarning{asyfig}{%
      ^^J\space\space
      "\asy@pathprefix\asy@path\asy@filename.asy" not found.^^J%
      This warning occurred%
    }%
  }%
  \let\asy@process\@False
}

% The starred version of \cmd\asyfig\ processes the graphic always:
\WithSuffix\newcommand\asyfig*[1]{%
  \begingroup
    \let\asy@process\@True
    \csname\NoSuffixName\asyfig\endcsname{#1}%
  \endgroup
}