etoolbox ifblank error with xkeyval

1,033

Solution 1

The right test to use is \ifdefempty

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{gaussler}[2013/07/19]

\RequirePackage{xkeyval}

\def\myclass@firstoption{}
\def\myclass@secondoption{}

\DeclareOptionX{firstoption}[]{\def\myclass@firstoption{#1}}
\DeclareOptionX{secondoption}[]{\def\myclass@secondoption{#1}}


\ProcessOptionsX\relax

\LoadClass{memoir}
\RequirePackage{etoolbox}

\title{\ifdefempty{\myclass@firstoption}{% check if firstoption is blank
                %do nothing
    }{%
        \myclass@firstoption: %
    }%
    \myclass@secondoption
}
\endinput

\ifdefempty{⟨control sequence⟩}{⟨true⟩}{⟨false⟩}
Expands to ⟨true⟩ if the ⟨control sequence⟩ is defined and is a parameterless macro whose replacement text is empty, and to ⟨false⟩ otherwise. In contrast to \ifx, this test ignores the prefixes of the ⟨command⟩.

Solution 2

From the documentation of etoolbox:

\ifblank{<string>}{<true>}{<false>}
Expands to <true> if the <string> is blank (empty or spaces), and to <false> otherwise. The <string> is not expanded in the test.

In myclass.cls the macro is called with:

\ifblank{\myclass@firstoption}{}{\myclass@firstoption: }

Thus, the unexpanded first argument contains \myclass@firstoption that is not "blank". Two \expandafters help:

\expandafter\ifblank\expandafter{\myclass@firstoption}{}{\myclass@firstoption: }
Share:
1,033

Related videos on Youtube

Gaussler
Author by

Gaussler

Updated on July 20, 2020

Comments

  • Gaussler
    Gaussler almost 3 years

    I am trying to create my first class, which is basically just my ordinary preamble with a set of options. These options are handled by xkeyval using the following syntax:

    \NeedsTeXFormat{LaTeX2e}
    \ProvidesClass{myclass}[2013/07/19]
    
    \RequirePackage{xkeyval}
    
    \DeclareOptionX{firstoption}[]{\newcommand\myclass@firstoption{#1}}
    \DeclareOptionX{secondoption}[]{\newcommand\myclass@secondoption{#1}}
    
    \presetkeys{myclass.cls}{}{firstoption, secondoption} %sets all keys to their default values
                                         %unless set by the user
    
    
    \ProcessOptionsX\relax
    
    \LoadClass{memoir}
    \RequirePackage{etoolbox}
    
    \title{\ifblank{\myclass@firstoption}{% check if firstoption is blank
                    %do nothing
        }{%
            \myclass@firstoption: %
        }%
        \myclass@secondoption%
    }
    

    The last piece of code is supposed to output firstoption: secondoption if firstoption is not blank, but only secondoption in case of the opposite.

    However, for strange reasons, the colon always appears, independently of whether or not firstoption is set.