自定义 LaTeX 文档类中颜色包的控制序列未定义

自定义 LaTeX 文档类中颜色包的控制序列未定义

我有以下显示的类文件:

%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{puseniorthesis}
         [2008/05/02 v1.4.1 Princeton University Thesis class]

\RequirePackage{setspace}
\RequirePackage{zref-totpages}

%report type option
\newif\if@progress
\@progressfalse
\DeclareOption{progress}{\@progresstrue}



%copy type option
\newif\if@filecopy
\@filecopyfalse
\DeclareOption{filecopy}{\@filecopytrue}
\newif\if@advisercopy
\@advisercopyfalse
\DeclareOption{advisercopy}{\@advisercopytrue}
\newif\if@readercopy
\@readercopyfalse
\DeclareOption{readercopy}{\@readercopytrue}

%abstract
\long\def\@abstract{\@latex@error{No \noexpand\abstract given}\@ehc}

%frontmatter numbered with roman numerals, mainmatter with arabic
\newcommand*{\frontmatter}{
  \pagenumbering{roman}
 }
\newcommand*{\mainmatter}{\pagenumbering{arabic}}
\newcommand*{\backmatter}{\pagenumbering{Roman}}
\newcommand*{\makelot}{}
\newcommand*{\makelof}{}
\newcommand*{\makelos}{}
\newcommand*{\begincmd}{
\onehalfspacing
\frontmatter\maketitlepage\makecopyrightpage\makeabstract
\makeacknowledgements\makededication\tableofcontents\clearpage
\makelot\clearpage\makelof\clearpage\makelos
\clearpage\mainmatter
\renewcommand{\baselinestretch}{1.5}
}

%define required vars
\def\@deptpref{Department of}
\def\departmentprefix#1{\gdef\@deptpref{#1}}
\def\classyear#1{\gdef\@classyear{#1}}
\def\adviser#1{\gdef\@adviser{#1}}
\def\reader#1{\gdef\@reader{#1}}
\def\dept#1{\gdef\@dept{#1}}
\def\submitdate#1{\gdef\@submitdate{#1}}
\long\def\acknowledgements#1{\gdef\@acknowledgements{#1}}
\def\course#1{\gdef\@course{#1}}

%define optional vars
\def\dedication#1{\gdef\@dedication{#1}}
\def\extranotes#1{\gdef\@extranotes{#1}}
\extranotes{}


%title page
\newcommand{\maketitlepage}{{
  \thispagestyle{empty}
  \sc
  \vspace*{0in}
  \begin{center}
    \LARGE \@title
  \end{center}
  \vspace{.1in}
  \begin{center}
   \large \@author,~\@classyear
  \end{center}
  \vspace{.3in}
  \begin{center}
   Submitted to the\\
    \@deptpref~\@dept\\
     Princeton University \\
   in partial fulfillment of the requirements of\\
   Undergraduate Independent Work.\\ 
   \vspace{.3in}
   \if@progress
   Progress Report\\
   \else
   Final Report\\
   \fi
  \vspace{.3in}
    \@submitdate
  \end{center}

  \vfill

 % \begin{flushright}
 \hangindent=11.5cm\hangafter=0
  \noindent\@adviser\\
  \@reader\\
  \@course\\
  \ztotpages \,  pages\\
  \if@filecopy
   File Copy
  \fi
  \if@advisercopy
   Adviser Copy
  \fi
   \if@readercopy
   reader Copy
  \fi
  \ifx\@extranotes\@empty
    \relax
  \else
     \\ \@extranotes\\
  \fi
 % \end{flushright}

  \clearpage
  }}

 %copyright page
\newcommand*{\makecopyrightpage}{
  \thispagestyle{empty}
  \vspace*{0in}
  \begin{center}
    \copyright\ Copyright by \@author, \number\year. \\
    All Rights Reserved
  \end{center}

  This  \if@progress progress report
  \else
  thesis
   \fi
   represents my own work in accordance with University regulations.

   %\vspace{1in}
   %\@author \hspace{0.5cm} \makebox[2in]{\hrulefill}

  \clearpage}
\newcommand*{\makeabstract}{
  \newpage
  \addcontentsline{toc}{section}{Abstract}
  \begin{center}
  \Large \textbf{Abstract}
  \end{center}
  \@abstract
  \clearpage
  }

  %acknowledgements
\def\makeacknowledgements{
  \ifx\@acknowledgements\undefined
  \else
    \addcontentsline{toc}{section}{Acknowledgements}
    \begin{center}
      \Large \textbf{Acknowledgements}
    \end{center}
    \@acknowledgements
    \clearpage
  \fi
  }

  %dedication page
\def\makededication{
  \ifx\@dedication\undefined
  \else
    \vspace*{1.5in}
    \begin{flushright}
      \@dedication
    \end{flushright}
    \clearpage
  \fi
  }

 %various optional lists 
\DeclareOption{myorder}{
  \renewcommand*{\begincmd}{
  \onehalfspacing
  }}
\DeclareOption{lot}{\renewcommand*{\makelot}{
  \addcontentsline{toc}{section}{List of Tables}\listoftables}}
