The graphics package^{∗}

D. P. Carlisle  S. P. Q. Rahtz

1996/10/31

1 Introduction

This package implements various ‘graphics’ functions. The main features are a) inclusion of ‘graphics’ files. b) Rotation of sections of the page, c) Scaling of sections of the page.

The design is split into three ‘levels’.

• The user interface. This is the collection of commands designed to appear in a document text. Actually two separate user interface have been implemented. The ‘standard’ interface, described here, and a more powerful, and more ‘user-friendly’ interface provided by the graphicx package.

• The core functions. These functions, which are also implemented in this file do all the ‘main work’. The ‘user-interface functions just collect together the information from any optional-arguments or star-forms, and then call one of these functions.

• The driver files. It is not possible to achieve the functionality of this package just using TEX. The dvi driver used must be given additional instructions. (Using the \special command of TEX.) Unfortunately, the capabilities of various drivers differ, and the syntax required to pass instructions to the drivers is also not standardised. So the ‘core functions’ never access \special directly, but rather call a series of commands that must be defined in a special file customised for each driver. The accompanying file, drivers.dtx has suitable files for a range of popular drivers.

2 Package Options

Most of the options, such as dvips, textures etc., specify the driver that is to be used to print the document. You may wish to set up a configuration file so that this option always takes effect, even if not specified in the document. To do this, produce a file graphics.cfg containing the line:

\ExecuteOptions{dvips}

(or whichever other driver you wish.)

Apart from the driver options there are a few other options to control the behaviour of the package. draft Do not include graphics files, but instead print a box of the size the graphic

∗This file has version number v1.0c, last revised 1996/10/31.

would take up, and the file name. This greatly speeds up previewing on most systems.

final Turns off the draft option.

debugshow Show a lot of tracing information on the terminal. If you are not me you probably do not want to use this option.

hiderotate Do not show rotated text. Sometimes useful if your previewer can not rotate text.

hidescale Do not show scaled text.

hiresbb Look for Bounding Box lines of the form %%HiResBoundingBox instead of the standard %%BoundingBox. These are used by some applications to get round the restriction that BoundingBox comments should only have integer values.

3 Standard Interface

3.1 Graphics Inclusion

\includegraphics *[⟨llx,lly⟩][⟨urx,ury⟩]{⟨file⟩}

Include a graphics file.

If * is present, then the graphic is ‘clipped’ to the size specified. If * is omitted, then any part of the graphic that is outside the specified ‘bounding box’ will overprint the surrounding text.

If the optional arguments are omitted, then the size of the graphic will be determined by reading an external file as described below. If [⟨urx,ury⟩] is present, then it should specify the coordinates of the top right corner of the image, as a pair of TEX dimensions. If the units are ommited they default to bp. So [1in,1in] and [72,72] are equivalent. If only one optional argument appears, the lower left corner of the image is assumed to be at [0,0]. Otherwise [⟨llx,lly⟩] may be used to specify the cordinates of this point.

\graphicspath {⟨dir-list⟩}

This optional declaration may be used to specify a list of directories in which to search for graphics files. The format is as for the L^{A}TEX 2ε primitive \input@path, a list of directories, each in a {} group (even if there is only one in the list). For example: \graphicspath{{eps/}{tiff/}} would cause the system to look in the subdirectories eps and tiff of the current directory. The default setting of this path is \input@path that is: graphics files will be found wherever TEX files are found.

\DeclareGraphicsExtensions {⟨ext-list⟩}

This specifies the behaviour of the system when argument to \includegraphics does not have an extension specified. {⟨ext-list⟩} should be a comma separated list of file extensions. A file name is produced by appending sep and one extension. If a file is found, the system acts as if that extension had been specified. If not, the next extension in ext-list is tried.

Early versions of this package defined a default argument for this command. This has been removed.

\DeclareGraphicsRule {⟨ext⟩}{⟨type⟩}{⟨read-file⟩}{⟨command⟩} Any number of these declarations can be made. They determine how the system behaves when a file with extension ext is specified. (The extension may be specified explicitly or, if the argument to \includegraphics does not have an extension, it may be a default extension from the ext-list specified with

\DeclareGraphicsExtensions.)

ext is the extension of the file. Any file with this extension will be processed by this graphics rule. Normally a file with an extension for which no rule has been declared will generate an error, however you may use * as the extension to define a default rule. For instance the dvips driver file declares all files to be of type eps unless a more specific rule is declared.

Since Version v0.6, extensions should be specified including the . that is, .eps not eps.

type is the ‘type’ of file involved. All files of the same type will be input with the same internal command (which must be defined in a ‘driver file’). For example files with extensions ps, eps, ps.gz may all be classed as type eps.

read-file determines the extension of the file that should be read to determine size information. It may be the same as ext but it may be different, for example .ps.gz files are not readable easily by TEX, so you may want to put the bounding box information in a separate file with extension .ps.bb. If read-file is empty, {}, then the system will not try to locate an external file for size info, and the size must be specified in the arguments of \includegraphics. As a special case * may be used to denote the same extension as the graphic file. This is mainly of use in conjuction with using * as the extension, as in that case the particular graphic extension is not known. For example

 \DeclareGraphicsRule{*}{eps}{*}{}

This would decalare a default rule, such that all unknown extensions would be treated as EPS files, and the graphic file would be read for a BoundingBox comment.

If the driver file specifies a procedure for reading size files for type, that will be used, otherwise the procedure for reading eps files will be used. Thus the size of bitmap files may be specified in a file with a PostScript style %%BoundingBox line, if no other specific format is available.

command is usually empty, but if non empty it is used in place of the filename in the \special. Within this argument, #1 may be used to denote the filename. Thus using the dvips driver, one may use

