9

I I have a usrdefn.tex file in which I defined some commends/environments used a lot, also there are two version of theorem environments based on the language (english/chinese). Then I want to pass an option to the documentclass and choose the block automatically. The mme looks like:

FOR CHINESE:

\documentclass[zh]{ctexart}
\usepackage{amsthm}
\input usrdefn
\begin{document}
 \begin{thm}
    中文定理
\end{thm}
\end{document}

FOR ENGLISH

\documentclass[en]{amsart}
\begin{document}
\begin{thm}
 English Theorem
\end{thm}
\end{document}

with the usrdefn.tex

\usepackage{etoolbox}
\newtoggle{langen}
\makeatletter
\DeclareOption{en}{\toggletrue{langen}}
\DeclareOption{zh}{\togglefalse{langen}}
\makeatother
\iftoggle{langen}{
\newtheorem{thm}{Theorem}
}{
\newtheorem{thm}{定理}
}

This is not work, since it tells me that

|4 error| \RequirePackage or \LoadClass in Options Section.

OK, I see, I need to \ProcessOptions after the \DeclareOption stuff, which solved my problem!!

  • 1
    do you want to add your solution as an answer? – Runar Jul 4 '16 at 6:05
10

Better: use the structure of LaTeX 2ε by creating a personal package. Put this in a usrdefn.sty file (that you can keep in the current directory, or better, in the standard places):

\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{usrdefn}[2016/07/04 My personal macros v1.0]
\RequirePackage{etoolbox} % <- conventionally, use this and not \usepackage in .sty files
\newtoggle{langen}
\DeclareOption{en}{\toggletrue{langen}}
\DeclareOption{zh}{\togglefalse{langen}}
\ProcessOptions
\iftoggle{langen}{
\newtheorem{thm}{Theorem}
}{
\newtheorem{thm}{定理}
}

You do not need makeatletter here (it's the default in .sty files). Now you can use it in your main document as

\documentclass[en]{amsart}
\usepackage{usrdefn}
...

(Notice that class options "percolate" down to packages. You can also use \usepackage[en]{usrdefn} if you prefer).

  • Yes, in fact I know how to write it in a package, but I would prefer to use it directly(and more freely) instead to provide a package. But thanks for your response! – user19832 Jul 4 '16 at 12:32

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.