文档类中出现“选项部分中的 \RequirePackage 或 \LoadClass”错误

文档类中出现“选项部分中的 \RequirePackage 或 \LoadClass”错误

我尝试用 LaTeX 文档(由默认的 Pandoc LaTeX 模板生成,仅供参考)制作文档类。该文档与 XeLaTeX 完美编译,但我无法使文档类正常工作。

以下是jku.cls

\NeedsTeXFormat{LaTeX2e}[2018/04/01]
\ProvidesClass{jku}[2019/01/03 JKU Template]

\RequirePackage{kvoptions}

\DeclareVoidOption{graphics}{
  \RequirePackage{graphicx,grffile}
  \graphicspath{{include/}{figures/}{images/}{../include/}{../figures/}{../images/}}
  \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
  \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
  % Scale images if necessary, so that they will not overflow the page
  % margins by default, and it is still possible to overwrite the defaults
  % using explicit options in \includegraphics[width, height, ...]{}
  \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
}

\DeclareStringOption[english]{lang}
\DeclareStringOption{margin}
\DeclareStringOption{parspacing}[6pt]
\DeclareStringOption[6pt]{parskip}
\DeclareStringOption{linespacing}[1.5]
\DeclareStringOption[12pt]{fontsize}

\DeclareStringOption{lefthead}
\DeclareStringOption{righthead}
\DeclareStringOption{leftfoot}
\DeclareStringOption[\today]{rightfoot}[]

\DeclareBoolOption{tightlists}

\DeclareDefaultOption{
  \PassOptionsToClass{\CurrentOption}{article}
}

\ExecuteOptions{a4paper,12pt}
\ProcessKeyvalOptions*
\LoadClass{article}

\RequirePackage{lmodern,etoolbox,letltxmacro}
\RequirePackage{setspace}
\RequirePackage{amssymb,amsmath,siunitx}
\RequirePackage{ifxetex,ifluatex}
\RequirePackage{fancyhdr,datetime2}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[T1]{fontenc}
  \RequirePackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \RequirePackage{mathspec}
  \else
    \RequirePackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\RequirePackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\notblank{\jku@margin}{
  \RequirePackage[margin=\jku@margin,includehead,includefoot]{geometry}
}{}
\RequirePackage[unicode=true]{hyperref}
\hypersetup{
  pdfborder={0 0 0},
  breaklinks=true
}
\urlstyle{same}  % don't use monospace font for urls
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[shorthands=off,main=\jku@lang]{babel}
\else
  \RequirePackage{polyglossia}
  \setmainlanguage[]{\jku@lang}
\fi

\def\fps@figure{!htp}