\DeclareGrahicsRule{.ps.gz}{eps}{.ps.bb}{`zcat #1}

the final argument causes dvips to use the zcat command to unzip the file before inserting it into the PostScript output.

3.2 Rotation

\rotatebox {⟨angle⟩}{⟨text⟩}

Rotate text angle degrees anti-clockwise. Normally the rotation is about the lefthand end of the baseline of text.

3.3 Scaling

\scalebox {⟨h-scale⟩}[⟨v-scale⟩]{⟨text⟩} Scale text by the specified amounts. If v-scale is omitted, the vertical scale factor is the same as the horizontal one.

\resizebox *{⟨h-length⟩}{⟨v-length⟩}{⟨text⟩} Scale text so that the width is h-length. If ! is used as either length argument, the other argument is used to determine a scale factor that is used in both directions. Normally v-length refers to the height of the box, but in the star form, it refers to the ‘height + depth’. As normal for L^{A}TEX 2ε box length arguments, \height, \width, \totalheight and \depth may be used to refer to the original size of the box.

4 The Key=Value Interface

As mentioned in the introduction, apart from the above ‘standard interface’, there is an alternative syntax to the \includegraphics and \rotatebox commands that some people may prefer. It is provided by the accompanying graphicx package.

5 The Graphics Kernel Functions

5.1 Graphics Inclusion

\Ginclude@graphics {⟨file⟩}

Insert the contents of the file file at the current point. \Ginclude@graphics may use the four macros \Gin@llx, \Gin@lly, \Gin@urx, \Gin@ury to determine the ‘bounding box’ of the graphic. The result will be a TEX box of width urx −llx and height ury − lly. If \Gin@clip is ⟨true⟩ then part of the graphic that is outside this box should not be displayed. (Not all drivers can support this ‘clipping’.) Normally all these parameters are set by the ‘user interface level’.

\Gread@eps {⟨file⟩}

For each type of graphics file supported, the driver file must define \Ginclude@type and, optionally \Gread@type. The read command is responsible for obtaining size information from the file specified in the \DeclareGraphicsRule command. However the kernel defines a function, \Gread@eps, which can read PostScript files to find the %%BoundingBox comment. This function will be used for any type for which a specific function has not been declared. \Gread@eps accepts a generalised version of the bounding box comment. TEX units may be used (but there must be no space before the unit). If the unit is omitted bp is assumed. So

%%BoundingBox 0 0 2in 3in

Would be accepted by this function, to produce a 2in wide, by 3in high graphic.

5.2 Rotation

\Grot@box

Rotate the contents of \box0 through \Grot@angle degrees (anti-clockwise). The user-interface is responsible for setting the macro \Grot@angle, and putting the appropriate text in \Grot@box.

5.3 Scaling

\Gscale@box {⟨xscale⟩}[⟨yscale⟩]{⟨text⟩}

(The second argument is not optional.) Scale text by the appropriate scale factors.

\Gscale@box@dd {⟨dima⟩}{⟨dimb⟩}{⟨text⟩}

Scale text in both directions by a factor dima/dimb.

\Gscale@box@dddd {⟨dima⟩}{⟨dimb⟩}{⟨dimc⟩}{⟨dimd⟩}{⟨text⟩} Scale text in horizontally by a factor dima/dimb, and vertically by a factor of dimc/dimd.

\Gscale@div {⟨cmd⟩}{⟨dima⟩}{⟨dimb⟩}

Define the macro cmd to be the ratio of the lengths dima/dimb.

6 Interface to the Driver Files

6.1 Graphics Inclusion

Each driver file must declare that its driver can include graphics of certain types. It does this by declaring for each type a command of the form:

\Ginclude@type

The Graphics kernel function will call this driver-defined function with the filename as argument, and certain additional information will be provided as follows.:

 \Gin@llx, \Gin@lly,
 \Gin@urx, \Gin@ury

Macros storing the ‘bounding box’

 \Gin@nat@width
 \Gin@nat@height

Registers storing the natural size.

 \Gin@req@width
 \Gin@req@height

Registers storing the required size, after scaling.

 \Gin@scalex, \Gin@scaley

macros with the scale factors. A value of ! means: Scale by the same amount as the other direction.

 \ifGin@clip

\newif token, true if the graphic should be ‘clipped’ to the bounding box.

Optionally the driver may define a command of the form:

\Gread@type

This is responsible for reading an external file to find the bounding box information. If such a command is not declared, but a read-file is specified the command \Gread@eps, which is defined in the Graphics Kernel will be used.

6.2 Literal Postscript

Drivers that are producing PostScript output may want to define the following macros. They each take one argument which should be passed to an appropriate special. They are not used directly by this package but allow other packages to use the standard configuration file and package options to customise to various drivers: \Gin@PS@raw, Literal PostScript special. \Gin@PS@restored, Literal PostScript special, the driver will surround this with a save-restore pair. \Gin@PS@literal@header, Postscript to be inserted in the header section of the PostScript file.

\Gin@PS@file@header, external file to be inserted in the header section of the PostScript file.

6.3 Rotation

\Grot@start, \Grot@end These macros must be defined to insert the appropriate \special to rotate the text between them by \Grot@angle degrees. The kernel function will make sure that the correct TEX spacing is produced, these functions only need insert the \special.

6.4 Scaling

\Gscale@start, \Gscale@end, as for rotation, but here scale the text by

\Gscale@x and \Gscale@y.

7 Implementation

1 ⟨∗package⟩

7.1 Initialisation

\Gin@codes First we save the catcodes of some characters, and set them to fixed values whilst this file is being read.

 2 \edef\Gin@codes{%
 3 \catcode`\noexpand\^^A\the\catcode`\^^A\relax
 4 \catcode`\noexpand\"\the\catcode`\"\relax
 5 \catcode`\noexpand\*\the\catcode`\*\relax
 6 \catcode`\noexpand\!\the\catcode`\!\relax
 7 \catcode`\noexpand\:\the\catcode`\:\relax}
 8 \catcode`\^^A=\catcode`\%
 9 \@makeother\"%
