4

I'm writing a package but I'm having a little trouble. Right now, the package goes:

%!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

So when I type \usepackage[gbb]{mworks}, I expect LaTeX 1) to find the package 2) to set \gbb to 1 and therefore 3) to find \ag and have it available in my document. Trouble is, step 1 goes fine, step 2 just won't work. And therefore I always get Undefined control sequence for \ag. Why does that happen?

1
  • You have to \ProcessOptions before doing the conditional definition of \ag. When LaTeX finds \ifnum\gbb=1 the counter still has value 0.
    – egreg
    Apr 1, 2014 at 19:20

1 Answer 1

7

There are several errors in your code.

  1. Doing \ProcessOptions after the conditional definition, when the counter will still have value 0

  2. Don't define \Copyright, which is completely useless and may supersede commands defined in other packages

  3. Wasting a counter for deciding if an option has been given

  4. Using \IfValueTF#2 instead of the correct \IfValueTF{#2}

  5. Adding a brace after \ifnum\gbb=1, which makes the definitions local and so disappear even if \ProcessOptions is put at the right place

\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

An improved version of the code.

\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

Here's a test file

\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}

enter image description here

I can't see how such a command can be useful.

13
  • Even better: Use a package that offers real booleans - like ifthen. Apr 2, 2014 at 9:06
  • @MartinSchröder I didn't try to optimize the code; actually I can't understand the reason why one would want to enable a command only if some option is given. For this a primitive boolean is, in my opinion, better, because \ifthenelse would require doubling #.
    – egreg
    Apr 2, 2014 at 9:25
  • @egreg \str_case:nnF gives me Undefined control sequence
    – MickG
    Apr 17, 2014 at 15:05
  • @MickG Update your TeX distribution.
    – egreg
    Apr 17, 2014 at 15:09
  • @egreg I've updated it around New Year's Day 2014. TeX Live Utility v. 1.17, TeXShop v. 3, console starts with This is XeTeX, Version 3.1415926-2.5-0.9999.3 (TeX Live 2013) restricted \write18 enabled. entering extended mode.26. Is there anything out of date in that?
    – MickG
    Apr 17, 2014 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .