我正在编写一个包,但遇到了一点麻烦。目前,包如下:
%!TEX encoding = UTF-8 Unicode
\def\fileversion{0.2}
\def\filedate{%
Mon 2014-04-01
}
\def\Copyright{**********************************************
Quest'opera è stata rilasciata con licenza Creative Commons Attribuzione - Non commerciale - Non opere derivate 3.0 Unported. Per leggere una copia della licenza visita il sito web http://creativecommons.org/licenses/by-nc-nd/3.0/ o spedisci una lettera a Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
(C) MGorini
**********************************************
}
\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\typeout{Package `Mworks' <\filedate>.}
\typeout{\Copyright}
\ProvidesPackage{mworks}
\RequirePackage{amsmath,amssymb,amsfonts}
\RequirePackage{multirow}
\RequirePackage{xparse}
\RequirePackage{thmtools}
\newcount\gbb
\gbb=0
\DeclareOption{gbb}{\gbb=1}
\ifnum\gbb=1{
\ExplSyntaxOn
\NewDocumentCommand{\@ag}{O{0.85}oO{}}
{\str_if_eq:nnTF{#1}{n}
{\IfValueTF#2
{#2{\scalebox{0.85}[0.85]{$#3\alpha$}}}
{\errmessage{Too few arguments}\errhelp{If you specify `n' as the first argument, the second optional argument must be specified, otherwise use a number as the first argument or specify no optional argument.}}
}{\str_if_eq:nnTF{#1}{e}
{\IfValueTF#2
{#2{\scalebox{0.65}[0.65]{$#3\alpha$}}}
{\scalebox{0.65}[0.65]{$#3\alpha$}}
}{\str_if_eq:nnTF{#1}{b}
{\IfValueTF#2
{#2{#3\alpha}}
{{#3\alpha}}
}{\IfValueTF#2
{#2{\scalebox{#1}[#1]{$#3\alpha$}}}
{\scalebox{#1}[#1]{$#3\alpha$}}
}}}}
\providecommand{\ag}{\@ag}}\else{}\fi
\ExplSyntaxOff
\ProcessOptions\relax
\endinput
因此,当我输入 时\usepackage[gbb]{mworks}
,我希望 LaTeX 1) 找到包 2) 将其设置\gbb
为 1,因此 3) 找到\ag
并在我的文档中使用它。问题是,步骤 1 顺利进行,步骤 2 就是行不通。因此我总是得到Undefined control sequence
。\ag
为什么会这样?
答案1
您的代码中有几个错误。
在条件定义之后执行
\ProcessOptions
,此时计数器的值仍为 0不要定义
\Copyright
,这完全没用,而且可能会取代其他包中定义的命令浪费一个计数器来决定是否给出了一个选项
使用
\IfValueTF#2
而不是正确的\IfValueTF{#2}
在 后面添加一个括号
\ifnum\gbb=1
,这使得定义成为局部定义,因此即使\ProcessOptions
放在正确的位置也会消失
\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{mworks}[2014/04/01 v. 0.2]
\RequirePackage{amsmath,amssymb,amsfonts}
\RequirePackage{multirow}
\RequirePackage{xparse}
\RequirePackage{thmtools}
\let\mworks@gbb=F
\DeclareOption{gbb}{\let\mworks@gbb=T}
\ProcessOptions\relax
\if\mworks@gbb T
\ExplSyntaxOn
\NewDocumentCommand{\ag}{O{0.85}oO{}}
{
\str_if_eq:nnTF{#1}{n}
{\IfValueTF{#2}
{#2{\scalebox{0.85}[0.85]{$#3\alpha$}}}
{\errmessage{Too few arguments}\errhelp{If you specify `n' as the first argument, the second optional argument must be specified, otherwise use a number as the first argument or specify no optional argument.}}
}{\str_if_eq:nnTF{#1}{e}
{\IfValueTF{#2}
{#2{\scalebox{0.65}[0.65]{$#3\alpha$}}}
{\scalebox{0.65}[0.65]{$#3\alpha$}}
}{\str_if_eq:nnTF{#1}{b}
{\IfValueTF{#2}
{#2{#3\alpha}}
{{#3\alpha}}
}{\IfValueTF{#2}
{#2{\scalebox{#1}[#1]{$#3\alpha$}}}
{\scalebox{#1}[#1]{$#3\alpha$}}
}}}
\ExplSyntaxOff
\fi
\endinput
代码的改进版本。
\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{mworks}[2014/04/01 v. 0.2]
\RequirePackage{amsmath,amssymb,amsfonts}
\RequirePackage{multirow}
\RequirePackage{xparse}
\RequirePackage{thmtools}
\let\mworks@gbb=F
\DeclareOption{gbb}{\let\mworks@gbb=T}
\ProcessOptions\relax
\ExplSyntaxOn
\NewDocumentCommand{\ag}{O{0.85}oO{}}
{
\str_case:nnF { #1 }
{
{ n } {
\IfValueTF{#2}
{#2{\scalebox{0.85}[0.85]{$#3\alpha$}}}
{\msg_error:nn { mworks } { ag/toofew }}
}
{ e } { \IfValueT{#2}{#2} \scalebox{0.65}[0.65]{$#3\alpha$} }
{ b } { \IfValueT{#2}{#2} {#3\alpha} }
}
{
\IfValueT{#2}{#2} \scalebox{#1}[#1]{$#3\alpha$}
}
}
\msg_new:nnnn { mworks } { ag/toofew }
{
Too~few~arguments
}
{
If~you~specify~`n'~as~the~first~argument,~the~second~optional~%
argument~must~be~specified,~otherwise~use~a~number~as~the~%
first~argument~or~specify~no~optional~argument.
}
\ExplSyntaxOff
\if\mworks@gbb F
\renewcommand{\ag}{\alpha}
\fi
\endinput
这是一个测试文件
\documentclass{article}
\usepackage[gbb]{mworks}
\begin{document}
$\ag$
$\ag[n]$ ERROR!
$\ag[n][x]$
$\ag[n][x][y]$
$\ag[e]$
$\ag[e][x]$
$\ag[e][x][y]$
$\ag[b]$
$\ag[b][x]$
$\ag[b][x][y]$
$\ag[.5]$
$\ag[.5][x]$
$\ag[.5][x][y]$
\end{document}
我看不出这样的命令有什么用。