| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use constant |
| 4 |
Version => '1.03'; |
| 5 |
|
| 6 |
my $help = |
| 7 |
<<'DOC' |
| 8 |
= mkpic - interface for making pictures with mfpic |
| 9 |
|
| 10 |
= Synopsis |
| 11 |
mkpic [options] [picfile] |
| 12 |
|
| 13 |
= Options |
| 14 |
-c,--clean remove all but the input file and die |
| 15 |
-p,--pdfsample create pdf file with sample images |
| 16 |
-f,--font=<font command> set default font for labels |
| 17 |
--[no]box produce framed boxes |
| 18 |
-V,--version report version number and die |
| 19 |
-h,--help display help info and die |
| 20 |
--[no]debug display debugging information |
| 21 |
-l,--log=<logfile> file for warning messages |
| 22 |
|
| 23 |
Without an input file, the DATA section is used. |
| 24 |
|
| 25 |
= Command overview |
| 26 |
|
| 27 |
begin name xl yl xmin ymin xmax ymax xlabel ylabel |
| 28 |
end |
| 29 |
stop |
| 30 |
var=value |
| 31 |
# comment |
| 32 |
|
| 33 |
arccst xcenter ycenter xstart ystart theta |
| 34 |
arcset xstart ystart xend yend theta |
| 35 |
arccrtt xcenter ycenter radius theta1 theta2 |
| 36 |
arc3 x1 y1 x2 y2 x3 y3 |
| 37 |
|
| 38 |
xmark [label1] x1 [label2] x2 ... |
| 39 |
Xmark [label1] x1 [label2] x2 ... |
| 40 |
ymark [label1] y1 [label2] y2 ... |
| 41 |
Ymark [label1] y1 [label2] y2 ... |
| 42 |
|
| 43 |
xdrop x y |
| 44 |
ydrop x y |
| 45 |
xydrop x y |
| 46 |
|
| 47 |
arrow x1 y1 x2 y2 label |
| 48 |
label YX x y label |
| 49 |
xlabels YX x y dx label ... |
| 50 |
ylabels YX x y dy label ... |
| 51 |
|
| 52 |
point x1 y1 x2 y2 ... |
| 53 |
dpoint x1 y1 dx1 dy1 ... |
| 54 |
lines x1 y1 x2 y2 ... |
| 55 |
dlines x1 y1 dx1 dy1 ... |
| 56 |
curve x1 y1 x2 y2 ... |
| 57 |
dcurve x1 y1 dx1 dy1 ... |
| 58 |
|
| 59 |
rect x1 y1 x2 y2 |
| 60 |
drect x y dx dy |
| 61 |
dcrect x y dx dy |
| 62 |
crect x1 y1 x2 y2 |
| 63 |
arect xc yc width height theta |
| 64 |
bar x xdev height |
| 65 |
|
| 66 |
func xmin xmax step expression-in-x |
| 67 |
|
| 68 |
grid dx dy xgap ygap |
| 69 |
hatch |
| 70 |
bhat |
| 71 |
ehat |
| 72 |
|
| 73 |
= Description |
| 74 |
mkpic provides an easy interface for making small pictures with mfpic. To |
| 75 |
this end an input file has to be created consisting of commands, one per |
| 76 |
line, with space separated parameters (or you modify the DATA section of |
| 77 |
the mkpic script, which is used if you run it without an input file). For |
| 78 |
an extensive description see the files |mkpicdoc.tex| and |mkpicdoc.pdf|, |
| 79 |
which are part of the distribution. |
| 80 |
|
| 81 |
mkpic produces two files. Assuming an input file named |picfile| defaulting |
| 82 |
to |mkpic| these are: |
| 83 |
|
| 84 |
picfile.mac |
| 85 |
a macro file which will contain |TeX| commands for every picture |
| 86 |
picfile.sty |
| 87 |
a style file for latex, defining the same |TeX| commands for every picture. |
| 88 |
|
| 89 |
With the |--pdfsample| option, two other files are produced: |
| 90 |
|
| 91 |
picfile.pdf |
| 92 |
a PDF file containing all pictures. |
| 93 |
This lets you easily check the results of your designs. |
| 94 |
|
| 95 |
picfile.tex |
| 96 |
the |TeX| source used for creating this PDF file. |
| 97 |
|
| 98 |
In |LaTeX|, you have to include |\usepackage{picfile}| and to include |
| 99 |
commands like |\Fig|/name/ in your source, where /name/ is the |
| 100 |
name you gave one of your pictures in an mkpic |begin| command. |
| 101 |
|
| 102 |
In TeX and ConTeXt, you have to |\input picfile.mac| and to |
| 103 |
include commands like |\Fig|/name/ in your source, where /name/ is |
| 104 |
the name you gave one of your pictures in an mkpic |begin| command. |
| 105 |
|
| 106 |
In TeX, you must use the |\bye| command (/not/ |\end| to finish your TeX source |
| 107 |
|
| 108 |
See the RUNNING section for how to run mkpic and TeX, LaTeX, or ConTeXt. |
| 109 |
|
| 110 |
= Commands |
| 111 |
The source is set up so that it is easy to add your own commands, |
| 112 |
Currently the following commands have been implemented (the arguments |
| 113 |
are not listed here; for those, refer to the SYNOPSIS section): |
| 114 |
|
| 115 |
begin, end |
| 116 |
Every picture begins with the |begin| command and ends with the |end| |
| 117 |
command. The |begin| command defines a name for the picture and |
| 118 |
defines a tex command with that name, prefixed with 'Fig'. The |
| 119 |
resulting command is written to a |.mac| file. Thus the command |
| 120 |
|
| 121 |
begin aa ... |
| 122 |
|
| 123 |
starts writing |\def\Figaa{...}| to the |.mac| file, and the |
| 124 |
picture can be reproduced in a |TeX| document by importing the |.mac| file |
| 125 |
and using the |\Figaa| command. |
| 126 |
|
| 127 |
xl and yl are the lengths of the x- and y-axes. xlabel and ylabel are |
| 128 |
the label that are placed at the ends of those axes. Use a space to |
| 129 |
suppress labeling, or "-" to suppress drawing the axes at all. |
| 130 |
|
| 131 |
stop |
| 132 |
stops further reading of the input. Useful if you have many pictures, |
| 133 |
but want to see only the first few for testing purposes. |
| 134 |
|
| 135 |
var=value |
| 136 |
sets the variable var to value. This variable, or an expression containing it, |
| 137 |
can be used instead of any numerical parameter. Variable names may contain |
| 138 |
lower and uppercase letters, digits or underscores, with the restriction that |
| 139 |
they must start with a letter and may not end in an underscore. |
| 140 |
|
| 141 |
# |
| 142 |
denotes a comment |
| 143 |
|
| 144 |
xmark, ymark, Xmark, Ymark |
| 145 |
These commands place one or more labels along the x- or y-axes, either |
| 146 |
below (|xmark| and |ymark|) or above (|Xmark| and |Ymark|) the axis. |
| 147 |
|
| 148 |
For the |[xXyY]mark| commands a parameter containing any character |
| 149 |
other than [-.0-9] is interpreted as the label (this implies that you |
| 150 |
cannot use expressions here!) to be placed and its position is expected |
| 151 |
in the next parameter. If a parameter is just a number, it is placed at |
| 152 |
that x-position. If you want a number to be interpreted as a label, put |
| 153 |
it in braces: |{1950}|. |
| 154 |
|
| 155 |
arccst |
| 156 |
(Mnemonic: |c|enter |s|tart |t|heta.) |
| 157 |
Draws an arc with its center in |xcenter,ycenter|, starting in |xstart,ystart| |
| 158 |
and with an arc length of |theta| degrees. |
| 159 |
|
| 160 |
arcset |
| 161 |
(Mnemonic: |s|tart |e|nd |t|heta.) |
| 162 |
Draws an arc starting in |xstart,ystart|end ending in |xend,yend| |
| 163 |
and with an arc length of |theta| degrees. |
| 164 |
|
| 165 |
arccrtt |
| 166 |
(Mnemonic: |c|enter |r|adius |t|heta1 |t|heta2.) |
| 167 |
Draws an arc with its center in |xcenter,ycenter|, a radius |radius| |
| 168 |
starting at |theta1| degrees and ending at |theta2| degrees. |
| 169 |
|
| 170 |
arc3 |
| 171 |
(Mnemonic: |3| points.) |
| 172 |
Draws an arc starting at |(x1,y1)|, through |(x2,y2)| and ending in |(x3,y3)|. |
| 173 |
|
| 174 |
xdrop, ydrop, xydrop |
| 175 |
These commands draw dotted arrows perpendicularly to the x-axis, the |
| 176 |
y-axis and both axes, respectively, ending on the axes with the |
| 177 |
arrow head. |
| 178 |
|
| 179 |
arrow |
| 180 |
draws an arrow from |(x1,y1)| to |(x2,y2)| labeled on its tail with |
| 181 |
|label| |
| 182 |
|
| 183 |
label |
| 184 |
draws a label at |(x,y)|. |YX| tells how it will be adjusted: for |
| 185 |
Y=t,b,c |(x,y)| will be, in the y-direction, on top, bottom or center |
| 186 |
of the label respectively, for X=l,r,c it will be, in the x-direction, |
| 187 |
left, right or center adjusted on |(x,y)|. Thus |
| 188 |
|
| 189 |
label tl 0 0 Hello World! |
| 190 |
|
| 191 |
will draw the string "Hello World" with its lower left corner at (0,0) |
| 192 |
|
| 193 |
xlabels |
| 194 |
draws many labels, starting at |(x,y)|, and incrementing |x| with |dx| |
| 195 |
after every label. |YX|: see |label|. Labels may not contain spaces; |
| 196 |
if you need spaces, use - instead. |
| 197 |
|
| 198 |
ylabels |
| 199 |
Same as |xlabels|, but incrementing |y| with |dy| instead. |
| 200 |
|
| 201 |
point |
| 202 |
draws points (dots) at |(x1,y1)|, |(x2,y2)| et cetera. |
| 203 |
|
| 204 |
dpoint |
| 205 |
draws points (dots) starting at |(x1,y1)| and then moving by |
| 206 |
|(dx1,dy1)|, |(dx2,dy2)| et cetera. |
| 207 |
|
| 208 |
lines |
| 209 |
draws line segments from |(x1,y1)| to |(x2,y2)|, |(x3,y3)| et cetera. |
| 210 |
|
| 211 |
dlines |
| 212 |
draws line segments starting at |(x1,y1)| and then moving by |
| 213 |
|(dx1,dy1)|, |(dx2,dy2)| et cetera. |
| 214 |
|
| 215 |
curve |
| 216 |
draws a bezier curve from |(x1,y1)| to |(x2,y2)|, |(x3,y3)| et cetera. |
| 217 |
|
| 218 |
dcurve |
| 219 |
draws a bezier curve starting at |(x1,y1)| and then moving by |
| 220 |
|(dx1,dy1)|, |(dx2,dy2)| et cetera. |
| 221 |
|
| 222 |
rect |
| 223 |
draws a rectangle with diagonal points at |(x1,y1)| and |(x2,y2)|. |
| 224 |
|
| 225 |
drect |
| 226 |
draws a rectangle with diagonal points at |(x,y)| and |(x+dx,y+dy)|. |
| 227 |
|
| 228 |
crect |
| 229 |
clears a rectangle with diagonal points at |(x1,y1)| and |(x2,y2)|. |
| 230 |
|
| 231 |
dcrect |
| 232 |
clears a rectangle with diagonal points at |(x,y)| and |(x+dx,y+dy)|. |
| 233 |
|
| 234 |
arect |
| 235 |
draws a rectangle with a widdh |width| and a height |height|; |
| 236 |
the middle of the bottom is at |(xc,yc)| and the centerline through |
| 237 |
|(xc,yc)| makes an angle |theta| with the x-axis. |
| 238 |
|
| 239 |
bar |
| 240 |
draws a equivalent with |rect x-xdev 0 x+xdev height| |
| 241 |
|
| 242 |
func |
| 243 |
draws the function given by |expression-in-x| between |xmin| and |
| 244 |
|xmax|, stepping with |step| units in the x-direction. |
| 245 |
Note that the |expression-in-x| will be evaluated by |Metafont|, so |
| 246 |
you will have to use metafont syntax. |
| 247 |
|
| 248 |
grid |
| 249 |
draw dotted grid lines at distances dx and dy in the x- and y |
| 250 |
directions; the gaps between the dots are set to |xgap| an |ygap| |
| 251 |
respectively. For an esthetic appearance, be sure to use integer |
| 252 |
|dx/xgap| and |dy/ygap| ratios. |
| 253 |
|
| 254 |
hatch |
| 255 |
hatch the closed curve that follows. |
| 256 |
|
| 257 |
bhat |
| 258 |
starts a path that will eventually be closed, and then hatched. |
| 259 |
|
| 260 |
ehat |
| 261 |
ends a path started with |bhat|, closes it and then hatches it. |
| 262 |
|
| 263 |
anything else |
| 264 |
will be inserted as is in the macro file, and therefore should be a |
| 265 |
valid |mfpic| statement. You use this when you need such a statement |
| 266 |
only once, or a few times and therefore see no need to define a proper |
| 267 |
command for it. |
| 268 |
|
| 269 |
|
| 270 |
= Running mkpic/TeX |
| 271 |
The effect of running |
| 272 |
mkpic picfile |
| 273 |
is the creation of |picfile.mac|, which you can |\input| into a TeX or |
| 274 |
ConTeXt source, and |picfile.sty| which can be input into a LaTeX |
| 275 |
source using the |\usepackage| command. |
| 276 |
|
| 277 |
After running TeX (or LaTeX or ConTeXt), you will find a file |
| 278 |
|picfile.mf| and you will have to run Metafont on it, which |
| 279 |
(assuming you configured TeX for 600 dpi) produces picfile.600gf. This |
| 280 |
file will have to be converted to a pk file with gftopk. Finally, you |
| 281 |
need to run TeX, normally at least twice, again. So for pdfLaTeX the sequence is: |
| 282 |
|
| 283 |
mkpic picfile |
| 284 |
pdflatex file.tex |
| 285 |
mf picfile |
| 286 |
gftopk picfile.600gf |
| 287 |
pdflatex file |
| 288 |
pdflatex file |
| 289 |
|
| 290 |
= Bug |
| 291 |
Currently only up to 256 pictures can be generated. In the future this |
| 292 |
problem will probably be solved by introducing more than one font and |
| 293 |
generating tex-command names that have the font name in front. |
| 294 |
|
| 295 |
= Author |
| 296 |
[Wybo Dekker]([email protected]) |
| 297 |
|
| 298 |
= Copyright |
| 299 |
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html) |
| 300 |
DOC |
| 301 |
; |
| 302 |
|
| 303 |
use vars |
| 304 |
qw/$xgap $ygap $dx $dy $xl $yl $x3 $y3 $height $Z $d $F $r $theta1 $theta2/; |
| 305 |
|
| 306 |
sub evaluate { |
| 307 |
defined or die "Missing parameter at line $.\n"; |
| 308 |
if ( !m|^[0-9.]+$| ) { |
| 309 |
my $x = ''; # we build up the expression with $-ized variable names in $x |
| 310 |
while ($_) { |
| 311 |
unless (/[A-Za-z0-9]+/) { $x .= $_; last } |
| 312 |
my ( $left, $middle, $right ) = ( $`, $&, $' ); |
| 313 |
$x .= $left |
| 314 |
. ( ( $middle =~ m|^[0-9.]+$| || $fun{$middle} ) ? "" : '$' ) |
| 315 |
. $middle; |
| 316 |
$_ = $right; |
| 317 |
} |
| 318 |
$_ = $x; |
| 319 |
eval "\$_=$_" while m|[^0-9.-]|; |
| 320 |
} |
| 321 |
$_; |
| 322 |
} |
| 323 |
|
| 324 |
sub parse { |
| 325 |
my @x; |
| 326 |
( $command_name, @x ) = |
| 327 |
split ( /\s+/, $_, scalar @_ + 1 ); # input values/expressions in @x |
| 328 |
print LOG "command: $command_name\n" if $debug; |
| 329 |
for ( my $i = 0 ; $i < @_ ; $i++ ) { # each variable to be set |
| 330 |
$_ = $x[$i]; # take the expression |
| 331 |
if ( $_[$i] !~ /^[A-Z]/ ) |
| 332 |
{ # if name of receiving var not capitalized: await numerical |
| 333 |
evaluate; |
| 334 |
} |
| 335 |
my $varname = "$_[$i]"; |
| 336 |
$$varname = $_; |
| 337 |
print LOG "$varname=$_\n" if $debug; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
sub parse_array { |
| 342 |
my @x; |
| 343 |
( $command_name, @x ) = split ( /\s+/, $_[0] ); |
| 344 |
print LOG "command: $command_name\n" if $debug; |
| 345 |
unless ( $command_name =~ /mark/ ) { |
| 346 |
for (@x) { |
| 347 |
evaluate; |
| 348 |
} |
| 349 |
} |
| 350 |
print LOG "parse_array returns: @x\n" if $debug; |
| 351 |
@x; |
| 352 |
} |
| 353 |
|
| 354 |
sub open_macrofile { |
| 355 |
my $rerun = 0; |
| 356 |
# we need to rerun if: |
| 357 |
# - one of the needed files is missing or else |
| 358 |
# - the input file is newer than the .mac file or else |
| 359 |
# - the present options differ from/keep the previous ones |
| 360 |
|
| 361 |
# check the presence of needed files: |
| 362 |
chomp( $_ = -f $tex ? $tex : `which mkpic` ); |
| 363 |
my $inputtime = ( stat($_) )[9]; |
| 364 |
for ( "mac", "sty" ) { |
| 365 |
-f "$tex.$_" or $rerun = 1, last; |
| 366 |
} |
| 367 |
# if all are there, test if the input is older than the .mac file: |
| 368 |
!$rerun && ( stat("$tex.mac") )[9] > $inputtime or $rerun = 1; |
| 369 |
|
| 370 |
# if that's true too, check that the previous option were the same: |
| 371 |
unless ($rerun) { |
| 372 |
open( MAC, "$tex.mac" ); |
| 373 |
$_ = <MAC>; |
| 374 |
$_ eq $options or $rerun = 1; |
| 375 |
close MAC; |
| 376 |
} |
| 377 |
$rerun or return (0); |
| 378 |
open( MAC, ">$tex.mac" ); |
| 379 |
select MAC; |
| 380 |
$_ = <<'EOF' |
| 381 |
% vim: syntax=tex |
| 382 |
\input mfpic |
| 383 |
\let\circle\undefinedcs |
| 384 |
\mfpicunit.01mm |
| 385 |
\opengraphsfile{TX} |
| 386 |
\def\ifundefined#1{\expandafter\ifx\csname#1\endcsname\relax} |
| 387 |
\def\doatend{\closegraphsfile} |
| 388 |
\ifundefined{typefile}\else\appendtoks\doatend\to\everystoptext\fi % for context |
| 389 |
\ifundefined{documentclass}\else\AtEndDocument{\doatend}\fi % for latex |
| 390 |
\def\bye{\doatend\par\vfill\supereject\end} % plain |
| 391 |
\def\ruledhbox#1{\vbox{\hrule\hbox{\vrule\kern3pt |
| 392 |
\vbox{\kern3pt#1\kern3pt}\kern3pt\vrule}\hrule}} |
| 393 |
|
| 394 |
EOF |
| 395 |
; |
| 396 |
s/^\s+//gm; |
| 397 |
s/TX/$tex/gm; |
| 398 |
print MAC $options,$_; |
| 399 |
|
| 400 |
open( STY, ">$tex.sty" ) or die "Could not open $tex.sty: $!\n"; |
| 401 |
$_ = <<'EOF' |
| 402 |
\ProvidesPackage{TX} |
| 403 |
\input TX.mac |
| 404 |
\endinput |
| 405 |
EOF |
| 406 |
; |
| 407 |
s/^\s+//gm; |
| 408 |
s/TX/$tex/gm; |
| 409 |
print STY; |
| 410 |
1; |
| 411 |
} |
| 412 |
|
| 413 |
sub sys { |
| 414 |
system("@_") and die "@_: $?\n"; |
| 415 |
} |
| 416 |
|
| 417 |
sub parse_input { |
| 418 |
while (<$input>) { |
| 419 |
chomp; |
| 420 |
s/\s*#.*//; # remove comment |
| 421 |
next unless $_; # skip empty lines |
| 422 |
/^begin/ and do { |
| 423 |
parse qw(Name xl yl xmin ymin xmax ymax Xlabel Ylabel); |
| 424 |
$names{$Name} and die "multiple occurrences of picture named $Name\n"; |
| 425 |
$names{$Name} = 1; |
| 426 |
|
| 427 |
$Xlabel = "" if $Xlabel eq '-'; |
| 428 |
$Ylabel = "" if $Ylabel eq '-'; |
| 429 |
$xscale = int( 100 * $xl / ( $xmax - $xmin ) + .5 ); |
| 430 |
$yscale = int( 100 * $yl / ( $ymax - $ymin ) + .5 ); |
| 431 |
$sx = sprintf( "%.2f", 100 / $xscale ); |
| 432 |
$sy = sprintf( "%.2f", 100 / $yscale ); |
| 433 |
$yx = $ymin > 0 ? $ymin : 0; # y-position of the x-axis |
| 434 |
$xy = $xmin > 0 ? $xmin : 0; # x-position of the y-axis |
| 435 |
$xlo = $xy - $sx; # x-pos of right side of y-markers |
| 436 |
$ylo = $yx - $sy; # y-pos of top side of x-markers |
| 437 |
$xhi = $xmax + 3 * $sx; # x-pos of left side of x-labels |
| 438 |
$yhi = $ymax + 3 * $sy; # y-pos of bottom side of y-labels |
| 439 |
print "%\n%====$Name====\n" |
| 440 |
. "\\def\\Fig$Name\{\\$box\{\\mftitle{$Name}%\n" |
| 441 |
. "\\def\\xlo{$xlo}\\def\\xhi{$xhi}%\n" |
| 442 |
. "\\def\\ylo{$ylo}\\def\\yhi{$yhi}%\n" |
| 443 |
. "\\def\\xy{$xy}\\def\\yx{$yx}%\n" |
| 444 |
. "\\mfpic[$xscale][$yscale]{$xmin}{$xmax}{$ymin}{$ymax}%\n" |
| 445 |
. "\\hatchwd{2}%\n" |
| 446 |
. ( $Xlabel ? "\\tlabel[bc]($xy,$yhi){$font $Ylabel}%\n" : '' ) |
| 447 |
. ( $Ylabel ? "\\tlabel[cl]($xhi,$yx){$font $Xlabel}%\n" : '' ); |
| 448 |
print TEX "\\Fig$Name\\par{\\bf $Name}\\eject\n" if $pdfsample; |
| 449 |
}, next; |
| 450 |
|
| 451 |
/^arccst/ and do { # arc center start theta |
| 452 |
parse qw(xc yc x1 y1 theta); |
| 453 |
print "\\arc[c]{($xc,$yc),($x1,$y1),$theta}\n"; |
| 454 |
}, next; |
| 455 |
|
| 456 |
/^arcset/ and do { # arc start end theta |
| 457 |
parse qw(x1 y1 x2 y2 theta); |
| 458 |
print "\\arc[s]{($x1,$y1),($x2,$y2),$theta}\n"; |
| 459 |
}, next; |
| 460 |
|
| 461 |
/^arccrtt/ and do { # arc center radius theta1 theta2 |
| 462 |
parse qw(xc yc r theta1 theta2); |
| 463 |
print "\\arc[p]{($xc,$yc),$theta1,$theta2,$r}\n"; |
| 464 |
}, next; |
| 465 |
|
| 466 |
/^arc3/ and do { # arc three points |
| 467 |
parse qw(x1 y1 x2 y2 x3 y3); |
| 468 |
print "\\arc[t]{($x1,$y1),($x2,$y2),($x3,$y3)}\n"; |
| 469 |
}, next; |
| 470 |
|
| 471 |
/^arrow/ and do { |
| 472 |
parse qw(x1 y1 x2 y2 Label); |
| 473 |
my $yd = $y1 > $y2 ? -3 : 3; |
| 474 |
print "\\tlabel[bc]($x1,$y1){$font $Label}%\n" |
| 475 |
. "\\arrow\\lines{($x1," |
| 476 |
. ( $y1 + $yd * $sy ) |
| 477 |
. "),($x2,$y2)}%\n"; |
| 478 |
}, next; |
| 479 |
|
| 480 |
/^[xyXY]mark/ and do { |
| 481 |
@z = parse_array($_); |
| 482 |
for ( $i = 0 ; $i < @z ; $i++ ) { |
| 483 |
$label = $z[$i]; |
| 484 |
if ( $label =~ /^[-.\d]+$/ ) { |
| 485 |
$label = "\$$label\$"; |
| 486 |
} else { |
| 487 |
$i++; |
| 488 |
} |
| 489 |
$x = |
| 490 |
$command_name =~ /x/i ? $z[$i] |
| 491 |
: $command_name =~ /y/ ? $xlo |
| 492 |
: -$xlo; |
| 493 |
$y = |
| 494 |
$command_name =~ /y/i ? $z[$i] |
| 495 |
: $command_name =~ /x/ ? $ylo |
| 496 |
: -$ylo; |
| 497 |
print "\\tlabel[$pos{$command_name}]($x,$y)" |
| 498 |
. "{\\rm \\strut $label}%\n"; |
| 499 |
} |
| 500 |
}, next; |
| 501 |
|
| 502 |
/^grid/ and do { |
| 503 |
parse qw(xdist ydist xgap ygap); |
| 504 |
$topmaxy = ( int( $ymax / $ydist ) * $ydist ); |
| 505 |
$lftmaxx = ( int( $xmin / $xdist ) * $xdist ); |
| 506 |
$botmaxy = ( int( $ymin / $ydist ) * $ydist ); |
| 507 |
$rtmaxx = ( int( $xmax / $xdist ) * $xdist ); |
| 508 |
for ( $t = $lftmaxx ; $t <= $rtmaxx ; $t += $xdist ) { |
| 509 |
print |
| 510 |
"\\dotted[.5pt,$xgap mm]\\lines{($t,$botmaxy),($t,$topmaxy)}%\n"; |
| 511 |
} |
| 512 |
for ( $t = $botmaxy ; $t <= $topmaxy ; $t += $ydist ) { |
| 513 |
print |
| 514 |
"\\dotted[.5pt,$ygap mm]\\lines{($lftmaxx,$t),($rtmaxx,$t)}%\n"; |
| 515 |
} |
| 516 |
}, next; |
| 517 |
|
| 518 |
/^bar/ and do { |
| 519 |
parse qw(x xdev height); |
| 520 |
$x1 = $x - $xdev; |
| 521 |
$x2 = $x + $xdev; |
| 522 |
$y1 = 0; |
| 523 |
$y2 = $height; |
| 524 |
print "\\rect{($x1,$y1),($x2,$y2)}%\n"; |
| 525 |
}, next; |
| 526 |
|
| 527 |
/^d?(point|lines|curve|rect|crect)/ and do { |
| 528 |
@z = parse_array($_); |
| 529 |
$command_name =~ /^d/ and do { # relative points |
| 530 |
for ( $i = 3 ; $i < @z ; $i += 2 ) { |
| 531 |
$z[$i] += $z[ $i - 2 ]; |
| 532 |
$z[ $i - 1 ] += $z[ $i - 3 ]; |
| 533 |
} |
| 534 |
$command_name =~ s/^d//; |
| 535 |
}; |
| 536 |
$_ = "$command_name @z"; |
| 537 |
# for example: crect 5 20 20 5 |
| 538 |
s/^/\\/; # \crect 5 20 20 5 |
| 539 |
s/\s+/{(/; # \crect{(5 20 20 5 |
| 540 |
while (/ +/) { |
| 541 |
s/ +([-.\d]+)(\s+)?/,$1),\n(/; |
| 542 |
} # \crect{(5,20),(20,5) |
| 543 |
s/,\n\($/}/; # \crect{(5,20),(20,5)} |
| 544 |
s/crect/gclear\\rect/; |
| 545 |
# \gclear\rect{(5,20),(20,5)} |
| 546 |
print "$_\n"; |
| 547 |
}, next; |
| 548 |
|
| 549 |
/^func/ and do { |
| 550 |
parse qw(x y d F); |
| 551 |
print "\\function{$x,$y,$d}{$F}%\n"; |
| 552 |
}, next; |
| 553 |
|
| 554 |
/^hatch/ and do { |
| 555 |
print '\rhatch\draw\lclosed'; |
| 556 |
}, next; |
| 557 |
|
| 558 |
/^bhat/ and do { |
| 559 |
print '\rhatch\lclosed\connect', "\n"; |
| 560 |
}, next; |
| 561 |
|
| 562 |
/^ehat/ and do { |
| 563 |
print '\endconnect', "\n"; |
| 564 |
}, next; |
| 565 |
|
| 566 |
/^xdrop/ and do { |
| 567 |
parse qw(x y); |
| 568 |
print "\\dotted\\arrow" . "\\lines{($x,$y),($xy,$y)}%\n"; |
| 569 |
}, next; |
| 570 |
|
| 571 |
/^ydrop/ and do { |
| 572 |
parse qw(x y); |
| 573 |
print "\\dotted\\arrow" . "\\lines{($x,$y),($x,$yx)}%\n"; |
| 574 |
}, next; |
| 575 |
|
| 576 |
/^xydrop/ and do { |
| 577 |
parse qw(x y); |
| 578 |
print "\\dashed\\arrow" . "\\lines{($x,$y),($xy,$y)}%\n"; |
| 579 |
print "\\dashed\\arrow" . "\\lines{($x,$y),($x,$yx)}%\n"; |
| 580 |
}, next; |
| 581 |
|
| 582 |
/^xlabels/ and do { |
| 583 |
parse qw(M x y dx Z); |
| 584 |
@z = split ( /\s+/, $Z ); |
| 585 |
for (@z) { |
| 586 |
print "\\tlabel[$M]($x,$y){$font $_}%\n"; |
| 587 |
$x += $dx; |
| 588 |
} |
| 589 |
}, next; |
| 590 |
|
| 591 |
/^ylabels/ and do { |
| 592 |
parse qw(M x y dy Z); |
| 593 |
@z = split ( /\s+/, $Z ); |
| 594 |
for (@z) { |
| 595 |
print "\\tlabel[$M]($x,$y){$font $_}%\n"; |
| 596 |
$y += $dy; |
| 597 |
} |
| 598 |
}, next; |
| 599 |
|
| 600 |
/^label/ and do { |
| 601 |
parse qw(M x y Label); |
| 602 |
$M =~ /^[bct][lcr]$/ or die "illegal label in $_\n"; |
| 603 |
print "\\tlabel[$M]($x,$y){$font $Label}%\n"; |
| 604 |
}, next; |
| 605 |
|
| 606 |
/^end/ and do { |
| 607 |
# axes drawn last for easier rect clears: |
| 608 |
print "\\arrow[\\axisheadlen]\\lines" . "{($xmin,$yx),($xmax,$yx)}%\n" |
| 609 |
if $Xlabel; |
| 610 |
print "\\arrow[\\axisheadlen]\\lines" . "{($xy,$ymin),($xy,$ymax)}%\n" |
| 611 |
if $Ylabel; |
| 612 |
print "\\endmfpic}}%\n"; |
| 613 |
}, next; |
| 614 |
|
| 615 |
/^arect/ and do { |
| 616 |
parse qw/xc yc w h theta/; |
| 617 |
$theta *= 3.141592 / 180; |
| 618 |
$ws = $w / 2 * sin($theta); |
| 619 |
$wc = $w / 2 * cos($theta); |
| 620 |
$hs = $h * sin($theta); |
| 621 |
$hc = $h * cos($theta); |
| 622 |
print "\\lines{($xc-$ws,$yc+$wc),($xc-$ws+$hc,$yc+$wc+$hs),($xc+$ws+$hc,$yc-$wc+$hs),($xc+$ws,$yc-$wc),($xc-$ws,$yc+$wc)}\n"; |
| 623 |
}, next; |
| 624 |
|
| 625 |
/^(\w+)\s*=/ and do { |
| 626 |
$var = $1; |
| 627 |
evaluate; |
| 628 |
print LOG "$var=$$var\n" if $debug; |
| 629 |
}, next; |
| 630 |
|
| 631 |
/^stop/ and do { last }; # stop reading the input |
| 632 |
|
| 633 |
print "$_\n" if $_; # anything else printed literally |
| 634 |
|
| 635 |
} |
| 636 |
print MAC "\\endinput\n"; |
| 637 |
close(MAC); |
| 638 |
if ($pdfsample) { |
| 639 |
print TEX "\\bye\n"; |
| 640 |
close(TEX); |
| 641 |
sys("pdftex --interaction batchmode $tex 2>/dev/null"); |
| 642 |
sys("mf $tex; gftopk $tex.${dpi}gf"); |
| 643 |
sys("pdftex --interaction batchmode $tex 2>/dev/null"); |
| 644 |
for ( ( "log", "mf", "tfm", "${dpi}pk", "${dpi}gf", ) ) { |
| 645 |
unlink "$tex.$_"; |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
sub help { undef $/; open(FH,$0); print "Usage:".[split(/= .*\n/,<FH>)]->[3]; exit 1 } |
| 651 |
sub install { system('instscript --zip --extras=mkpicdoc.tex,mkpicdoc.pdf mkpic'); exit 0; } |
| 652 |
|
| 653 |
use Getopt::Long; |
| 654 |
Getopt::Long::Configure("bundling_override"); |
| 655 |
|
| 656 |
GetOptions( |
| 657 |
'c|clean' => \$clean, # remove all but input file and don't run |
| 658 |
'p|pdfsample' => \$pdfsample, # create pdf file with sample images |
| 659 |
'b|box!' => \$box, # produce framed boxes |
| 660 |
'f|font=s' => \$font, # default font instead of rm |
| 661 |
'V|version' => \$version, # report version number and die |
| 662 |
'h|help' => \&help, # display help info and die |
| 663 |
'd|debug!' => \$debug, # print debug information |
| 664 |
'l|log=s' => \$log, # file for warning messages |
| 665 |
'I' => \&install # not for general use |
| 666 |
); |
| 667 |
|
| 668 |
if ($version) { print Version."\n"; exit; }; |
| 669 |
$font = $font || '\rm'; |
| 670 |
$box ||= ''; |
| 671 |
$pdfsample ||= 0; |
| 672 |
if ($log) { |
| 673 |
open( LOG, ">>$log" ) or die "Could not open $log for output: $!\n"; |
| 674 |
} else { |
| 675 |
*LOG = *STDERR; |
| 676 |
} |
| 677 |
|
| 678 |
# find printer density |
| 679 |
if ( open( IN, `kpsewhich --format="web2c files" mktex.cnf` ) ) { |
| 680 |
while (<IN>) { |
| 681 |
/BDPI=(\d+)/ and $dpi = $1, last; |
| 682 |
} |
| 683 |
} else { |
| 684 |
print LOG "could not find mktex.cnf; forcing 600dpi\n"; |
| 685 |
$dpi = 600; |
| 686 |
} |
| 687 |
|
| 688 |
# we'll place the options in the .mac file so that, in the next run, |
| 689 |
# we can see if the previous run had the same options; if so and if |
| 690 |
# the input file is older than the .mac file, we won't rerun mkpic: |
| 691 |
$options = "% font:$font box:$box dpi=$dpi pdfsample:$pdfsample\n"; |
| 692 |
|
| 693 |
$tex = shift; |
| 694 |
if ($tex) { |
| 695 |
open( IN, $tex ) or die "Could not open $tex\n"; |
| 696 |
$input = *IN; |
| 697 |
} else { |
| 698 |
$input = *DATA; |
| 699 |
$tex = 'mkpic'; |
| 700 |
} |
| 701 |
|
| 702 |
$clean and do { |
| 703 |
for ( eval "qw(mf ${dpi}gf ${dpi}pk log dvi pdf mac tex tfm sty)" ) { |
| 704 |
unlink "$tex.$_"; |
| 705 |
} |
| 706 |
exit 0; |
| 707 |
}; |
| 708 |
|
| 709 |
open_macrofile() or die "mkpic: no new run needed\n"; |
| 710 |
print LOG "Mkpic Warning: Rerun (La)TeX\n" if $log; |
| 711 |
|
| 712 |
$box = $box ? 'ruledhbox' : 'hbox'; |
| 713 |
if ($pdfsample) { |
| 714 |
open( TEX, ">$tex.tex" ); |
| 715 |
($_ = <<EOF ) =~ s/^\s+//gm; |
| 716 |
\\input $tex.mac |
| 717 |
\\parindent0pt\\parskip2ex |
| 718 |
\\nopagenumbers\\vsize=260mm |
| 719 |
EOF |
| 720 |
print TEX; |
| 721 |
} |
| 722 |
|
| 723 |
%pos = ( |
| 724 |
'xmark' => 'tc', # positions for [xyXY]marks |
| 725 |
'ymark' => 'cr', |
| 726 |
'Xmark' => 'bc', |
| 727 |
'Ymark' => 'cl' |
| 728 |
); |
| 729 |
%fun = ( |
| 730 |
sin => 1, |
| 731 |
cos => 1, |
| 732 |
sqrt => 1, |
| 733 |
atan2 => 1, |
| 734 |
abs => 1, |
| 735 |
exp => 1, |
| 736 |
int => 1, |
| 737 |
log => 1, |
| 738 |
not => 1 |
| 739 |
); |
| 740 |
|
| 741 |
%names = (); |
| 742 |
|
| 743 |
parse_input; |
| 744 |
|
| 745 |
__DATA__ |
| 746 |
l=20 |
| 747 |
begin steps l l 0 0 l l - - |
| 748 |
\pen{2pt} |
| 749 |
dlines 10 10 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 |
| 750 |
end |
| 751 |
|
| 752 |
begin rectangle 64 64 0 0 10 10 $x$ $y$ |
| 753 |
arect 5 5 3 2 60 |
| 754 |
lines 0 0 10 10 0 10 10 0 |
| 755 |
end |
| 756 |
|
| 757 |
begin parabola 64 64 0 -6 4 5 $x$ $y$ |
| 758 |
xmark 2 |
| 759 |
Xmark 3 |
| 760 |
ymark -5 0 4 |
| 761 |
bhat |
| 762 |
lines 0 0 0 4 |
| 763 |
func 0 3 .05 4-x*x |
| 764 |
lines 3 -5 3 0 |
| 765 |
ehat |
| 766 |
func 0 3.2 .05 4-x*x |
| 767 |
xydrop 3 -5 |
| 768 |
arrow 3 4 1.7 1.1 $f(x)=4-x^2$ |
| 769 |
arrow 2 5 1 2 Area $O_1$ |
| 770 |
arrow 4 2 2.8 -2 Area $O_2$ |
| 771 |
end |
| 772 |
|
| 773 |
begin droparrows 64 64 0 3 12 8 $x$ $y$ |
| 774 |
xmark $a$ 2 $x_1$ 4 $x_2$ 8 $b$ 10 |
| 775 |
ydrop 2 4.414 |
| 776 |
ydrop 4 5 |
| 777 |
ydrop 8 5.828 |
| 778 |
ydrop 10 6.162 |
| 779 |
label cc 4 4 $f(x_1)$ |
| 780 |
label cc 8 4 $f(x_2)$ |
| 781 |
label cc 7 8 $f(x)$ increasing on $[a,b]$ |
| 782 |
label cc 7 7.5 $x_1<x_2\Rightarrow f(x_1)<F(x_2)$ |
| 783 |
func 1 11 .1 x**(.5)+3 |
| 784 |
end |
| 785 |
|
| 786 |
begin asymptotes 64 64 0 0 10 10 $x$ $y$ |
| 787 |
curve 1 1 2 3 4 5.7 7 8.1 9 9 |
| 788 |
point 2 3 4 5.7 7 8.1 |
| 789 |
\shift{(-.05,.05)} |
| 790 |
func 1.4 2.6 .1 1.65*x-.3 |
| 791 |
func 3.2 4.8 .1 1.025*x+1.6 |
| 792 |
func 6.1 7.9 .1 .62*x+3.76 |
| 793 |
label cl 9.5 9 $f(x)$ |
| 794 |
label tl 5 5 $f^\prime(x)>0$ |
| 795 |
label tl 5 4 $f^\prime(x)$ decreasing |
| 796 |
end |
| 797 |
|
| 798 |
# the previous diagram was made when expression were not yet available |
| 799 |
# with them, it's easier to use a defined curve and its calculated slopes |
| 800 |
# and positions: |
| 801 |
begin droppers 64 64 0 0 10 10 $x$ $y$ |
| 802 |
func 0 10 .1 3*(sqrt(x)) |
| 803 |
point 2 3*sqrt(2) 4 3*sqrt(4) 7 3*sqrt(7) |
| 804 |
\shift{(-.05,.05)} |
| 805 |
func 1.5 2.5 .1 3/2*(x/sqrt(2)+sqrt(2)) |
| 806 |
func 3.5 4.5 .1 3/2*(x/sqrt(4)+sqrt(4)) |
| 807 |
func 6.5 7.5 .1 3/2*(x/sqrt(7)+sqrt(7)) |
| 808 |
label cl 9.5 9 $f(x)$ |
| 809 |
label tl 5 5 $f^\prime(x)>0$ |
| 810 |
label tl 5 4 $f^\prime(x)$ decreasing |
| 811 |
end |
| 812 |
|
| 813 |
begin inflections 64 64 -.85 -1.5 1.5 5 $x$ $y$ |
| 814 |
func -.6 1.5 .05 6*(x**4)-8*(x**3)+1 |
| 815 |
lines -.2 1 .2 1 |
| 816 |
label cr -.25 1 horizontal |
| 817 |
arrow .5 2 0 1 inflection point |
| 818 |
arrow .8 1 .65 0 inflection point |
| 819 |
arrow 1 5 1.5 4.375 $f(x)=6x^4-8x^3+1$ |
| 820 |
Xmark 1 |
| 821 |
ymark \raise-8pt\hbox{0} 0 -1 |
| 822 |
xydrop 1 -1 |
| 823 |
end |
| 824 |
|
| 825 |
begin paraboloid 64 64 -4 -4 4 4 - - |
| 826 |
\dashed |
| 827 |
lines -4 0 0 0 0 0 0 -4 # neg z and neg y |
| 828 |
\dashed |
| 829 |
lines 0 0 3 1.73 # neg x |
| 830 |
\arrow |
| 831 |
lines 0 0 0 4 # pos z |
| 832 |
\arrow |
| 833 |
lines 0 0 4 0 # pos y |
| 834 |
\arrow |
| 835 |
lines 0 0 -3 -1.73 # pos x |
| 836 |
\dotted |
| 837 |
lines -1 4 -1 0 -4 -1.73 # intersections y=-1 plane |
| 838 |
\dotted |
| 839 |
lines -1 -.577 -4 -.577 # intersection x=2 plane with xy-plane |
| 840 |
% extra helplines |
| 841 |
\dotted |
| 842 |
lines -2 .423 -2 -.577 0 0 0 1 -2 .423 |
| 843 |
Ymark 3 |
| 844 |
% end of extras |
| 845 |
\dotted |
| 846 |
lines 0 3 -1 3 -4 1.27 |
| 847 |
\dashed\sclosed |
| 848 |
curve -3 2.42 -1.5 2.711 -1 2.42 -2.5 2.134 |
| 849 |
label bc 0 yhi $z$ |
| 850 |
label cl xhi 0 $y$ |
| 851 |
label tr -3.1 -1.8 $x$ |
| 852 |
label cl -.85 -.577 2 |
| 853 |
label tc 0 5.5 $f(x,y)=x^2-4x+2y^2+4y+7$ |
| 854 |
xmark -1 |
| 855 |
Ymark 1 |
| 856 |
\shift{(-2,.42)} |
| 857 |
\dashed |
| 858 |
func 0 .5 .1 9*x*x |
| 859 |
func -.5 0 .1 7*x*x |
| 860 |
\dashed |
| 861 |
func -1 0 .1 2*x*x |
| 862 |
func 0 1 .1 2*x*x |
| 863 |
end |