4

Assume I have a bunch of flags which I want to pass to my class, how do I do this? If you see the example below, I am looking for passing flags in the form:

key=<boolean value from flag>

to the document class.

If I run pdflatex on the following code, followed by grep TEST test.log I only get:

TEST: MyClass

Now I know that I can workaround this by: \iftodos \wlog{TEST: TODO visible} \fi because this \iftodos is also visible in the document class, but I am just curious how to solve this very neat.

E.g:

MWE

\newif\iftodos
\newif\ifshowfigures

%Set the flags (usually I do this by input a flags.tex)
\todostrue
\showfigurestrue

\documentclass[classtodo=\@todos,classshowfigures=\@showfigures]{myclass}

\begin{document}
dummy
\end{document}

MYCLASS.CLS

\ProvidesClass{myclass}

\newif\if@classtodos
\@classtodosfalse
\newif\if@classshowfigures
\@classshowfiguresfalse

\DeclareOption{classtodos}{\@classtodostrue}
\DeclareOption{classshowfigures}{\@classshowfigurestrue}
\ProcessOptions\relax

\LoadClass{book}

\wlog{TEST: MyClass}

\if@classtodos
   \wlog{TEST: TODO visible}
else
   \wlog{TEST: TODO not visible}
\fi
  • You don't have a \ProcessOptions line. Also, the values you are passing to the keys do nothing and everything before \documentclass has no effect as far as I can tell. – cfr Dec 27 '15 at 14:31
  • Thanks @cfr. I added the ProcessOptions but then I end up with. ! Missing \endcsname inserted. <to be read again> \spacefactor l.10 \ProcessOptions\relax. Flags set prior to documentclass do work – robert Dec 27 '15 at 14:39
  • Yes but the flags you are setting prior to \documentclass have no effect because they are different \ifs than the ones you test in the class. Moreover, the key=value syntax is not default. To enable something like this you need to set it up using e.g. one of the packages for managing values in this way. As it is, you are passing the option <key>=<value> and not setting <key> to <value>. LaTeX can't parse key/value syntax by default. – cfr Dec 27 '15 at 14:47
  • What is the point of \iftodos and \ifshowfigures? These do not seem to do anything at all. You don't test their values and they are not mentioned in the class or the document. If these were defined in the class and used in the class, then the idea of setting their values makes sense. But, as it is, I'm just not clear what you want to do with them. You say that you don't want to 'hard code' the flags. My answer doesn't hard code the conditionals' values. (At least, they are set, but only to defaults.) But it does define and use those conditionals. – cfr Dec 27 '15 at 18:14
  • Why can't you input flags.tex or whatever after \documentclass? Then you can override default values if you wish. That's how I usually do this. (I also use the extended kv keys stuff a bit, but mostly try to use the expl3 stuff for new non-TikZ key-value things and pgfkeys for TikZ stuff. – cfr Dec 27 '15 at 18:17
2

The <key>=<value> syntax requires additional support: it is not parsed by default. So <key>=<value> is just read as the name of an option by LaTeX. To have it read <key>=<value> as setting <key> to <value>, you must set this up. There are various packages to do this in different ways.

In this case, however, you don't need this as you only care whether the option is set or not i.e. you are only implementing a boolean test and this can be done without any additional apparatus.

\begin{filecontents}{myclass.cls}
\ProvidesClass{myclass}

\newif\if@todos
\@todosfalse
\newif\if@showfigures
\@showfiguresfalse

\DeclareOption{todos}{\@todostrue}
\DeclareOption{showfigures}{\@showfigurestrue}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{book}}
\ExecuteOptions{}
\ProcessOptions*

\LoadClass{book}

\wlog{TEST: MyClass}

\if@todos
   \wlog{TEST: TODO visible}
\else
   \wlog{TEST: TODO not visible}
\fi
\end{filecontents}
\documentclass[todos,showfigures]{myclass}

\begin{document}
dummy
\end{document}

writes the following to the log file:

TEST: MyClass
TEST: TODO visible
  • Yes @cfr but now the flags to documentclass are hardcoded. My flags/parameters are stored in flags.tex. This because I can easily manipulate them by shell programming. In your setup I need to grep for document class, then insert certain flags as parameter. – robert Dec 27 '15 at 14:49
  • Maybe you should edit your question so that it reflects what you want to do a bit better. Right now, it is not at all clear to me. But, as I say, any key-value syntax needs to be configured. I don't see how it will help here but, if you need it, you need to decide which implementation you want to use. There is the kv stuff and extensions or pgfkeys or the expl3 syntax and so on. They can all implement key-value syntax but in different ways. – cfr Dec 27 '15 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.