\DeclareOption{lof}{\renewcommand*{\makelof}{
  \addcontentsline{toc}{section}{List of Figures}\listoffigures}}
\DeclareOption{los}{
  \renewcommand*{\makelos}{
    \newcommand*\l@symbol{\@dottedtocline{1}{1.5em}{3.5em}}
    \addtocontents{los}{\protect\addvspace{10\p@}}%
    \chapter*{List of Symbols\@mkboth {LIST OF SYMBOLS}{LIST OF SYMBOLS}}
    \@starttoc{los}
    \addcontentsline{toc}{section}{List of Symbols}
  }
}
\def\addsymbol#1#2{%
    \addtocontents{los}{\protect \contentsline {symbol}{\protect \numberline {#1}{\ignorespaces #2}}{\thepage}}
}

%inherits report class and associated options
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{report}}
\ProcessOptions
%% Not necessary to specify the point size - we inherit it from above
%% \LoadClass[12pt]{report}
\LoadClass{report}
\setlength{\oddsidemargin}{.5in}   %{.4375in}
\setlength{\evensidemargin}{.5in} %{.4375in}
\setlength{\topmargin}{-.5in}  %{-.5625in}
\setlength{\textheight}{9in}
\setlength{\textwidth}{6in}
\long\def\abstract#1{\gdef\@abstract{#1}}
\AtBeginDocument{\begincmd}
\endinput
%% 
%% End of file `puthesis.cls'.

问题是,当我尝试做一件简单的事情时,\usepackage{color}我遇到了这样的问题:

! Undefined control sequence.
\set@color ...\@pdfcolorstack push{\current@color
}\aftergroup \reset@color
l.55 \begin{document}

但是,当我仅对文件执行以下操作时,article.cls没有任何问题:

\documentclass{article}

\usepackage{listings}
\usepackage{color}
\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
    language=c,
    basicstyle=\scriptsize,
    upquote=true,
    aboveskip={1.5\baselineskip},
    columns=fullflexible,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    identifierstyle=\ttfamily,
    keywordstyle=\color[rgb]{0,0,1},
    commentstyle=\color[rgb]{0.133,0.545,0.133},
    stringstyle=\color[rgb]{0.627,0.126,0.941},
}

\begin{document}

\begin{lstlisting}

#include <stdio.h>

int main() 
{
    // A line comment
    printf("A really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, REALLY long line. && & \n");
    return 0;
}

\end{lstlisting}

\end{document}

puseniorthesis.cls那么我需要添加一些什么以使color包正常工作吗?

编辑:我使用的导致错误的确切代码在这里;

\documentclass[lof,lot,los,12pt,advisercopy]{puseniorthesis}
%options to class:
% lof - generate list of figures
% lot - generate list of tables
% Xpt - font size
% filecopy - note that this is 'File Copy' on title page
% advisercopy - note that this is 'Adviser Copy' on title page
% readercopy - note that this is 'Reader Copy' on title page
% progress - change title page to say 'Progress Report' instead of 'Final Report'

\usepackage{graphicx} %need for \includegraphics
\usepackage{amsmath}

\usepackage{listings}
\usepackage{color}
\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
    language=c,
    basicstyle=\scriptsize,
    upquote=true,
    aboveskip={1.5\baselineskip},
    columns=fullflexible,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    identifierstyle=\ttfamily,
    keywordstyle=\color[rgb]{0,0,1},
    commentstyle=\color[rgb]{0.133,0.545,0.133},
    stringstyle=\color[rgb]{0.627,0.126,0.941},
}

%required inputs:
\author{My Name}
\classyear{`13}
\adviser{My Adviser}
\reader{My Reader}
\title{Thesis Title}
\dept{Mechanical and Aerospace Engineering}
\submitdate{May 2, 2013}
\course{MAE something}
\abstract{\input abstract} %read from file abstract.tex

%optional inputs (any of these can be omitted)
\extranotes{These are a few lines of optional notes} 
\dedication{To myself.}
\acknowledgements{Thank you very much.}

\numberwithin{equation}{section}

\begin{document}

%\include{ch1} %read from file ch1.tex
%\include{ch2} %read from file ch2.tex

%\appendix

%\include{app1}  %read from file app1.tex


\end{document}

答案1

该类用于\AtBeginDocument排版。但这是不允许的,摘自《LaTeX Companion》第二版第 884 页:

但请注意,钩子中的代码\AtBeginDocument是前言的一部分。因此,限制了可以放在那里的内容;特别是,不能进行排版。

在这种情况下,包color尚未完成设置,即定义\defaultcolor。排版代码调用\normalcolor分配\current@color给尚未定义的\defaultcolor

解决方法:

\let\savedbegincmd\begincmd
\let\begincmd\relax
\begin{document}
\savedbegincmd

可以通过更改线路来修复该类

\AtBeginDocument{\begincmd}

\RequirePackage{etoolbox}
\AfterEndPreamble{\begincmd}

使用包中更合适的钩子etoolbox

相关内容