新的第一章页面创建失败

新的第一章页面创建失败

我收到错误“缺少数字,视为零 MWE.tex”,并紧急停止。我尝试删除每章第一页的空格,并在其上设置页眉和页脚。

梅威瑟:

\documentclass[a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\makeatletter
\renewcommand{\chapter}{
\if@openright
    \cleardoublepage\else\clearpage
\fi
    \thispagestyle{fancy}%
    \global\@topnum\z@
    \@afterindentfalse
    \secdef\@chapter\@schapter}
\def\@chapter[#1]#2{%\ifnum 
\c@secnumdepth >\m@ne
    \refstepcounter{chapter}
    \typeout{\@chapapp\thechapter.}
    \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
%\else
%   \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
%\fi
\chaptermark{#1}
\addtocontents{lof}{\protect\addvspace{10\p@}}
\addtocontents{lot}{\protect\addvspace{10\p@}}
\if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]
\else
    \@makechapterhead{#2}
    \@afterheading
\fi}
\def\@makechapterhead#1{
    \vspace*{0\p@}
    {\parindent \z@ \raggedright \normalfont
        \ifnum \c@secnumdepth >\m@ne
            \huge\bfseries \@chapapp \space \thechapter
            \par\nobreak
            \vskip 20\p@
        \fi
        \interlinepenalty\@M
        \Huge \bfseries #1\par\nobreak
        \vskip 20\p@
    }}
\def\@schapter#1{\if@twocolumn
        \@topnewpage[\@makeschapterhead{#1}]
    \else
        \@makeschapterhead{#1}
        \@afterheading
    \fi}
\def\@makeschapterhead#1{
    \vspace*{-10\p@}
    {\parindent \z@ \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  #1\par\nobreak
        \vskip 20\p@
    }}
\makeatother
\begin{document}
    \chapter{Asdf}
        \lipsum[1]
\end{document}

答案1

您的示例有两个问题。首先,您使用的是\thispagestyle未加载的命令fancyhdr。这将引发undefined control sequence

第二个问题是你(我猜)注释了\ifnum但留下了条件(\c@secnumdepth >\m@ne)。如果你取消注释,\ifnum它会起作用,但可能不像预期的那样:

\documentclass[a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{fancyhdr}% <---- Included this package
\makeatletter
\renewcommand{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi % Typ II
                                        \thispagestyle{fancy}%
                                        \global\@topnum\z@
                                        \@afterindentfalse
                                        \secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum% <---- This was commented
\c@secnumdepth >\m@ne
                                                 \refstepcounter{chapter}%
                                                 \typeout{\@chapapp\thechapter.}
                                                 \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
%                                       \else
%                                           \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
%                                       \fi
                                        \chaptermark{#1}
                                        \addtocontents{lof}{\protect\addvspace{10\p@}}
                                        \addtocontents{lot}{\protect\addvspace{10\p@}}
                                        \if@twocolumn
                                            \@topnewpage[\@makechapterhead{#2}]%
                                        \else
                                            \@makechapterhead{#2}%
                                            \@afterheading
                                        \fi}
\def\@makechapterhead#1{
    \vspace*{0\p@}
    {\parindent \z@ \raggedright \normalfont
        \ifnum \c@secnumdepth >\m@ne
                \huge\bfseries \@chapapp \space \thechapter
                \par\nobreak
                \vskip 20\p@
        \fi
        \interlinepenalty\@M
        \Huge \bfseries #1\par\nobreak
        \vskip 20\p@
    }}
\def\@schapter#1{\if@twocolumn
                                     \@topnewpage[\@makeschapterhead{#1}]
                                 \else
                                     \@makeschapterhead{#1}
                                     \@afterheading
                                 \fi}
\def\@makeschapterhead#1{
    \vspace*{-10\p@}
    {\parindent \z@ \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  #1\par\nobreak
        \vskip 20\p@
    }}
\makeatother
\begin{document}
    \chapter{Asdf}
        \lipsum[1]
\end{document}

或者,如果你不想要条件,你可以将条件测试(\c@secnumdepth >\m@ne)全部注释掉:

\documentclass[a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{fancyhdr}% <---- Included this package
\makeatletter
\renewcommand{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi % Typ II
                                        \thispagestyle{fancy}%
                                        \global\@topnum\z@
                                        \@afterindentfalse
                                        \secdef\@chapter\@schapter}
\def\@chapter[#1]#2{%\ifnum
%\c@secnumdepth >\m@ne% <---- This was un-commented
                                                 \refstepcounter{chapter}%
                                                 \typeout{\@chapapp\thechapter.}
                                                 \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
%                                       \else
%                                           \addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
%                                       \fi
                                        \chaptermark{#1}
                                        \addtocontents{lof}{\protect\addvspace{10\p@}}
                                        \addtocontents{lot}{\protect\addvspace{10\p@}}
                                        \if@twocolumn
                                            \@topnewpage[\@makechapterhead{#2}]%
                                        \else
                                            \@makechapterhead{#2}%
                                            \@afterheading
                                        \fi}
\def\@makechapterhead#1{
    \vspace*{0\p@}
    {\parindent \z@ \raggedright \normalfont
        \ifnum \c@secnumdepth >\m@ne
                \huge\bfseries \@chapapp \space \thechapter
                \par\nobreak
                \vskip 20\p@
        \fi
        \interlinepenalty\@M
        \Huge \bfseries #1\par\nobreak
        \vskip 20\p@
    }}
\def\@schapter#1{\if@twocolumn
                                     \@topnewpage[\@makeschapterhead{#1}]
                                 \else
                                     \@makeschapterhead{#1}
                                     \@afterheading
                                 \fi}
\def\@makeschapterhead#1{
    \vspace*{-10\p@}
    {\parindent \z@ \raggedright
        \normalfont
        \interlinepenalty\@M
        \Huge \bfseries  #1\par\nobreak
        \vskip 20\p@
    }}
\makeatother
\begin{document}
    \chapter{Asdf}
        \lipsum[1]
\end{document}

相关内容