10 \catcode`\*=11
11 \@makeother\!%
12 \@makeother\:%

We will need to have an implementation of the trigonometric functions for the rotation feature. May as well load it now.

13 \RequirePackage{trig}

\Grot@start \Grot@end Initialise the rotation primitives.

14 \providecommand\Grot@start{\@latex@error{Rotation not supported}\@ehc
15  \global\let\Grot@start\relax}
16 \providecommand\Grot@end{}

\Gscale@start \Gscale@end Initialise the scaling primitives.

17 \providecommand\Gscale@start{\@latex@error{Scaling not supported}\@ehc
18  \global\let\Gscale@start\relax}
19 \providecommand\Gscale@end{}

\Gread@BBox %%BoundingBox as a macro for testing with \ifx. This may be redefined by the hiresbb option.

20 \edef\Gread@BBox{\@percentchar\@percentchar BoundingBox}

7.2 Options

\ds@draft \ds@final
21 \DeclareOption{draft}{\Gin@drafttrue}
22 \DeclareOption{final}{\Gin@draftfalse}

\ifGin@draft True in draft mode.

23 \newif\ifGin@draft

\ds@hiresbb If given this option the package will look for bounding box comments of the form %%HiResBoundingBox (which typically have real values) instead of the standard %%BoundingBox (which should have integer values).

24 \DeclareOption{hiresbb}{%
25 \edef\Gread@BBox{\@percentchar\@percentchar HiResBoundingBox}}

\Gin@driver Driver in use.

26 \providecommand\Gin@driver{}

\ds@dvips \ds@xdvi Tomas Rockiki’s PostScript driver (unix, MSDOS, VMS…). The X11 previewer xdvi supports basically the same set of \specials.

27 \DeclareOption{dvips}{\def\Gin@driver{dvips.def}}
28 \DeclareOption{xdvi}{\ExecuteOptions{dvips}}

\ds@dvipdf Sergey Lesenko’s dvipdf driver.

29 \DeclareOption{dvipdf}{\def\Gin@driver{dvipdf.def}}

\ds@dvipsone \ds@dviwindo The drivers for the Y&Y TEX system.

30 \DeclareOption{dvipsone}{\def\Gin@driver{dvipsone.def}}
31 \DeclareOption{dviwindo}{\ExecuteOptions{dvipsone}}

\ds@emtex \ds@dviwin Two freely available sets of drivers for MSDOS, OS/2 and Windows.

32 \DeclareOption{emtex}{\def\Gin@driver{emtex.def}}
33 \DeclareOption{dviwin}{\def\Gin@driver{dviwin.def}}

\ds@oztex OZTEX (Macintosh).

34 \DeclareOption{oztex}{\def\Gin@driver{oztex.def}}

\ds@textures Textures (Macintosh).

35 \DeclareOption{textures}{\def\Gin@driver{textures.def}}
\ds@pctexps \ds@pctexwin \ds@pctexhp \ds@pctex32 PCTEX (MSDOS/Windows) .
36 \DeclareOption{pctexps}{\def\Gin@driver{pctexps.def}}
37 \DeclareOption{pctexwin}{\def\Gin@driver{pctexwin.def}}
38 \DeclareOption{pctexhp}{\def\Gin@driver{pctexhp.def}}
39 \DeclareOption{pctex32}{\def\Gin@driver{pctex32.def}}

\ds@truetex \ds@tcidvi Kinch TrueTeX, and its version with extended special support as shipped by Scientific Word.

40 \DeclareOption{truetex}{\def\Gin@driver{truetex.def}}
41 \DeclareOption{tcidvi}{\def\Gin@driver{tcidvi.def}}

\ds@dvi2ps \ds@dvialw \ds@dvilaser \ds@dvitops \ds@psprint \ds@pubps \ds@ln If anyone is using any of these driver options would they let me know. All these

are essentially untried and untested as far as I know.

42 %\DeclareOption{dvi2ps}{\def\Gin@driver{dvi2ps.def}}
43 %\DeclareOption{dvialw}{\def\Gin@driver{dvialw.def}}
44 %\DeclareOption{dvilaser}{\def\Gin@driver{dvilaser.def}}
45 %\DeclareOption{dvitops}{\def\Gin@driver{dvitops.def}}
46 %\DeclareOption{psprint}{\def\Gin@driver{psprint.def}}
47 %\DeclareOption{pubps}{\def\Gin@driver{pubps.def}}
48 %\DeclareOption{ln}{\def\Gin@driver{ln.def}}

\ds@debugshow You probably don’t want to use this…

49 \DeclareOption{debugshow}{\catcode`\^^A=9 \let\GDebug\typeout}

A local configuration file may define more options. It should also make one driver option the default, by calling \ExecuteOptions with the appropriate option.

50 \InputIfFileExists{graphics.cfg}{}{}
\ds@hiderotate
51 \DeclareOption{hiderotate}{%
52 \def\Grot@start{\begingroup\setbox\z@\hbox\bgroup}
53 \def\Grot@end{\egroup\endgroup}}
\ds@hidescale
54 \DeclareOption{hidescale}{%
55 \def\Gscale@start{\begingroup\setbox\z@\hbox\bgroup}
56 \def\Gscale@end{\egroup\endgroup}}

