Latex2e class and package design (2)
Continue to explain how to write the class and package.
Ii. Class and package writing commands
1) file identity command
/Needstexformat {format-name} [release-date]
This command tells tex that this file is processed by the format-name format tex system.
Release-date is used to specify the version date in the format.
/Providesclass {class-name} [release-info]
/Providespackage {package-name} [release-info]
This file contains the definition of Class-Name and package-name.
Release-Info indicates the description of the class or package. The format is as follows:
(1) package version Date: yyyy/mm/DD;
(2) description text, including the version number.
You can use the/listfiles command to display the release-Info information.
/Providesfile {file-name} [release-info]
File-name must be the full name of the file, including the extension. This command is used to declare files other than the main class and package files.
2) file loading command
Add another package to the class or package file:
/Requirepackage [Options-list] {package-name} [release-info]
/Requirepackagewitexceptions {package-name} [release-info]
/Loadclass [Options-list] {class-name} [release-info]
/Loadclasswitexceptions {class-name} [release-info]
Each class file can only be used once, and can only be used in class files.
3) option declaration command
/Declareoption {Option-name} {code}
Change option-name to an option of the package or class. The Code parameter is the code to be executed when the user uses this option. It can contain any legal latex2e structure.
/Declareoption * {code}
Set the "Default Option Code", which is the code parameter used by the system to execute the command when the options used by the user are not explicitly declared in the class or package. The Code parameter can contain any legal latex2e structure.
4) The Command Used in the optional code
The Command Used in the Code parameter:
/Currentoption
Indicates the current option specified by the user.
/Optionnotused
Add the current option specified by the user to the list of "no options available.
/Passoptionstopackage {options-list} {package-name}
Pass the option listed in optons-List (separated by commas) to the package-name, that is, the Optional options that can be used by the subsequent/requirepackage or/usepackage Commands include the options in Option-list.
/Passoptionstoclass {options-list} {class-name}
Note:
The/requirepackagewitexceptions command is similar to the/requirepackage command, but the option list used by the former when loading the specified package is always the same as the option list used when the current class or package is opened, the optional values are not explicitly provided or passed by the/passoptionstopackage command.
The main purpose of/loadclasswitexceptions is to allow a class to simply inherit the features of other classes, such as the command/loadclasswitexceptions {Article} with the control sequence:
/Declareoption * {/passoptionstoclass {/currentoption} {Article }}
/Processoptions/relax
/Loadclass {Article}
But using the/loadclasswitexceptions command is much simpler and faster. If the class declares its own options, the difference between the two is big. For example:
(1)
/Declareoption {landscape} {/@ landscapetrue}
/Processoptions/relax
/Loadclasswitexceptions {Article}
(2)
/Declareoption {landscape} {/@ landscapetrue}
/Declareoption * {/passoptionstoclass {/currentoption} {Article }}
/Processoptions/relax
/Loadclass {Article}
Sequence (1) is good; in sequence (2), when article is loaded, it cannot receive the landscape option at all, because only the default option is passed to article, landscape is an option with a display declaration, which is not included in the pass column.
5) code delayed Command Execution
/Atendofclass {code}
/Atendofpackage {code}
It is mainly used in the parameter of the/declareoption or/declareoption * command to temporarily Save the code internally and then run the code after the current class or package is processed. The command can be used multiple times.
/Atbegindocument {code}
/Atenddocument {code}
The/atbegindocument Command temporarily saves the code internally and waits until latex executes the/begin {document} And/end {document} commands.
The Code parameter of the/atenddocument command is executed when the/end {document} command is executed, the final page is not completed, and the remaining floating environment is not processed. If some code in the code needs to be executed after the typographical task has been completed in both the page and the floating environment, A/clearpage command must be inserted before the code.
/Atbegindvi {specials}
This command saves the specials parameter in a registration box and writes it to the beginning of the output of the document homepage in the. DVI file. Parameters cannot contain any typographical content to enter the. DVI file. The command can be used multiple times.
6) Optional processing commands
/Processoptions
The processing code set for each option used during declaration will be executed. The execution process and result in the class file and package file are slightly different.
There are two options: local and global.
Partial option: the options that are explicitly specified in the/passoptionstopackage {options} command,/usepackage [Options] command, or/requirepackage [Options] command.
Global Options: all options except local options that you specify in the options parameter of the/documentclass [Options] command.
(1) Use it in the package file
(2) Use in class files
The class file is used in the same package file, but all the options are considered as partial options. In addition, the default value of/declareoption * Is/optionnotused rather than error.
/Processoptions */@ options
The execution process is the same as/processoptions, but the order of options is not declared in the class or package, but specified by the options parameter during command calling.
/Exceuteoptions {options-list}
For each option of the options-list parameter, run the/ds @ option command in sequence. You can use this command to provide a "Default Option List" immediately before the/processoptions command ".
7) file operation commands
/Iffileexists {file-name} {true} {false}
Checks whether a file exists. If yes, run the Code provided by the true parameter. If no, run the Code provided by the false parameter.
/Inputiffileexists {file-name} {true} {false}
If the file-name file exists, the Code provided by the true parameter is executed and the file is loaded immediately. If the file does not exist, the Code provided by the false parameter is executed.
8) report error commands
/Classerror {class-name} {error-text} {help-text}
/Packageerror {package-name} {error-text} {help-text}
Used to generate error information. When an error occurs, the error message provided by the error-text parameter and? Prompt. If you press the H key, the system displays the help text specified by the help-text parameter. In the errror-text and help-text parameters, you can use the/protect command to terminate the further expansion of the current error command. You can use the/messagebreak command to wrap the display content; use the/Space Command to output spaces.
For example:
/Newcommand {/Foo} {Foo}
/Packageerror {Etel} {%
Your hovercraft is full of eels,/messagebreak
And/protect/Foo/space is/foo
} {%
Oh dear! Someting's gone wrong./messagebreak
/SPACE/space try typing/space <return>
/Space to proceed, igoring/protect/Foo.
}
The following output result is generated:
! Package Etel error: Your hovercraft is full of eels,
(Etel) And/foo is foo.
See the Etel package documentation for explanation.
Press the H key and then display:
Oh dear! Something's gone wrong.
Try typing <return> to proceed, ignoring/Foo.
/Classwarning {class-name} {warning-text}
/Packagewarning {package-name} {warning-text}
/Classwarningnoline {class-name} {warning-text}
/Packagewarningnoline {package-name} {warning-text}
The warning message given by the warning-text parameter is displayed on the screen. Among them, lines 1st and 2 show the row number where the warning occurs, while lines 3rd and 4 do not show the row number.
/Classinfo {class-name} {Info-text}
/Packageinfo {package-name} {Info-text}
Write the information given by the Info-text parameter into the log file (including the row number ).
In the warning-text and info-text parameters, you can use the/protect command to terminate the further expansion of the current error command. You can use the/messagebreak command to wrap the display content, use the/Space Command to output spaces.
9) well-defined commands
Solid command: This command can be used as a parameter of other commands, without the need to use the/protect prefix command.
/Declarerobustcommand {cmd} [num] [Default] [Definition]
/Declarerobustcommand * {cmd} [num] [Default] [Definition]
You can use these two commands to define both new commands and existing commands.
For example:
/Declarerobustcommand {/seq} [2] [N] {%
/Ifmmode
#1 _ {1}/ldots #1 _ {#2} %
/Else
/Packagewarning {Fred} {you can't use/protect/seq/space in text} %
/FI
}
/Checkcommand {cmd} [num] [Default] {definition}
/Checkcommand * {cmd} [num] [Default] {definition}
Check whether the definition of the CMD command is defined as defined by the definition parameter. if the definition is different, an error occurs.
10) Other miscellaneous commands
Layout Parameters
/Paperheight
/Paperwidth
The actual size of the paper. Unlike the/textwidth and/textheight commands, it only indicates the size of the internal main document area.
Case-sensitive character conversion command
/Uppercase {text}
/Lowercase {text}
Converts uppercase and lowercase letters. Special characters generated by some commands (such as/AE or/AA) cannot be converted.
To solve this problem, run the following command:
/Makeuppercase {text}
/Makelowercase {text}