即使没有读取包选项,编译时也会读取其中定义的代码。代码中有什么问题?

即使没有读取包选项,编译时也会读取其中定义的代码。代码中有什么问题?

问题!

我的 tex 文件是......

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass{scrbook}
%%%
\usepackage[capimage]{suave}
%%%
\begin{document}
\frontmatter
\chapterimage{imagetest}    
\tableofcontents
\mainmatter
\chapterimage{imagetest}    
\chapter{Rings and Fields}
A ring is a set on which addition, subtraction, multiplication. A field is a comutative ring wiht  division. 

\section{The life}
The life likes those who likes she.
\end{document}

...

该文件编译没有问题,但如果行

             `\usepackage[capimage]{suave}` is replaced by `\usepackage{suave}`

和命令

                     `\chapterimage{imagetest}` 

在tex文件中的\tableofcontents和\chapter之前被注释掉,编译仍然失败,出现以下错误信息

 Too many }'s. ...age\renewcommand{\thechapterimage}{#1}\fi}
 Extra \fi. \begin
 Environment tikzpicture undefined. \tableofcontents
 Undefined control sequence. \tableofcontents
 

这很奇怪,因为没有加载 suave 包的“capimage”选项,所以不应该读取其中定义的代码,更不用说出现错误了,因为我们验证了代码是正确的。

这段代码有什么问题?

我感谢任何评论。

suave.sty 文件是

%%%
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{suave}[2016/11/11 v1. Pacote de facilidades]
%%%
\RequirePackage{geometry}
\geometry{a4paper,top=3cm,bottom=3cm,left=3cm,right=3cm,headsep=5pt}
%%%
\newif\if@loadcapimage
\DeclareOption{capimage}{\@loadcapimagetrue}
%%%
\ProcessOptions\relax 
%%%
%%% Option capimage
%%%
\if@loadcapimage
%%%
\RequirePackage[svgnames,x11names]{xcolor}
    \definecolor{laranja}{RGB}{243,102,25}%%%{0,128,128}
    \newcommand{\laranja}[1]{{\color{laranja}#1}}
\RequirePackage{tikz,pgf}          
    \usetikzlibrary{positioning}    
    \usetikzlibrary{arrows,shapes}
    \usetikzlibrary{shadows}
    \newcommand{\separador}{:}
\makeatletter
% A switch to conditionally include a picture, implemented by  Christian Hupfer
\newif\ifusechapterimage
\usechapterimagetrue
\newcommand{\thechapterimage}{}%
\newcommand{\chapterimage}[1]{\ifusechapterimage\renewcommand{\thechapterimage}{#1}\fi}%
%%%\newcommand{\autodot}{.} %%% Defined in scrbook class or other package in the komascript.
\def\@makechapterhead#1{%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\begin{tikzpicture}[remember picture,overlay]
\node at (current page.north west)
{\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north west,inner sep=0pt] at (0,0) {\ifusechapterimage\includegraphics[width=\paperwidth]{\thechapterimage}\fi};
\draw[anchor=west] (\Gm@lmargin,-9cm) node [line width=2pt,rounded corners=15pt,draw=laranja,fill=white,fill opacity=0.5,inner sep=15pt]{\strut\makebox[22cm]{}};
\draw[anchor=west] (\Gm@lmargin+.3cm,-9cm) node {\huge\sffamily\bfseries\color{black}\chaptername\,\thechapter\separador\autodot~#1\strut};
\end{tikzpicture}};
\end{tikzpicture}
\else
\begin{tikzpicture}[remember picture,overlay]
\node at (current page.north west)
{\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north west,inner sep=0pt] at (0,0) {\ifusechapterimage\includegraphics[width=\paperwidth]{\thechapterimage}\fi};
\draw[anchor=west] (\Gm@lmargin,-9cm) node [line width=2pt,rounded corners=15pt,draw=laranja,fill=white,fill opacity=0.5,inner sep=15pt]{\strut\makebox[22cm]{}};
\draw[anchor=west] (\Gm@lmargin+.3cm,-9cm) node {\huge\sffamily\bfseries\color{black}#1\strut};
\end{tikzpicture}};
\end{tikzpicture}
\fi\fi\par\vspace*{270\p@}}}

%-------------------------------------------

\def\@makeschapterhead#1{%
\begin{tikzpicture}[remember picture,overlay]
\node at (current page.north west)
{\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north west,inner sep=0pt] at (0,0) {\ifusechapterimage\includegraphics[width=\paperwidth]{\thechapterimage}\fi};
\draw[anchor=west] (\Gm@lmargin,-9cm) node [line width=2pt,rounded corners=15pt,draw=laranja,fill=white,fill opacity=0.5,inner sep=15pt]{\strut\makebox[22cm]{}};
\draw[anchor=west] (\Gm@lmargin+.3cm,-9cm) node {\huge\sffamily\bfseries\color{black}#1\strut};
\end{tikzpicture}};
\end{tikzpicture}
\par\vspace*{270\p@}}
\makeatother
\fi 
%%% 
\setlength{\parindent}{1.25cm} %%% 
%%%
\endinput

答案1

你有

\if@loadcapimage
%%%
\RequirePackage[svgnames,x11names]{xcolor}
    \definecolor{laranja}{RGB}{243,102,25}%%%{0,128,128}
    \newcommand{\laranja}[1]{{\color{laranja}#1}}
\RequirePackage{tikz,pgf}          
    \usetikzlibrary{positioning}    
    \usetikzlibrary{arrows,shapes}
    \usetikzlibrary{shadows}
    \newcommand{\separador}{:}
\makeatletter
% A switch to conditionally include a picture, implemented by  Christian Hupfer
\newif\ifusechapterimage
\usechapterimagetrue
\newcommand{\thechapterimage}{}%
\newcommand{\chapterimage}[1]{\ifusechapterimage\renewcommand{\thechapterimage}{#1}\fi}

因此,如果为\if@loadcapimage假,则将跳过所有代码,但会扫描匹配项\fi以结束该块。

由于这被跳过\newif不是展开,所以\ifusechapterimage没有定义为\if标记(或任何东西),所以 \fi 显示的行末尾与预期不匹配\ifusechapterimage但匹配\if@loadcapimage,所以外部块跳过结束,一切都出错。

将该 \newif行移到更靠前的位置,以便\ifusechapterimage在扫描此部分时将其视为 if 标记并正确匹配。

不相关,但您还应该自动删除\makeatletter@文件中的字母。

相关内容