| 1 |
|
| 2 |
% We need to know the vertical position on the page where the figures |
| 3 |
% are located. Because this position can't be known until the page is |
| 4 |
% fully composed ("shipout"), the value is only accurate after the |
| 5 |
% second run, just like the \ref command. The value from the previous |
| 6 |
% run is available in the .aux file. |
| 7 |
\RequirePackage{zref-savepos} |
| 8 |
|
| 9 |
% The zref package also provides the page number. It gives the |
| 10 |
% absolute page number, not the logical page number. Things like the |
| 11 |
% table of contents and preface are part of the count. This value is |
| 12 |
% available from abspage. This number is zero-based, so that the |
| 13 |
% "first" page is numbered as zero. Just as positions within a page |
| 14 |
% are not really known until a page is shipped out, the page number |
| 15 |
% isn't known either. Generally, if you're near the middle or the end |
| 16 |
% of the page, then \thepage or abspage is correct, but it is often |
| 17 |
% wrong near the top of the page. The only way around this seems to be |
| 18 |
% to place a label on the page, in a manner similar to what is done |
| 19 |
% for positions within the page. |
| 20 |
\RequirePackage{zref-thepage} |
| 21 |
\RequirePackage{zref-abspage} |
| 22 |
\RequirePackage{zref-user} |
| 23 |
\RequirePackage{zref-pagelayout} |
| 24 |
\RequirePackage{xsim} |
| 25 |
\RequirePackage{tikz} |
| 26 |
|
| 27 |
% This is needed to help with the process of replacing the body of |
| 28 |
% the environment and (optionally) copying it to an external file. |
| 29 |
\RequirePackage{verbatim} |
| 30 |
|
| 31 |
|
| 32 |
\ExplSyntaxOn |
| 33 |
|
| 34 |
% The fig.aux file is open throughout the run. |
| 35 |
\iow_new:N \g_figurefile_iow |
| 36 |
\iow_open:Nn \g_figurefile_iow { \jobname.fig.aux } |
| 37 |
|
| 38 |
% The inner and outer margin widths |
| 39 |
\dim_new:N \l_figput_innermargin_dim |
| 40 |
\dim_new:N \l_figput_outermargin_dim |
| 41 |
|
| 42 |
% By default, set these margins to have zero width. This has no effect |
| 43 |
% on how the text is laid out by latex. It's used on the browser end |
| 44 |
% to set the origin for drawing. In turn, that affects where the |
| 45 |
% figures end up when the tikz is loaded. |
| 46 |
% |
| 47 |
% BUG: There should be a way (?) to determine where the left margin of |
| 48 |
% the text is from latex instead of inputing your best approximation. |
| 49 |
% At the same time, the user might want to adjust this regardless. |
| 50 |
\dim_set:Nn \l_figput_innermargin_dim { 0 pt } |
| 51 |
\dim_set:Nn \l_figput_outermargin_dim { 0 pt } |
| 52 |
|
| 53 |
% Two little commands to set these values. |
| 54 |
\NewDocumentCommand\SetInnerMargin { m } |
| 55 |
{ |
| 56 |
\dim_set:Nn \l_figput_innermargin_dim { #1 } |
| 57 |
} |
| 58 |
|
| 59 |
\NewDocumentCommand\SetOuterMargin { m } |
| 60 |
{ |
| 61 |
\dim_set:Nn \l_figput_outermargin_dim { #1 } |
| 62 |
} |
| 63 |
|
| 64 |
% Use this to turn off any of the optional "skip" arguments to figput. |
| 65 |
% If you call \NeverSkip, then these "skip" arguments are ignored. |
| 66 |
\bool_new:N \l_figput_skipoff_bool |
| 67 |
\bool_set_false:N \l_figput_skipoff_bool |
| 68 |
|
| 69 |
\NewDocumentCommand\NeverSkip{} { |
| 70 |
\bool_set_true:N \l_figput_skipoff_bool |
| 71 |
} |
| 72 |
|
| 73 |
\NewDocumentCommand\AllowSkip{} { |
| 74 |
\bool_set_false:N \l_figput_skipoff_bool |
| 75 |
} |
| 76 |
|
| 77 |
% Use this to request that a specific file be loaded by the browser. |
| 78 |
% This simply writes a line to the .aux file. |
| 79 |
\NewDocumentCommand\LoadFigureCode{ m } |
| 80 |
{ |
| 81 |
\group_begin: |
| 82 |
|
| 83 |
\iow_now:Nx \g_figurefile_iow { load~ #1 } |
| 84 |
|
| 85 |
\group_end: |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
% The variables used below: |
| 90 |
% |
| 91 |
% Manditory name for the figure, as given by the user. Using string, |
| 92 |
% although maybe token list would be better? |
| 93 |
\str_new:N \l_figput_figname_str |
| 94 |
|
| 95 |
% The manditory height given by the user. This is a dim, as opposed to |
| 96 |
% merely a floating-point value. |
| 97 |
\dim_new:N \l_figput_height_dim |
| 98 |
|
| 99 |
% The optional height adjustments given by the user. Again, dims. |
| 100 |
\dim_new:N \l_figput_htAbove_dim |
| 101 |
\dim_new:N \l_figput_htBelow_dim |
| 102 |
|
| 103 |
% Scratch values used for calculation of figure position. |
| 104 |
\tl_new:N \l_figput_tempa_tl |
| 105 |
\tl_new:N \l_figput_tempb_tl |
| 106 |
\tl_new:N \l_figput_tempc_tl |
| 107 |
|
| 108 |
% And several booleans. |
| 109 |
% BUG: In theory, it would be possible to get rid of nostatic. |
| 110 |
% If the figure height is set to zero, then that should implicitly |
| 111 |
% imply nostatic. |
| 112 |
\bool_new:N \l_figput_hasintht_bool |
| 113 |
\bool_new:N \l_figput_nostatic_bool |
| 114 |
\bool_new:N \l_figput_done_bool |
| 115 |
\bool_new:N \l_figput_skip_bool |
| 116 |
|
| 117 |
% This is for the figures file: the position of the figure on the page. |
| 118 |
\dim_new:N \l_figput_pagepos_dim |
| 119 |
|
| 120 |
% BUG: I don't understand the whole concept of these variants of |
| 121 |
% functions. Somehow it deals with various cases of whether arguments |
| 122 |
% are expanded or not. There's another variant defintion below as well. |
| 123 |
\cs_generate_variant:Nn \file_if_exist:nTF {V} |
| 124 |
\cs_generate_variant:Nn \file_input:n {V} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
% This command works in (almost) exactly the same way as the figput |
| 129 |
% environment. The differenence is that, as a command instead of |
| 130 |
% an environment, there is no body to deal with. Most of the comments |
| 131 |
% have been removed. See the definition of the environment for *much* |
| 132 |
% more detailed comments. |
| 133 |
% |
| 134 |
% BUG: possible to define a function and combine these? |
| 135 |
\NewDocumentCommand\FigPut { > { \SplitArgument { 1 } { , } } m!o } |
| 136 |
{ |
| 137 |
\group_begin: |
| 138 |
|
| 139 |
% The manditory arguments come in in #1 |
| 140 |
\figput_manargs #1 |
| 141 |
|
| 142 |
% Now the optional arguments. |
| 143 |
\bool_set_false:N \l_figput_hasintht_bool |
| 144 |
\bool_set_false:N \l_figput_nostatic_bool |
| 145 |
\bool_set_false:N \l_figput_done_bool |
| 146 |
\bool_set_false:N \l_figput_skip_bool |
| 147 |
|
| 148 |
\tl_if_blank:nTF { #2 } {} { |
| 149 |
\clist_set:Nn \l_figput_optclist_clist { #2 } |
| 150 |
\figput_optargs { } |
| 151 |
} |
| 152 |
|
| 153 |
% We have everything needed to know what to do. |
| 154 |
\str_set:Nx \l_figput_filespec_str { |
| 155 |
\use:c { zref@extractdefault } { \l_figput_figname_str -pageno} { abspage } { 0 } |
| 156 |
} |
| 157 |
|
| 158 |
% The inner and outer margins. |
| 159 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_innermargin_dim } |
| 160 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_outermargin_dim } |
| 161 |
|
| 162 |
% And the text width. |
| 163 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \textwidth } |
| 164 |
|
| 165 |
% Massive fooling around to do arithmetic on page position. |
| 166 |
\tl_set:Nn \l_figput_tempa_tl { \zposy { \l_figput_figname_str } } |
| 167 |
% Convert the figure height to sp. |
| 168 |
\tl_set:Nn \l_figput_tempb_tl { \dim_to_decimal_in_sp:n{ \l_figput_height_dim } } |
| 169 |
% Add |
| 170 |
\tl_set:Nn \l_figput_tempc_tl { \int_eval:n { \l_figput_tempa_tl + \l_figput_tempb_tl } } |
| 171 |
% Convert to a dim |
| 172 |
\dim_set:Nn \l_figput_pagepos_dim { \l_figput_tempc_tl sp } |
| 173 |
% And (whew!) write it out, in bp. |
| 174 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_pagepos_dim } |
| 175 |
|
| 176 |
% The latex height of the figure (in bp) |
| 177 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_height_dim } |
| 178 |
|
| 179 |
% The interactive height paddings, which are zero by default. |
| 180 |
\bool_if:NTF \l_figput_hasintht_bool |
| 181 |
{ |
| 182 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_htAbove_dim } |
| 183 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_htBelow_dim } |
| 184 |
} |
| 185 |
{ |
| 186 |
\str_put_right:Nx \l_figput_filespec_str { ~0~0 } |
| 187 |
} |
| 188 |
|
| 189 |
% The figure name -- used for the JS function to call. |
| 190 |
\str_put_right:Nx \l_figput_filespec_str { ~ \l_figput_figname_str } |
| 191 |
|
| 192 |
% And a boolean for whether the tikz is 'done.' |
| 193 |
\bool_if:NTF \l_figput_done_bool |
| 194 |
{ |
| 195 |
\str_put_right:Nx \l_figput_filespec_str { ~ true } |
| 196 |
} |
| 197 |
{ |
| 198 |
\str_put_right:Nx \l_figput_filespec_str { ~ false } |
| 199 |
} |
| 200 |
|
| 201 |
% And indicate that this function must appear in an external file. |
| 202 |
% It is not (false) to be loaded from a seperate file. |
| 203 |
\str_put_right:Nx \l_figput_filespec_str { ~ false } |
| 204 |
|
| 205 |
% String is ready. Write out the line. |
| 206 |
\iow_now:Nx \g_figurefile_iow { \l_figput_filespec_str } |
| 207 |
|
| 208 |
% That's it for the figures.aux file. Next is the tikz. |
| 209 |
% Above is identical to the environment code, but the following differs |
| 210 |
% slightly since there is no body. |
| 211 |
\bool_set_true:N \l_figput_readtikz_bool |
| 212 |
|
| 213 |
\bool_if:NTF \l_figput_skip_bool |
| 214 |
{ |
| 215 |
\bool_set_false:N \l_figput_readtikz_bool |
| 216 |
} |
| 217 |
{} |
| 218 |
|
| 219 |
% In the environment definition, this was part of the "post body" |
| 220 |
% set of commands. |
| 221 |
\bool_if:NTF \l_figput_nostatic_bool |
| 222 |
{ |
| 223 |
% nostatic is set, so do nothing at all -- no figure in the static version. |
| 224 |
} |
| 225 |
{ |
| 226 |
% nostatic is not set, so there is a figure. |
| 227 |
\str_set_eq:NN \l_figput_tikzfile_str \l_figput_figname_str |
| 228 |
\str_put_right:Nn \l_figput_tikzfile_str { .tikz } |
| 229 |
|
| 230 |
% Whether to read the tikz file or display a "not available" message |
| 231 |
% depends on whether the tikz file exists and whether skip was set. |
| 232 |
\file_if_exist:VTF \l_figput_tikzfile_str |
| 233 |
{ |
| 234 |
% Yes, tikz exists, although we may not read it if skip is set. |
| 235 |
} |
| 236 |
{ |
| 237 |
% Tikz file doesn't exist, so we can't read it in any case. |
| 238 |
\bool_set_false:N \l_figput_readtikz_bool |
| 239 |
} |
| 240 |
|
| 241 |
\bool_if:NTF \l_figput_readtikz_bool |
| 242 |
{ |
| 243 |
% Load the tikz file here. |
| 244 |
\file_input:V \l_figput_tikzfile_str |
| 245 |
} |
| 246 |
{ |
| 247 |
% Not loading the tikz file. Display a big empty box. |
| 248 |
\begin{tikzpicture} |
| 249 |
\useasboundingbox (0pt,0pt) rectangle (\textwidth,\l_figput_height_dim); |
| 250 |
\draw[dashed] (0pt,0pt) rectangle ( \textwidth,\l_figput_height_dim); |
| 251 |
\node at (\textwidth / 2,\l_figput_height_dim / 2) {The\ drawing\ is\ not\ available\ to\ load.}; |
| 252 |
\draw (5pt,5pt) rectangle ( \textwidth - 5,\l_figput_height_dim - 5); |
| 253 |
\end{tikzpicture} |
| 254 |
} |
| 255 |
|
| 256 |
% End of figure inclusion -- nostatic is not set. |
| 257 |
} |
| 258 |
|
| 259 |
% Note the figures location as of after the figure. |
| 260 |
\par\zsaveposy { \l_figput_figname_str } |
| 261 |
\zlabel{ \l_figput_figname_str -pageno} |
| 262 |
|
| 263 |
\group_end: |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
% See xparse for the top-level definition, \NewDocumentEnvironment and |
| 269 |
% its use of \SplitArgument. The gist is that there will be two |
| 270 |
% arguments, one for the manditory items (m), given in {}, and one for |
| 271 |
% the optional arguments (o), given in []. |
| 272 |
% |
| 273 |
% In theory, it is possible to take the body of the environment as an |
| 274 |
% argument, but that won't work if you want to receive the body |
| 275 |
% verbatim. Somehow, using xsim allows the body to be taken verbatim. |
| 276 |
% |
| 277 |
% Note also the ! that appears before the o. This is explained in the |
| 278 |
% xparse documentation. Without the !, if the user does not provide |
| 279 |
% any optional arguments, then any leading space in the body will be |
| 280 |
% eaten and disappear. |
| 281 |
|
| 282 |
\NewDocumentEnvironment{figput} { > { \SplitArgument { 1 } { , } } m!o } |
| 283 |
{ |
| 284 |
% I don't understand latex's concept of local variable, but |
| 285 |
% this makes what follows local in some sense. |
| 286 |
% BUG: Does this even matter? It looks like all my variables are in |
| 287 |
% the figput module. |
| 288 |
\group_begin: |
| 289 |
|
| 290 |
% The manditory arguments come in in #1, but they have been |
| 291 |
% broken up so that #1 "reads as" {first arg}{second arg}. By |
| 292 |
% passing this to another function, it automatically breaks this |
| 293 |
% into two distinct arguments. This seems a silly way, but it works. |
| 294 |
% This sets \l_figput_figname_str and \l_figput_height_dim. |
| 295 |
\figput_manargs #1 |
| 296 |
|
| 297 |
% Now the optional arguments. Start off with some default values. |
| 298 |
% It looks like the htAbove and htBelow values are blank by default, |
| 299 |
% which is what I want. |
| 300 |
\bool_set_false:N \l_figput_hasintht_bool |
| 301 |
\bool_set_false:N \l_figput_nostatic_bool |
| 302 |
\bool_set_false:N \l_figput_done_bool |
| 303 |
\bool_set_false:N \l_figput_skip_bool |
| 304 |
|
| 305 |
% Only try to set these values if there *were* some optional arguments, |
| 306 |
% which come in as the #2 argument to figput. |
| 307 |
% I set the clist here and "pass" it as global variable since I can't |
| 308 |
% figure out how to pass the #2 argument directly. This works, but it |
| 309 |
% is ugly. So this does nothing if the #2 argument is empty, leaving |
| 310 |
% the boolean values as set above. Otherwise, it reads in the |
| 311 |
% height adjustments and sets the boolean values above. |
| 312 |
\tl_if_blank:nTF { #2 } {} { |
| 313 |
\clist_set:Nn \l_figput_optclist_clist { #2 } |
| 314 |
\figput_optargs { } |
| 315 |
} |
| 316 |
|
| 317 |
% We have everything needed to know what to do. Prepare to write the |
| 318 |
% data to the figures file. It seems that the easiest way to do this |
| 319 |
% is to build up the string, then write it out. Getting spaces right |
| 320 |
% is hard when I tried to write each bit directly to the file. |
| 321 |
% Unravelling what's actually written and its meaning is hard to do |
| 322 |
% here. It's easier to understand the js code that reads this output. |
| 323 |
|
| 324 |
% Need the position on the page. This saves it (for the next run). |
| 325 |
% Just as for the page number (see below), it turns out that this |
| 326 |
% shouldn't be done here; if it is, the figures at the bottom of the page are |
| 327 |
% improperly positioned. This is done *after* the environment closes. |
| 328 |
%\zsaveposy { \l_figput_figname_str } |
| 329 |
|
| 330 |
% And this gets the value from the previous run. |
| 331 |
% This value is relative to the bottom of the page (normal axes) |
| 332 |
% See below. The possibility of a figure at the bottom of the page |
| 333 |
% requires a bit of trickery. |
| 334 |
%\dim_set:Nn \l_figput_pagepos_dim { \zposy { \l_figput_figname_str } sp} |
| 335 |
|
| 336 |
% Getting the page number correct is fiddly. We need to place a |
| 337 |
% label at this location, and this label is updated at shipout. |
| 338 |
% Because I am using the figure name as the label for the vertical |
| 339 |
% position, I add a suffix ("-pageno") to distinguish this label. |
| 340 |
% Originally, I had this line here, but that doesn't work because |
| 341 |
% the page number is that at which the environment "opens" rather |
| 342 |
% than when it "closes." If the figure is at the bottom of the page, |
| 343 |
% then it seems that you get the page prior to the one you want. |
| 344 |
%\zlabel{ \l_figput_figname_str -pageno} |
| 345 |
|
| 346 |
% I would have thought that saying |
| 347 |
% \zref[abspage]{ \l_figput_figname_str } |
| 348 |
% would work, but it's not properly expanded. It *does* work to say |
| 349 |
% the above and have it appear as a number on the page. I don't fully |
| 350 |
% understand what the bit below does, but it works. |
| 351 |
\str_set:Nx \l_figput_filespec_str { |
| 352 |
\use:c { zref@extractdefault } { \l_figput_figname_str -pageno} { abspage } { 0 } |
| 353 |
} |
| 354 |
|
| 355 |
% The inner and outer margins. |
| 356 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_innermargin_dim } |
| 357 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_outermargin_dim } |
| 358 |
|
| 359 |
% And the text width. |
| 360 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \textwidth } |
| 361 |
|
| 362 |
% Use tilde (~) to get a space in the string, and convert to bp (big |
| 363 |
% points). Fortunately, this eliminates the "bp" suffix too. |
| 364 |
% There is a problem here, related to whether the figure is at the |
| 365 |
% bottom of the page and gets bumped to the next page. |
| 366 |
% I want to know the location of the top of the figure relative to |
| 367 |
% the page on which it appears. The way to get the position on the |
| 368 |
% page is with \zsaveposy. The problem is that if I want this |
| 369 |
% location *before* the figure, then you get the bottom of the text |
| 370 |
% immediately before the figure; if you ask for this location |
| 371 |
% *after* the figure (or from within the figure), then you get the |
| 372 |
% location of the bottom edge of the figure. |
| 373 |
% |
| 374 |
% The solution is to get the location of the bottom edge of the |
| 375 |
% figure, then subtract the known figure height -- actually we *add* |
| 376 |
% because we are in "normal" coordinates. We want to move up on the |
| 377 |
% page. |
| 378 |
% |
| 379 |
% So, get the value from the previous run, in sp (small or scaled points). |
| 380 |
\tl_set:Nn \l_figput_tempa_tl { \zposy { \l_figput_figname_str } } |
| 381 |
% Convert the figure height to sp. |
| 382 |
\tl_set:Nn \l_figput_tempb_tl { \dim_to_decimal_in_sp:n{ \l_figput_height_dim } } |
| 383 |
% Add |
| 384 |
\tl_set:Nn \l_figput_tempc_tl { \int_eval:n { \l_figput_tempa_tl + \l_figput_tempb_tl } } |
| 385 |
% Convert to a dim |
| 386 |
\dim_set:Nn \l_figput_pagepos_dim { \l_figput_tempc_tl sp } |
| 387 |
% And (whew!) write it out, in bp. |
| 388 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_pagepos_dim } |
| 389 |
|
| 390 |
% The latex height of the figure (in bp) |
| 391 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_height_dim } |
| 392 |
|
| 393 |
% The interactive height paddings, which are zero by default. |
| 394 |
\bool_if:NTF \l_figput_hasintht_bool |
| 395 |
{ |
| 396 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_htAbove_dim } |
| 397 |
\str_put_right:Nx \l_figput_filespec_str { ~ \dim_to_decimal_in_bp:n \l_figput_htBelow_dim } |
| 398 |
} |
| 399 |
{ |
| 400 |
\str_put_right:Nx \l_figput_filespec_str { ~0~0 } |
| 401 |
} |
| 402 |
|
| 403 |
% The figure name -- used for the JS function to call. |
| 404 |
\str_put_right:Nx \l_figput_filespec_str { ~ \l_figput_figname_str } |
| 405 |
|
| 406 |
% And a boolean |
| 407 |
\bool_if:NTF \l_figput_done_bool |
| 408 |
{ |
| 409 |
\str_put_right:Nx \l_figput_filespec_str { ~ true } |
| 410 |
} |
| 411 |
{ |
| 412 |
\str_put_right:Nx \l_figput_filespec_str { ~ false } |
| 413 |
} |
| 414 |
|
| 415 |
% We're making a seperate file for this particular function. |
| 416 |
% Tell the browser (true) to load it. |
| 417 |
\str_put_right:Nx \l_figput_filespec_str { ~ true } |
| 418 |
|
| 419 |
% String is ready. Write out the line. |
| 420 |
\iow_now:Nx \g_figurefile_iow { \l_figput_filespec_str } |
| 421 |
|
| 422 |
% That's it for the fig.aux file. Next is the heavy lifting, |
| 423 |
% making the environment shift blocks of text around. Exactly what |
| 424 |
% to do depends on the optional flags. |
| 425 |
% |
| 426 |
% Ultimately, there are only two things we might do (plus nothing): |
| 427 |
% write the body of the environment to a file, and/or read a tikz |
| 428 |
% file in to replace the current body (which may be blank). The |
| 429 |
% easiest way to manage these options is to set a couple of booleans |
| 430 |
% to indicate whether we should do these two things. |
| 431 |
\bool_set_true:N \l_figput_writebody_bool |
| 432 |
\bool_set_true:N \l_figput_readtikz_bool |
| 433 |
|
| 434 |
% Note that there is no need to check nostatic here. |
| 435 |
|
| 436 |
\bool_if:NTF \l_figput_skip_bool |
| 437 |
{ |
| 438 |
% skip == true, so don't write body or read tikz. |
| 439 |
\bool_set_false:N \l_figput_writebody_bool |
| 440 |
\bool_set_false:N \l_figput_readtikz_bool |
| 441 |
} |
| 442 |
{} |
| 443 |
|
| 444 |
\bool_if:NTF \l_figput_writebody_bool |
| 445 |
{ |
| 446 |
% Yes, we are to write the body to an external file. |
| 447 |
|
| 448 |
% The output file name needs an extra ".fjs" on the end. |
| 449 |
% I don't like using this suffix, but it's seems the least bad. |
| 450 |
\str_set_eq:NN \l_figput_jinfile_str \l_figput_figname_str |
| 451 |
\str_put_right:Nn \l_figput_jinfile_str { .fjs } |
| 452 |
|
| 453 |
% Whether to add an additional EOL to the output file depends on |
| 454 |
% whether there were any optional arguments. That's what the boolean |
| 455 |
% argument does here. It tells the writer whether to start off with |
| 456 |
% an "extra" EOL. |
| 457 |
\IfValueTF {#2} |
| 458 |
{ \xsim_file_write_start:nn { \c_true_bool } } |
| 459 |
{ \xsim_file_write_start:nn { \c_false_bool } } |
| 460 |
{ \l_figput_jinfile_str } |
| 461 |
} |
| 462 |
{ |
| 463 |
% Do not write the body to an external file, but it still needs |
| 464 |
% to be "eaten" and thrown away; otherwise, it passes through |
| 465 |
% and latex tries to make the JS code into part of the document. |
| 466 |
% This uses the verbatim package. |
| 467 |
\comment |
| 468 |
} |
| 469 |
}{ |
| 470 |
% Post environment commands. Remember that the "execution" of an |
| 471 |
% environment is divided into two parts. |
| 472 |
\bool_if:NTF \l_figput_writebody_bool |
| 473 |
{ |
| 474 |
% Stop writing the body since it's done. |
| 475 |
\xsim_file_write_stop: |
| 476 |
} |
| 477 |
{ |
| 478 |
\endcomment |
| 479 |
} |
| 480 |
|
| 481 |
\bool_if:NTF \l_figput_nostatic_bool |
| 482 |
{ |
| 483 |
% nostatic is set, so do nothing at all -- no figure in the static version. |
| 484 |
} |
| 485 |
{ |
| 486 |
% nostatic is not set, so there is a figure. |
| 487 |
|
| 488 |
% And replace the current body with an external tikz file. |
| 489 |
\str_set_eq:NN \l_figput_tikzfile_str \l_figput_figname_str |
| 490 |
\str_put_right:Nn \l_figput_tikzfile_str { .tikz } |
| 491 |
|
| 492 |
% Whether to read the tikz file or display a "not available" message |
| 493 |
% depends on whether the tikz file exists and whether skip was set. |
| 494 |
\file_if_exist:VTF \l_figput_tikzfile_str |
| 495 |
{ |
| 496 |
% Yes, tikz exists, although we may not read it if skip is set. |
| 497 |
} |
| 498 |
{ |
| 499 |
% Tikz file doesn't exist, so we can't read it in any case. |
| 500 |
\bool_set_false:N \l_figput_readtikz_bool |
| 501 |
} |
| 502 |
|
| 503 |
\bool_if:NTF \l_figput_readtikz_bool |
| 504 |
{ |
| 505 |
% Load the tikz file here. |
| 506 |
\file_input:V \l_figput_tikzfile_str |
| 507 |
} |
| 508 |
{ |
| 509 |
% Not loading the tikz file. Display a big empty box. |
| 510 |
% The problem here is that I need to specify the size of the |
| 511 |
% drawing, and tizk doesn't make that easy. It prefers to look at |
| 512 |
% the drawing and determine the size based on what was actually |
| 513 |
% drawn. It seems as though it is possible to *scale* a drawing to |
| 514 |
% take a particular size, although simply specifying the size is |
| 515 |
% hard. What does seem to work is one of the two following approaches. |
| 516 |
% (1) If you want to "trick" latex into allocating a given area |
| 517 |
% for the drawing, even if the drawing ends up going "outside the |
| 518 |
% lines," then you can say (within the tikzpicture environment) |
| 519 |
% \useasboundingbox (0pt,0pt) rectangle (100pt,200pt); |
| 520 |
% or whatever the dimensions are. Use this on the first line. |
| 521 |
% (2) This idea doesn't so much trick latex into using a particular |
| 522 |
% vbox/hbox for the drawing as limit the drawing to the rectangle |
| 523 |
% you want by clipping it. Here, as the first line of the drawing |
| 524 |
% you say |
| 525 |
% \clip (0,0) rectangle (100pt,200pt); |
| 526 |
% and the drawing will be limited to that region. I suspect (?) |
| 527 |
% that if the actual drawing is smaller than the clip region, the |
| 528 |
% latex will reduce the size of the figure accordiningly. So (1) |
| 529 |
% is probably the better solution. |
| 530 |
|
| 531 |
\begin{tikzpicture} |
| 532 |
% The opposite corner is given by (width,height), and the width |
| 533 |
% doesn't really matter since I assume that the figure spans the page. |
| 534 |
% BUG: Maybe I should change this to bp? |
| 535 |
% BUG: The right thing to do here is (?) to make the box match |
| 536 |
% the settings for \l_figput_innermargin_dim and outermargin, but |
| 537 |
% I think this requires that I know the page width. |
| 538 |
\useasboundingbox (0pt,0pt) rectangle (\textwidth,\l_figput_height_dim); |
| 539 |
|
| 540 |
% I don't understand exactly how the figure is placed on the |
| 541 |
% page, but this seems to work. |
| 542 |
\draw[dashed] (0pt,0pt) rectangle ( \textwidth,\l_figput_height_dim); |
| 543 |
|
| 544 |
% It looks like the text is drawn so that the bounding box is |
| 545 |
% centered at the given coordinates. Note how tikz lets you |
| 546 |
% divide with a simple "/2". Nice. |
| 547 |
% |
| 548 |
% Note also that, because this is being done with \ExplSyntaxOn, |
| 549 |
% the spaces in the text need to be \-spaces. Also, if I say |
| 550 |
% \node[draw], then a box is drawn around the text. |
| 551 |
\node at (\textwidth / 2,\l_figput_height_dim / 2) {The\ drawing\ is\ not\ available\ to\ load.}; |
| 552 |
|
| 553 |
% For visual appeal, an extra enclosing rectangle. |
| 554 |
% BUG: I might want to check how tall the figure is and adjust |
| 555 |
% this box accordingly, or not show it at all if the box is very small. |
| 556 |
\draw (5pt,5pt) rectangle ( \textwidth - 5,\l_figput_height_dim - 5); |
| 557 |
|
| 558 |
\end{tikzpicture} |
| 559 |
} |
| 560 |
|
| 561 |
% End of figure inclusion -- nostatic is not set. |
| 562 |
} |
| 563 |
|
| 564 |
% See above. I originally had these lines near the start of the |
| 565 |
% process, but I had to put it here, as the very last thing, for the |
| 566 |
% values to be correct. Otherwise figures at the very end of a |
| 567 |
% page are assigned the page number prior to what it should be, and |
| 568 |
% they end up on the bottom of that page. |
| 569 |
% |
| 570 |
% Wow, I really don't understand this, but somehow, adding the \par |
| 571 |
% here serves to "quiet" another \par that would be inserted and |
| 572 |
% that I do not want. Without this, it seems like there is an extra |
| 573 |
% \par following the .tikz file when one is loaded. This solution |
| 574 |
% was provided as the answer to my question 643125 on tex.stackexchagne. |
| 575 |
% Apparently, it has something to do with \zsaveposy being a |
| 576 |
% "whatsit" command. I haven't unravelled this fully, but it somehow |
| 577 |
% explains what is going on. |
| 578 |
\par\zsaveposy { \l_figput_figname_str } |
| 579 |
\zlabel{ \l_figput_figname_str -pageno} |
| 580 |
|
| 581 |
% Note that group_end occurs here. It can't appear above, in the |
| 582 |
% pre-environment stuff because we still need access to some of the |
| 583 |
% variables here, post-environment. |
| 584 |
\group_end: |
| 585 |
} |
| 586 |
|
| 587 |
% I wanted to define this with \cs_new, but lost patience with it. |
| 588 |
% This works, but it's probably not "the right way." |
| 589 |
\NewDocumentCommand{\figput_manargs}{ m m } |
| 590 |
{ |
| 591 |
% You need braces around #1 or you only get the first character. |
| 592 |
\str_set:Nn \l_figput_figname_str { #1 } |
| 593 |
\dim_set:Nn \l_figput_height_dim { #2 } |
| 594 |
} |
| 595 |
|
| 596 |
% This is to handle the optional arguments. It merely sets the |
| 597 |
% relevant variables. |
| 598 |
\NewDocumentCommand{\figput_optargs} { } |
| 599 |
{ |
| 600 |
% Certain things are hard to do using a clist, so it's held as a seq |
| 601 |
% too. Again, no doubt there is a better way, but this works. |
| 602 |
\seq_set_from_clist:NN \l_figput_optseq_seq { \l_figput_optclist_clist } |
| 603 |
|
| 604 |
% The first of the optional arguments might be a length. If it is, then |
| 605 |
% we have two lengths (dims), for the additional space above/below when |
| 606 |
% the figure is viewed interactively. |
| 607 |
% |
| 608 |
% Note that one could make the call directly from the entire set of |
| 609 |
% optional arguments with |
| 610 |
%\figput_if_length:eTF { \clist_item:nn { #2 } { 1 } } {TRUE}{FALSE} \\ |
| 611 |
% However (!) this only works if the function if_length is defined |
| 612 |
% with \prg_new_conditional:Nnn rather than |
| 613 |
% \prg_new_protected_conditional. I have no idea what the difference |
| 614 |
% is. |
| 615 |
% |
| 616 |
% Pull out the first of the optional arguments to make it easier to |
| 617 |
% work with. Note that by using a copy of the data in a seq variable, |
| 618 |
% I can pop things off without worrying about side-effects. |
| 619 |
\seq_pop_left:NN \l_figput_optseq_seq \l_figput_firstvalue_tl |
| 620 |
|
| 621 |
% If the first value parses out to be a dimension, then note that |
| 622 |
% value as the height above and get the height below too. |
| 623 |
\figput_if_length:VTF \l_figput_firstvalue_tl |
| 624 |
{ |
| 625 |
\dim_set:Nn \l_figput_htAbove_dim \l_figput_firstvalue_tl |
| 626 |
\bool_set_true:N \l_figput_hasintht_bool |
| 627 |
|
| 628 |
% Get the second value (reusing the "firstvalue" variable). |
| 629 |
\seq_pop_left:NN \l_figput_optseq_seq \l_figput_firstvalue_tl |
| 630 |
\dim_set:Nn \l_figput_htBelow_dim \l_figput_firstvalue_tl |
| 631 |
} |
| 632 |
{ |
| 633 |
% If it's not a dimension, then ignore it for now. |
| 634 |
} |
| 635 |
|
| 636 |
% Check whether each possible boolean flag has been set. |
| 637 |
% BUG: I should check whether the user passed in something |
| 638 |
% spurious, and he might accidentally indicate the interactive |
| 639 |
% heights out of order. As things stand now, the user could pass in |
| 640 |
% "gibberish" as an option and it would be ignored. No harm done, |
| 641 |
% but it could confuse the user. |
| 642 |
\clist_if_in:NnTF \l_figput_optclist_clist { nostatic } |
| 643 |
{ |
| 644 |
\bool_set_true:N \l_figput_nostatic_bool |
| 645 |
}{} |
| 646 |
\clist_if_in:NnTF \l_figput_optclist_clist { done } |
| 647 |
{ |
| 648 |
\bool_set_true:N \l_figput_done_bool |
| 649 |
}{} |
| 650 |
\clist_if_in:NnTF \l_figput_optclist_clist { skip } |
| 651 |
{ |
| 652 |
% Note the test for whether \NeverSkip was called. |
| 653 |
\bool_if:NTF \l_figput_skipoff_bool |
| 654 |
{}{ |
| 655 |
\bool_set_true:N \l_figput_skip_bool |
| 656 |
} |
| 657 |
}{} |
| 658 |
} |
| 659 |
|
| 660 |
% This was a "top-level" command, defined with |
| 661 |
%\NewDocumentCommand{\iflengthTF}{mmm} |
| 662 |
% but that's really not the way to do this. |
| 663 |
% |
| 664 |
% Instead, we define a function "in the module" (figput), plus a |
| 665 |
% "variant." See p. 32 of the interface3 document. For the initial |
| 666 |
% function (not the variant), see p. 62. I can't say that I really |
| 667 |
% understand what's going on here, other than a bit of copy and paste |
| 668 |
% and the help from stack overflow does it. |
| 669 |
\prg_new_protected_conditional:Nnn \figput_if_length:n { T, F, TF } |
| 670 |
{ |
| 671 |
\regex_match:nnTF |
| 672 |
% Note that I only allow positive values. |
| 673 |
{ \A [+]? ((\d+(\.\d*)?)|(\.\d+)) \s* (pt|pc|in|bp|cm|mm|dd|cc|sp|ex|em) \Z} |
| 674 |
{ #1 } % test string |
| 675 |
{ \prg_return_true: } |
| 676 |
{ \prg_return_false: } |
| 677 |
} |
| 678 |
|
| 679 |
\prg_generate_conditional_variant:Nnn \figput_if_length:n { V } { T, F, TF } |
| 680 |
|
| 681 |
|
| 682 |
\ExplSyntaxOff |