After the options are processed, load the appropriate driver file. If a site wants a default driver (eg textures) it just needs to put \ExecuteOptions{textures}

in a graphics.cfg file.
57 \ProcessOptions

Check that a driver has been specified (either as an option, or as a default option in the configuration file). Then load the ‘def’ file for that option, if it has not already been loaded by some other package (for instance the color package).

58 \if!\Gin@driver!
59 \PackageError{graphics}
60  {No driver specified}
61  {You should make a default driver option in a file \MessageBreak
62  graphics.cfg\MessageBreak
63  eg: \protect\ExecuteOptions{textures}%

64 }

65 \else
66 \PackageInfo{graphics}{Driver file: \Gin@driver}
67 \@ifundefined{ver@\Gin@driver}{\input{\Gin@driver}}{}
68 \fi

7.3 Graphics Inclusion

This Graphics package uses a lot of dimension registers. TEX only has a limited number of registers, so rather than allocate new ones, re-use some existing L^{A}TEX registers. This is safe as long as all uses of the registers are local, and that you can be sure that you never need to have access to both uses within the same scope.

\Gin@llx \Gin@lly \Gin@urx \Gin@ury In fact these four lengths are now stored as macros not as dimen registers, mainly so that integer bp lengths may be passed exactly.

69 \def\Gin@llx{0}
70 \let\Gin@lly\Gin@llx
71 \let\Gin@urx\Gin@llx
72 \let\Gin@ury\Gin@llx

\Gin@nat@width \Gin@nat@height The ‘natural’ size of the graphic, before any scaling.

73 \let\Gin@nat@width\leftmarginv
74 \let\Gin@nat@height\leftmarginvi

\ifGin@clip This switch is ⟨true⟩ if any graphics outside the specified bounding box (really viewport) should not be printed.

75 \newif\ifGin@clip

\DeclareGraphicsExtensions Declare a comma separated list of default extensions to be used if the file is specified with no extension.

