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!!

user19832
  • 1,585

1 Answers1

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).

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 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 04 '16 at 12:32