1

I'm writting a package in which the user may choose between three types of documents through declared options when issuing the \usepackage command: undergraduatethesis, technicalreport and academicpaper. For this I'm using xkeyval so people use the command like \usepackage[documenttype=academicpaper]{mypackage}.

I'm organizing the package in different sections, each with a type of configuration (spacing, figure configuration, titles, etc.) and I would like to put my document-type-specific commands in these separate sections, for example:

\if{documenttype}{undergraduatethesis}
    % do something
\fi

\if{documenttype}{academicpaper \OR technicalreport}
    % do some other thing
\fi

I've tried a couple things, but I failed. The method I tried were not exactly like my situation, though.

Is this possible?

Thanks!

  • Sure. Why not? I would use boolean flags from package etoolbox. – Johannes_B Mar 25 '18 at 01:42
  • @Johannes_B Thanks! I'm reading the etoolbox documentation right now, but how could I expain to LaTeX that the argument is the option I declared? \def\documenttype{}? I'll try this. – Johann Hemmer Mar 25 '18 at 02:04

1 Answers1

2

Found an answer myself after realizing I made the mistake everyone warned me about: the placement of \ProcessOptions, or, in my case \ProcessOptionsX, since I'm using the xkeyval package.

I used the following bit if code to assign values to some variables in order to distinguish the three document types (for simplicity I'll call them A, B and C):

\def\A{A} % creates macro named A (not sure if this value matters)
\def\B{B} % creates macro named B
\def\C{C} % creates macro named C

I did that in order to later use the \ifx command and compare two macros. Now, using the xkeyval package's syntax to create package options:

\makeatletter
\define@key{fam}{documenttype}{%
    \ifthenelse{\equal{#1}{A}}{% if user types: documenttype=A
    \def\documenttype{A}}{% assigns value "A" to \documenttype macro
    \relax % not sure if needed
    }

    \ifthenelse{\equal{#1}{B}}{% if user types: documenttype=B
    \def\documenttype{B}}{% assigns value "B" to \documenttype macro
    \relax % not sure if needed
    }

    \ifthenelse{\equal{#1}{C}}{% if user types: documenttype=C
    \def\documenttype{C}}{% assigns value "C" to \documenttype macro
    \relax % not sure if needed
    }
}
\makeatother
\ProcessOptionsX<fam>\relax

Great, now the macro \documenttype has a value defined by the user. Now, we can write codes that will be compiled only when the user has specified a certain value to documenttype, using \ifx.

\ifx \documenttype \A % check if \documenttype is equal to \A
% insert commands for when user chooses A
\else % not sure if needed
\relax % not sure if needed
\fi

\ifx \documenttype \B % check if \documenttype is equal to \B
% insert commands for when user chooses B
\else % not sure if needed
\relax % not sure if needed
\fi

\ifx \documenttype \C % check if \documenttype is equal to \C
% insert commands for when user chooses C
\else % not sure if needed
\relax % not sure if needed
\fi

% exactly HERE is where I was placing \ProcessOptionsX, shame on me!!

That's it! Now I can use any booleans!!

  • 2
    Sorry, but just snippets aren't very helpful in a question, nor in most answers. – Johannes_B Mar 25 '18 at 05:33
  • no, the \relax should not be needed. – jakun Jun 23 '18 at 06:31
  • What are those \ifthenelse statements in the key definition good for? Why not simply \def\documenttype{#1} (or even better \edef\documenttype{#1}) instead? – jakun Jun 23 '18 at 06:42
  • I would recommend to add a check for invalid user input to the key definition: \ifx\documenttype\A \else\ifx\documenttype\B \else\ifx\documenttype\C \else \PackageError{mypackage}{invalid value for key documenttype: \documenttype}{Should be one of \A, \B\space or \C.}\fi\fi\fi – jakun Jun 23 '18 at 06:49