76 \newcommand\DeclareGraphicsExtensions[1]{%
77 \edef\Gin@extensions{\zap@space#1 \@empty}}

\Gin@extensions Initialise the list of possible extensions.

78 \providecommand\Gin@extensions{}

\includegraphics Top level command for the standard interface, just look for a *.

79 \def\includegraphics{%
80 \@ifstar
81  {\Gin@cliptrue\Gin@i}%
82  {\Gin@clipfalse\Gin@i}}

\Gin@i If an optional argument is present, call \Gin@ii to process it, otherwise call

\Ginclude@graphics.
83 \def\Gin@i{%
84 \@ifnextchar[%]
85  \Gin@ii
86  {\Gin@bboxfalse\Ginclude@graphics}}

\Gin@ii Look for a second optional argument.

87 \def\Gin@ii[#1]{%
88 \@ifnextchar[%]
89  {\Gin@iii[#1]}
90  {\Gin@iii[0,0][#1]}}

\Gin@iii Set the cordinates of the llower left corner, and the cordinates of the upper right corner. The cordinates may be any TEX dimension, defaulting to bp.

91 \def\Gin@iii[#1,#2][#3,#4]#5{%
92 \begingroup
93 \Gin@bboxtrue
94 \Gin@defaultbp\Gin@llx{#1}%
95 \Gin@defaultbp\Gin@lly{#2}%
96 \Gin@defaultbp\Gin@urx{#3}%
97 \Gin@defaultbp\Gin@ury{#4}%
98 \Ginclude@graphics{#5}%
99 \endgroup}

\Gin@defaultbp \Gin@def@bp This macro grabs a length, #2, which may or may not have a unit, and if a unit is supplied, converts to ‘bp’ and stores the value in #1. If a unit is not supplied ‘bp’ is assumed, and #2 is directly stored in #1. Note that supplying ‘bp’ is not quite the same as supplying no units, as in the former case a convertion via ‘pt’ and back to ‘bp’ takes place which can introduce rounding error. The error is invisibly small but files conforming to Adobe DSC should have integer Bounding Box Coordinates, and conceivably some drivers might demand integer values. (Although most seem to accept real values (if they accept bounding box coordinates at all) in the \special. This is the reason why the mechanism uses \def and not TEX lengths, as in earlier releases of the package.

100 \def\Gin@defaultbp#1#2{%
101 \afterassignment\Gin@def@bp\dimen@#2bp\relax{#1}{#2}}
102 \def\Gin@def@bp#1\relax#2#3{%
103 \if!#1!%
104  \def#2{#3}%
105  \else
106  \dimen@.99626\dimen@
107  \edef#2{\strip@pt\dimen@}%
108  \fi}

\DeclareGraphicsRule Declare what actions should be taken for a particular file extension.

#1 extension, #2 type, #3 read-file, #4 command,

109 \def\DeclareGraphicsRule#1#2#3#4{%
110 \edef\@tempa{\string *}\def\@tempb{#3}%
111 \expandafter\edef\csname Gin@rule@#1\endcsname##1%
112  {{#2}%
113  {\ifx\@tempa\@tempb\noexpand\Gin@ext\else#3\fi}%
114  {\ifx\indent#4\indent##1\else#4\fi}}}

An example rule base.

                    ext    type  read  command
 \DeclareGrahicsRule{.ps}   {eps} {.ps}    {}
 \DeclareGrahicsRule{.eps}  {eps} {.eps}   {}
 \DeclareGrahicsRule{.ps.gz}{eps} {.ps.bb} {`zcat #1}
 \DeclareGrahicsRule{.pcx}  {bmp} {}      {}

\graphicspath User level command to set the input path for graphics files. A list of directories, each in a {} group.

115 \def\graphicspath#1{\def\Ginput@path{#1}}

\Ginput@path The default graphic path is \input@path.

116 \ifx\Ginput@path\@undefined
117 \let\Ginput@path\input@path
118 \fi

\Gin@getbase Given a possible extension, #1, check whether the file exists. If it does set \Gin@base and \Gin@ext to the filename stripped of the extension, and the extension, respectively.

119 \def\Gin@getbase#1{%
120 \edef\Gin@tempa{%
121  \def\noexpand\@tempa####1#1\space{%

122  \def\noexpand\Gin@base{####1}}}%
123 \IfFileExists{\filename@area\filename@base#1}%
124  {\Gin@tempa
125  \expandafter\@tempa\@filef@und
126  \edef\Gin@ext{#1}}{}}%

\Gin@ext Initialise the macro to hold the extension.

127 \let\Gin@ext\relax

\Gin@sepdefault This must match the token used by \filename@parse to delimit the extension.

128 \def\Gin@sepdefault{.}

\Ginclude@graphics The main internal function implementing graphics file inclusion. #1 is the file name.

129 \def\Ginclude@graphics#1{%
130 \begingroup
131 \let\input@path\Ginput@path
132 \filename@parse{#1}%
133 \ifx\filename@ext\relax
134  \@for\Gin@temp:=\Gin@extensions\do{%
135  \ifx\Gin@ext\relax
136  \Gin@getbase\Gin@temp
137  \fi}%
138 \else
139  \Gin@getbase{\Gin@sepdefault\filename@ext}%

If the user supplied an explicit extension, just give a warning if the file does not exist. (It may be created later.)

140  \ifx\Gin@ext\relax
141  \@warning{File `#1' not found}%
142  \def\Gin@base{\filename@area\filename@base}%
143  \edef\Gin@ext{\Gin@sepdefault\filename@ext}%
144  \fi
145 \fi

If no extension is supplied, it is an error if the file does not exist, as there is no way for the system to know which extension to supply.

146  \ifx\Gin@ext\relax
147  \@latex@error{File `#1' not found}%
148  {I could not locate the file with any of these extensions:^^J%
149  \Gin@extensions^^J\@ehc}%
150  \else
151  \@ifundefined{Gin@rule@\Gin@ext}%

Handle default rule.

152  {\ifx\Gin@rule@*\@undefined
153  \@latex@error{Unknown graphics extension: \Gin@ext}\@ehc
154  \else
155  \expandafter\Gin@setfile\Gin@rule@*{\Gin@base\Gin@ext}%
156  \fi}%
157  {\expandafter\expandafter\expandafter\Gin@setfile
158  \csname Gin@rule@\Gin@ext\endcsname{\Gin@base\Gin@ext}}%
159  \fi
160 \endgroup}

\ifGread@ True if a file should be read to obtain the natural size.

161 \newif\ifGread@\Gread@true

\Gin@setfile Set a file to the size specified in arguments, or in a ‘read file’.

162 \def\Gin@setfile#1#2#3{%
163 \ifx\\#2\\\Gread@false\fi
164 \ifGin@bbox\else
165  \ifGread@
166  \csname Gread@%
167  \expandafter\ifx\csname Gread@#1\endcsname\relax
168  eps%
169  \else
170  #1%
171  \fi
172  \endcsname{\Gin@base#2}%
173  \else

By now the natural size should be known either from arguments or from the file. If not generate an error. (The graphicx interface relaxes this condition slightly.)

174  \Gin@nosize{#3}%
175  \fi
176 \fi

The following call will modify the ‘natural size’ if the user has supplied a viewport or trim specification. (Not available in the standard interface.)

177 \Gin@viewport@code

Save the natural size, and then call \Gin@req@sizes whic (in the key-val interface) will calculate the required size from the natural size, and any scaling info.

178 \Gin@nat@height\Gin@ury bp%
179 \advance\Gin@nat@height-\Gin@lly bp%
180 \Gin@nat@width\Gin@urx bp%
181 \advance\Gin@nat@width-\Gin@llx bp%
182 \Gin@req@sizes

Call \Ginclude@type to include the figure unless this is not defined, or draft mode is being used.

183 \expandafter\ifx\csname Ginclude@#1\endcsname\relax
184  \Gin@drafttrue
185  \expandafter\ifx\csname Gread@#1\endcsname\relax
186  \@latex@error{Can not include graphics of type: #1}\@ehc
187  \global\expandafter\let\csname Gread@#1\endcsname\@empty
188  \fi
189 \fi
190 \leavevmode
191 \ifGin@draft
192  \hb@xt@\Gin@req@width{%
193  \vrule\hss
194  \vbox to \Gin@req@height{%
195  \hrule \@width \Gin@req@width
196  \vss
197  \edef\@tempa{#3}%
198  \rlap{ \ttfamily\expandafter\strip@prefix\meaning\@tempa}%
199  \vss
200  \hrule}%

201  \hss\vrule}%
202 \else

Support \listfiles and then set the final box to the required size.

203  \@addtofilelist{#3}%
204  \ProvidesFile{#3}[Graphic file (type #1)]%
205  \setbox\z@\hbox{\csname Ginclude@#1\endcsname{#3}}%
206  \dp\z@\z@
207  \ht\z@\Gin@req@height
208  \wd\z@\Gin@req@width
209 \box\z@
210 \fi}

\Gin@exclamation Catcode 12 !, in case of French, or other language styles.

211 \def\Gin@exclamation{!}

\Gin@req@sizes \Gin@scalex \Gin@scaley \Gin@req@height \Gin@req@width In the standard interface there is no scaling, so the required size is the same as the natural size. In other interfaces \Gin@req@sizes will be responsible for setting these parameters. Here we can set them globally.

212 \let\Gin@req@sizes\relax
213 \def\Gin@scalex{1}%
214 \let\Gin@scaley\Gin@exclamation
215 \let\Gin@req@height\Gin@nat@height
216 \let\Gin@req@width\Gin@nat@width

\Gin@viewport@code In the standard interface there is no possibility of specifying a viewport, so this is a no-op.

217 \let\Gin@viewport@code\relax

\Gin@nosize This command is called in the case that the graphics type specifies no ‘read file’ and the user supplied no size arguments. In the standard interface can only generate an error.

218 \def\Gin@nosize#1{%
219 \@latex@error
220  {Cannot determine size of graphic in #1 (no size specifed)}%
221  \@ehc}

7.4 Reading the BoundingBox in EPS files

\ifGin@bbox This switch should be set ⟨true⟩ once a size has been found, either in an argument, or in an external file.

222 \newif\ifGin@bbox

\Gread@eps Read an EPS file (#1) and search for a line starting with %%BoundingBox and returns the result by setting four dimension registers \Gin@llx, \Gin@lly, \Gin@urx

and \Gin@ury.
223 \def\Gread@eps#1{%
224 \begingroup

Make it reasonably safe to have binary headers in the EPS file before the bounding box line.

225 \@tempcnta\z@
226 \loop\ifnum\@tempcnta<\@xxxii

227 \catcode\@tempcnta14 %
228 \advance\@tempcnta\@ne
229 \repeat
230 \catcode127=14 %
231 \let\do\@makeother\dospecials\catcode`\ 10 %
232 \catcode\endlinechar5 %

The first thing we need to do is to open the information file, if possible.

233 \immediate\openin\@inputcheck#1 %
234 \ifeof\@inputcheck
235  \@latex@error{File `#1' not found}\@ehc
236 \else

Now we’ll scan lines until we find one that starts with %%BoundingBox: We need to reset the catcodes to read the file, and so this is done in a group.

237  \Gread@true
238  \let\@tempb\Gread@false
239  \loop
240  \read\@inputcheck to\@tempa
241  \ifeof\@inputcheck
242  \Gread@false
243  \else
244  \expandafter\Gread@find@bb\@tempa:.\\%
245  \fi
246  \ifGread@
247  \repeat
248  \immediate\closein\@inputcheck
249 \fi
250 \ifGin@bbox\else
251  \@latex@error
252  {Cannot determine size of graphic in #1 (no BoundingBox)}%
253  \@ehc
254  \gdef\@gtempa{0 0 72 72 }%
255 \fi
256 \endgroup
257 \expandafter\Gread@parse@bb\@gtempa\\}

\Gread@find@bb If a line in the EPS file starts with a %%BoundingBox:, we will examine it more closely. Note using the ‘extra’ argument #2#3 causes any space after the : to be gobbled.

258 \long\def\Gread@find@bb#1:#2#3\\{%
259 \def\@tempa{#1}%
260 \ifx\@tempa\Gread@BBox
261  \Gread@test@atend#2#3()\\%
262 \fi}

\Gread@test@atend Determine if the stuff following the %%BoundingBox is ‘(atend)’, which will involve further reading of the file. This is accomplished by making \@tempb into a no-op, so that finding a %%BoundingBox does not stop the loop.

263 \def\Gread@test@atend#1(#2)#3\\{%
264 \def\@tempa{#2}%
265 \ifx\@tempa\Gread@atend
266  \Gread@true
267  \let\@tempb\relax

268 \else
269  \gdef\@gtempa{#1}%
270  \@tempb
271  \Gin@bboxtrue
272 \fi}

\Gread@parse@bb We have %%BoundingBox and what follows is not ‘(atend)’ so we will parse the rest of the line as a BB with four elements. PostScript files should never have units specified in the BoundingBox comment, but we allow arbitrary TEX units in external files, or in other interfaces.

273 \def\Gread@parse@bb#1 #2 #3 #4 #5\\{%
274 \Gin@defaultbp\Gin@llx{#1}%
275 \Gin@defaultbp\Gin@lly{#2}%
276 \Gin@defaultbp\Gin@urx{#3}%
277 \Gin@defaultbp\Gin@ury{#4}}%

\Gread@atend atend as a macro for testing with \ifx.

278 \def\Gread@atend{atend}

7.5 Rotation

As above, we will re-use some existing local registers.

\Grot@height \Grot@left \Grot@right \Grot@depth Final Rotated box dimensions
279 \let\Grot@height\@ovxx
280 \let\Grot@left\@ovyy
281 \let\Grot@right\@ovdx
282 \let\Grot@depth\@ovdy
\Grot@h \Grot@l \Grot@r \Grot@d Original box dimensions
283 \let\Grot@l\@ovro
284 \let\Grot@r\@ovri
285 \let\Grot@h\@xdim
286 \let\Grot@d\@ydim

\Grot@x \Grot@y Coordinates of centre of rotation.

287 \let\Grot@x\@linelen
288 \let\Grot@y\@dashdim

\rotatebox The angle is specified by #1. The box to be rotated is #2. In the standard interface the centre of rotation is (0, 0). Then finally call \Grot@box to rotate the box.

289 \def\rotatebox#1#2{%
290 \leavevmode
291 \Grot@setangle{#1}%
292 \setbox\z@\hbox{{#2}}%
293 \Grot@x\z@
294 \Grot@y\z@
295 \Grot@box}

\Grot@setangle Set the internal macro used by \Grot@box. In the standard interface this is trivial, but other interfaces may have more interesting definitions. For example:

 \def\Grot@setangle#1{%

   \dimen@#1\p@
   \dimen@-57.2968\dimen@
   \edef\Grot@angle{\strip@pt\dimen@}}

This would cause the argument of \rotatebox to be interpreted as an angle specified in radians, clockwise.

296 \def\Grot@setangle#1{\edef\Grot@angle{#1}}

7.6 Deriving a ‘bounding box’ for rotated object

We want to know the size of a ‘bounding box’ enclosing the rotated box. We define two formulae (as TEX macros) to work out the x and y coordinates of vertices of the rotated box in relation to its original coordinates (ie its width, height and depth). The box we visualize with vertices B, C, D and E is illustrated below. The vertex S is the reference point on the baseline. O is the centre of rotation, which in the standard interface is always S.

A B C D  E S  O

The formulae are, for a point P and angle α:

P′_{x} = P_{x} − O_{x}
P′_{y} = P_{y} − O_{y}
P′′_{x} = (P′_{x} × cos(α)) − (P′_{y} × sin(α))
P′′_{y} = (P′_{x} × sin(α)) + (P′_{y} × cos(α))
P′′′_{x} = P′′_{x} + O_{x} + L_{x}
P′′′_{y} = P′′_{y} + O_{y}

The ‘extra’ horizontal translation L_{x} at the end is calculated so that the leftmost point of the resulting box has x-coordinate 0. This is desirable as TEX boxes must have the reference point at the left edge of the box.

\Grot@Px Work out new x coordinate of point after rotation. The parameters #2 and #3 are the original x and y coordinates of the point. The new x coordinate is stored in

#1.
297 \def\Grot@Px#1#2#3{%
298  #1\Grot@cos#2%
299  \advance#1-\Grot@sin#3}

\Grot@Py Work out new y coordinate of point after rotation. The parameters #2 and #3 are the original x and y coordinates of the point. The new y coordinate is stored in

#1.
300 \def\Grot@Py#1#2#3{%
301  #1\Grot@sin#2%
302  \advance#1\Grot@cos#3}

\Grot@box This is the tricky bit. We can rotate the box, but then need to work out how

much space to leave for it on the page.

We simplify matters by working out first which quadrant we are in, and then picking just the right values.

303 \def\Grot@box{%
304 \begingroup

We are going to need to know the sine and cosine of the angle; simplest to calculate these now.

305 \CalculateSin\Grot@angle
306 \CalculateCos\Grot@angle
307 \edef\Grot@sin{\UseSin\Grot@angle}%
308 \edef\Grot@cos{\UseCos\Grot@angle}%
309 ^^A   \GDebug{Rotate: angle \Grot@angle, sine is \Grot@sin,
310 ^^A             cosine is \Grot@cos}%

Save the four extents of the original box.

311 \Grot@r\wd\z@  \advance\Grot@r-\Grot@x
312 \Grot@l\z@     \advance\Grot@l-\Grot@x
313 \Grot@h\ht\z@  \advance\Grot@h-\Grot@y
314 \Grot@d-\dp\z@ \advance\Grot@d-\Grot@y

Now a straightforward test to see which quadrant we are operating in;

315 \ifdim\Grot@sin\p@>\z@
316  \ifdim\Grot@cos\p@>\z@

First quadrant: Height=By, Right=Ex, Left=Cx, Depth=Dy

317  \Grot@Py\Grot@height \Grot@r\Grot@h%B
318  \Grot@Px\Grot@right  \Grot@r\Grot@d%E
319  \Grot@Px\Grot@left   \Grot@l\Grot@h%C
320  \Grot@Py\Grot@depth  \Grot@l\Grot@d%D
321  \else

Second quadrant: Height=Ey, Right=Dx, Left=Bx, Depth=Cy

322  \Grot@Py\Grot@height \Grot@r\Grot@d%E
323  \Grot@Px\Grot@right  \Grot@l\Grot@d%D
324  \Grot@Px\Grot@left   \Grot@r\Grot@h%B
325  \Grot@Py\Grot@depth  \Grot@l\Grot@h%C
326  \fi
327 \else
328  \ifdim\Grot@cos\p@<\z@

Third quadrant: Height=Dy, Right=Cx, Left=Ex, Depth=By

329  \Grot@Py\Grot@height \Grot@l\Grot@d%D
330  \Grot@Px\Grot@right  \Grot@l\Grot@h%C
331  \Grot@Px\Grot@left   \Grot@r\Grot@d%E
332  \Grot@Py\Grot@depth  \Grot@r\Grot@h%B
333  \else

Fourth quadrant: Height=Cy, Right=Bx, Left=Dx, Depth=Ey

334  \Grot@Py\Grot@height \Grot@l\Grot@h%C
335  \Grot@Px\Grot@right  \Grot@r\Grot@h%B
336  \Grot@Px\Grot@left   \Grot@l\Grot@d%D
337  \Grot@Py\Grot@depth  \Grot@r\Grot@d%E
338  \fi
339 \fi

Now we should translate back by (O_{x}, O_{y}), but TEX can not really deal with boxes that do not have the reference point at the left edge. (Everything with a −ve x-coordinate would over-print earlier text). So we modify the horizontal translation so that the reference point as understood by TEX is at the left edge. This means that the ‘centre of rotation’ is not fixed by \rotatebox, but typically moves horizontally. We also need to find the image of the original reference point, S, as that is where the rotation specials must be inserted.

340 \advance\Grot@height\Grot@y
341 \advance\Grot@depth\Grot@y
342 \Grot@Px\dimen@  \Grot@x\Grot@y
343 \Grot@Py\dimen@ii \Grot@x\Grot@y
344 \dimen@-\dimen@     \advance\dimen@-\Grot@left
345 \dimen@ii-\dimen@ii \advance\dimen@ii\Grot@y
346 ^^A   \GDebug{Rotate: (l,r,h,d)^^J%
347 ^^A Original \the\Grot@l,\the\Grot@r,\the\Grot@h,\the\Grot@d,^^J%
348 ^^A New..... \the\Grot@left,\the\Grot@right,%
349 ^^A          \the\Grot@height,\the\Grot@depth}%
350 \setbox\z@\hbox{%
351  \kern\dimen@
352  \raise\dimen@ii\hbox{\Grot@start\box\z@\Grot@end}}%
353 \ht\z@\Grot@height
354 \dp\z@-\Grot@depth
355 \advance\Grot@right-\Grot@left\wd\z@\Grot@right
356 \leavevmode\box\z@
357 \endgroup}

7.7 Stretching and Scaling

\scalebox The top level \scalebox. If the vertical scale factor is omitted it defaults to the horizontal scale factor, #1.

358 \def\scalebox#1{%
359 \@ifnextchar[{\Gscale@box{#1}}{\Gscale@box{#1}[#1]}}
\Gscale@box Internal version of \scalebox.
360 \def\Gscale@box#1[#2]#3{%
361 \leavevmode
362 \def\Gscale@x{#1}\def\Gscale@y{#2}%
363 \setbox\z@\hbox{{#3}}%
364 \setbox\tw@\hbox{\Gscale@start\rlap{\copy\z@}\Gscale@end}%
365 \ifdim#2\p@<\z@
366  \ht\tw@-#2\dp\z@
367  \dp\tw@-#2\ht\z@
368 \else
369  \ht\tw@#2\ht\z@
370  \dp\tw@#2\dp\z@
371 \fi
372 \ifdim#1\p@<\z@
373  \hb@xt@-#1\wd\z@{\kern-#1\wd\z@\box\tw@\hss}%
374 \else
375  \wd\tw@#1\wd\z@
376  \box\tw@
377 \fi}

\reflectbox Just an abbreviation for the appropriate scale to get reflection.

378 \def\reflectbox{\Gscale@box-1[1]}

\resizebox Look for a *, which specifies that a final vertical size refers to ‘height + depth’ not just ‘height’.

379 \def\resizebox{%
380 \leavevmode
381 \@ifstar{\Gscale@@box\totalheight}{\Gscale@@box\height}}

\Gscale@@box Look for the ! in the arguments.

382 \def\Gscale@@box#1#2#3{%
383 \let\@tempa\Gin@exclamation
384 \expandafter\def\expandafter\@tempb\expandafter{\string#2}%
385 \expandafter\def\expandafter\@tempc\expandafter{\string#3}%
386 \ifx\@tempb\@tempa
387  \ifx\@tempc\@tempa
388  \toks@{\mbox}%
389  \else
390  \toks@{\Gscale@box@dd{#3}#1}%
391  \fi
392 \else
393  \ifx\@tempc\@tempa
394  \toks@{\Gscale@box@dd{#2}\width}%
395  \else
396  \toks@{\Gscale@box@dddd{#2}\width{#3}#1}%
397  \fi
398 \fi
399 \the\toks@}

\Gscale@box@dd Scale the text #3 in both directions by a factor #1/#2.

400 \def\Gscale@box@dd#1#2#3{%
401 \@begin@tempboxa\hbox{#3}%
402  \setlength\@tempdima{#1}%
403  \setlength\@tempdimb{#2}%
404  \Gscale@div\@tempa\@tempdima\@tempdimb
405  \Gscale@box\@tempa[\@tempa]{\box\@tempboxa}%
406 \@end@tempboxa}

\Gscale@box@dddd Scale the text #5 horizontally by a factor #1/#2 and vertically by a factor #3/#4.

407 \def\Gscale@box@dddd#1#2#3#4#5{%
408 \@begin@tempboxa\hbox{#5}%
409  \setlength\@tempdima{#1}%
410  \setlength\@tempdimb{#2}%
411  \Gscale@div\@tempa\@tempdima\@tempdimb
412  \setlength\@tempdima{#3}%
413  \setlength\@tempdimb{#4}%
414  \Gscale@div\@tempb\@tempdima\@tempdimb
415  \ifGin@iso
416  \ifdim\@tempa\p@>\@tempb\p@
417  \let\@tempa\@tempb
418  \else
419  \let\@tempb\@tempa
420  \fi

421  \fi
422  \Gscale@box\@tempa[\@tempb]{\box\@tempboxa}%
423 \@end@tempboxa}

\ifGin@iso If this flag is true, then specifying two lengths to \resizebox scales the box by the same factor in either direction, such that neither length exceeds the stated amount. No user interface to this flag in the standard package, but it is used by

 the keepaspectratio key to \includegraphics in the graphicx package.
424 \newif\ifGin@iso

\Gscale@div The macro #1 is set to the ratio of the lengths #2 and #3.

425 \def\Gscale@div#1#2#3{%
426 \setlength\dimen@{#3}%
427 \ifdim\dimen@=\z@
428  \PackageError{graphics}{Division by 0}\@eha
429  \dimen@#2%
430 \fi
431 \edef\@tempd{\the\dimen@}%
432 \setlength\dimen@{#2}%
433 \count@65536\relax
434 \ifdim\dimen@<\z@
435  \dimen@-\dimen@
436  \count@-\count@
437 \fi
438 \ifdim\dimen@>\z@
439  \loop
440  \ifdim\dimen@<8192\p@
441  \dimen@\tw@\dimen@
442  \divide\count@\tw@
443  \repeat
444  \dimen@ii\@tempd\relax
445  \divide\dimen@ii\count@
446  \divide\dimen@\dimen@ii
447 \fi
448 \edef#1{\strip@pt\dimen@}}

Restore Catcodes

449 \Gin@codes
450 \let\Gin@codes\relax

451 ⟨/package⟩