\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\LetLtxMacro{\@section}{\section}
\renewcommand{\section}[2][]{
  \hypertarget{#1}{\@section{#2}\label{sec:#1}}
}
\LetLtxMacro{\@subsection}{\subsection}
\renewcommand{\subsection}[2][]{
  \hypertarget{#1}{\@subsection{#2}\label{subsec:#1}}
}
\LetLtxMacro{\@subsubsection}{\subsubsection}
\renewcommand{\subsubsection}[2][]{
  \hypertarget{#1}{\@subsubsection{#2}\label{subsec:#1}}
}

\notempty{\jku@lefthead}{
  \fancyhead[L]{\jku@lefthead}
}{}
\notempty{\jku@righthead}{
  \fancyhead[R]{\jku@righthead}
}{}
\notempty{\jku@leftfoot}{
  \fancyfoot[L]{JKU/\jku@leftfoot}
}{}
\notempty{\jku@rightfoot}{
  \fancyfoot[R]{\jku@rightfoot}
}{}
\AtBeginDocument{\pagestyle{fancy}}

\ifjku@tightlists
  \AtBeginEnvironment{itemize}{
    \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}
  }
\fi

\notblank{\jku@parspacing}{
  \IfFileExists{parskip.sty}{%
    \RequirePackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{\jku@parspacing plus 2pt minus 1pt}
  }
}{}

\notblank{\jku@linespacing}{
  \AtBeginDocument{\setstretch{\jku@linespacing}}
}{}

\endinput

使用示例:

\documentclass[
lang=english,
margin=2.5cm,
lefthead=Example,
leftfoot=Lorem,
rightfoot=Ipsum,
graphics,
linespacing,
parspacing,
tightlists
]{jku}

\begin{document}

...

以下是我遇到的错误:

jku.cls:36: Package keyval Error: linespacing undefined. [\ProcessKeyvalOptions*]
jku.cls:36: Package keyval Error: parspacing undefined. [\ProcessKeyvalOptions*]
jku.cls:36: Package keyval Error: tightlists undefined. [\ProcessKeyvalOptions*]
/usr/local/texlive/2018basic/texmf-dist/tex/latex/translator/translator.sty:93: LaTeX Error: Missing \begin{document}. [\ProcessOptions*]
/usr/local/texlive/2018basic/texmf-dist/tex/latex/translator/translator.sty:93: You can't use `macro parameter character #' in horizontal mode. [\ProcessOptions*]
/usr/local/texlive/2018basic/texmf-dist/tex/latex/translator/translator.sty:94: Paragraph ended before \reserved@a was complete. []
jku.cls:43: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\RequirePackage]
jku.cls:44: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\ifnum]
jku.cls:50: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [  \else]
jku.cls:53: Undefined control sequence. [  \defaultfontfeatures]
jku.cls:53: LaTeX Error: Missing \begin{document}. [  \defaultfontfeatures{L]
jku.cls:58: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\IfFileExists]
jku.cls:61: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [}{}]
jku.cls:61: Undefined control sequence. [}{}]
jku.cls:65: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\RequirePackage]
jku.cls:66: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [\hypersetup]
jku.cls:66: Undefined control sequence. [\hypersetup]
jku.cls:70: Undefined control sequence. [\urlstyle]
jku.cls:75: LaTeX Error: \RequirePackage or \LoadClass in Options Section. [  \setmainlanguage]
jku.cls:75: Undefined control sequence. [  \setmainlanguage]
jku.cls:105: Undefined control sequence. [\notempty]
jku.cls:105: LaTeX Error: Missing \begin{document}. [\notempty{\jku@lefthead]
jku.cls:106: Undefined control sequence. [  \fancyhead]
jku.cls:108: Undefined control sequence. [\notempty]
jku.cls:109: Undefined control sequence. [  \fancyhead]
jku.cls:111: Undefined control sequence. [\notempty]
jku.cls:112: Undefined control sequence. [  \fancyfoot]
jku.cls:114: Undefined control sequence. [\notempty]
jku.cls:115: Undefined control sequence. [  \fancyfoot]
jku.cls:133: LaTeX Error: \RequirePackage or \LoadClass in Options Section. []
test.tex:13: Undefined control sequence. [\begin{document}]

有人能给我指点迷津吗?我确信这是一种新手错误。

答案1

包加载不能发生在类或包选项设置中——这会中断选项的评估。

因此,graphics选项会导致错误,因为它尝试应用\RequirePackage{graphicx,...}

使用“布尔” 宏,可以存储已使用选项\ifgraphicsoption的信息,并在稍后加载包时进行检查。graphics\ifgraphicsoption...\fi

根据定义,a\newif\iffoo最初总是设置为false

我没有在.cls文件中解决其他潜在问题。

\NeedsTeXFormat{LaTeX2e}[2018/04/01]
\ProvidesClass{jku}[2019/01/03 JKU Template]

\RequirePackage{kvoptions}


\DeclareStringOption[english]{lang}
\DeclareStringOption{margin}
\DeclareStringOption{parspacing}[6pt]
\DeclareStringOption[6pt]{parskip}
\DeclareStringOption{linespacing}[1.5]
\DeclareStringOption[12pt]{fontsize}

\DeclareStringOption{lefthead}
\DeclareStringOption{righthead}
\DeclareStringOption{leftfoot}
\DeclareStringOption[\today]{rightfoot}[]

\DeclareBoolOption{tightlists}

\newif\ifgraphicsoption
\DeclareVoidOption{graphics}{
  \graphicsoptiontrue
}



\DeclareDefaultOption{
  \PassOptionsToClass{\CurrentOption}{article}
}


\ExecuteOptions{a4paper,12pt}
\ProcessKeyvalOptions*
\LoadClass{article}

\ifgraphicsoption
\RequirePackage{graphicx,grffile}
\graphicspath{{include/}{figures/}{images/}{../include/}{../figures/}{../images/}}
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\fi

\RequirePackage{lmodern,etoolbox,letltxmacro}
\RequirePackage{setspace}
\RequirePackage{amssymb,amsmath,siunitx}
\RequirePackage{ifxetex,ifluatex}
\RequirePackage{fancyhdr,datetime2}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[T1]{fontenc}
  \RequirePackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \RequirePackage{mathspec}
  \else
    \RequirePackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\RequirePackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\notblank{\jku@margin}{
  \RequirePackage[margin=\jku@margin,includehead,includefoot]{geometry}
}{}
\RequirePackage[unicode=true]{hyperref}
\hypersetup{
  pdfborder={0 0 0},
  breaklinks=true
}
\urlstyle{same}  % don't use monospace font for urls
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \RequirePackage[shorthands=off,main=\jku@lang]{babel}
\else
  \RequirePackage{polyglossia}
  \setmainlanguage[]{\jku@lang}
\fi

\def\fps@figure{!htp}

\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

\LetLtxMacro{\@section}{\section}
\renewcommand{\section}[2][]{
  \hypertarget{#1}{\@section{#2}\label{sec:#1}}
}
\LetLtxMacro{\@subsection}{\subsection}
\renewcommand{\subsection}[2][]{
  \hypertarget{#1}{\@subsection{#2}\label{subsec:#1}}
}
\LetLtxMacro{\@subsubsection}{\subsubsection}
\renewcommand{\subsubsection}[2][]{
  \hypertarget{#1}{\@subsubsection{#2}\label{subsec:#1}}
}

\notempty{\jku@lefthead}{
  \fancyhead[L]{\jku@lefthead}
}{}
\notempty{\jku@righthead}{
  \fancyhead[R]{\jku@righthead}
}{}
\notempty{\jku@leftfoot}{
  \fancyfoot[L]{JKU/\jku@leftfoot}
}{}
\notempty{\jku@rightfoot}{
  \fancyfoot[R]{\jku@rightfoot}
}{}
\AtBeginDocument{\pagestyle{fancy}}

\ifjku@tightlists
  \AtBeginEnvironment{itemize}{
    \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}
  }
\fi

\notblank{\jku@parspacing}{
  \IfFileExists{parskip.sty}{%
    \RequirePackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{\jku@parspacing plus 2pt minus 1pt}
  }
}{}

\notblank{\jku@linespacing}{
  \AtBeginDocument{\setstretch{\jku@linespacing}}
}{}

\endinput

相关内容