0

I'm trying to access the boolean macro I defined with my class. Whenever I access the boolean within the class, it works fine. However, if try to access it from the main document (using the same code), I get an Undefined control sequence. \ifMY.

testclass.cls

%! Class = testclass

\NeedsTeXFormat{LaTeX2e} \ProvidesClass{testclass}[2023/11/29 Minimum Working Example Class]

\LoadClass[t,xcolor={x11names},aspectratio=169]{beamer}

\RequirePackage{kvoptions} \SetupKeyvalOptions{family=MY,prefix=MY@} \DeclareBoolOption{optA} \ProcessKeyvalOptions*

\newcommand{\test}{\ifMY@optA true \else false \fi}

test.tex

% Preamble
\documentclass[optA=false]{testclass}

% Document \begin{document}

\begin{frame}{Works fine.} \test \end{frame}

\begin{frame}{Has an error.} \ifMY@optA true \else false \fi \end{frame}

\end{document}

How can I use the if within the document, or would I have to create a command in the class, similar to \test?

  • 1
    for a new class I would strongly suggest that you use the new Key option handling provided by the latex format rather than kvoptions see for example https://tex.stackexchange.com/a/693340/1090 – David Carlisle Dec 03 '23 at 20:20

1 Answers1

2

@ are not allowed in the main document for the macro name. Take a look this post. To make it work, several options you can do.

  1. Get rid of the @ sign in prefix.

  2. Add \makeatletter and \makeatother before and after the frame env.

  3. Copy the \ifMY@optA command to make a version without @.

I prefer the last option.

Here is the code for testclass.cls:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{testclass}[2023/11/29 Minimum Working Example Class]

\LoadClass[t,xcolor={x11names},aspectratio=169]{beamer}

\RequirePackage{kvoptions} \SetupKeyvalOptions{family=MY,prefix=MY@} \DeclareBoolOption{optA} \ProcessKeyvalOptions*

\let\ifmyoptA\ifMY@optA

\newcommand{\test}{\ifMY@optA true \else false \fi}

Here is the code for main document:

% Preamble
\documentclass[optA=false]{testclass}

% Document \begin{document}

\begin{frame}{Works fine.} \test \end{frame}

\begin{frame}{Has an error.} \ifmyoptA true \else false \fi \end{frame}

\end{document}

enter image description here

Tom
  • 7,318
  • 4
  • 21