在 .cls 檔案內使用 \cleardoublepage 重新定義 \chapter 和 \chapter*

在 .cls 檔案內使用 \cleardoublepage 重新定義 \chapter 和 \chapter*
\newcommand{\Chapter}[1]{{\let\cleardoublepage\relax\chapter{#1}}}

工作正常。如何對 .cls 檔案中加註星號 (*) 的版本執行相同操作?

(其他人可能會感興趣:在主文檔中定義它有什麼區別嗎?)

答案1

您已表示您想重新定義\chapter\chapter*。為此,我建議在課堂上進行更深層的研究,這不是初學者的任務。

\chapter無論如何,您都可以建立一個宏,其中包含帶有星號的版本並使用您的類別中編寫的命令。

一個非常基本的方法如下:

\documentclass{scrbook}

\newcommand{\MyChapter}{%
    \let\cleardoublepage\empty
    \clearpage
    \chapter
}

\begin{document}

\tableofcontents

\MyChapter{Introduction}
some text

\MyChapter[My Introduction]{Introduction}
some text

\MyChapter*{Introduction}
some text

\end{document}

命令\MyChapter擦除\cleardoublepage,它執行\clearpage\chapter。它將具有舊版本\chapter的所有功能,因為\chapter它是最後執行的,並且將您在後面添加的任何內容作為參數\MyChapter

然而後者這不是一個好的做法,因為命令\cleardoublepage變得無用,並且刪除乳膠命令通常會導致功能不良。

恕我直言,「體面」的做法可以是保存\cleardoublepage並稍後恢復它:

\documentclass{scrbook}

\newcommand{\ResCdp}{%
    \let\cleardoublepage\cdp
}

\newcommand{\MyChapter}{%
    \let\cdp\cleardoublepage
    \let\cleardoublepage\empty
    \clearpage
    \chapter
}

\begin{document}

\tableofcontents

\MyChapter{Introduction}\ResCdp
some text

\MyChapter[My Introduction]{Introduction}\ResCdp
some text

\cleardoublepage

\MyChapter*{Introduction}\ResCdp
some text

\end{document}

這有點難以讀/寫,但可以安全地完成任務。

為了避免\ResCdp在不刪除\cleardoublepage和保留所有\chapter功能的情況下使用它,需要付出更多的努力。

答案2

您已表明您使用scrbook文檔類別。如果你想重新定義\chapter(and \chapter*) 來執行\clearpage而不是\cleardoublepage,我建議你載入etoolbox套件並使用它的\patchcmd巨集來修改chapter巨集:

\usepackage{etoolbox}
\patchcmd{\chapter}{\cleardoublepage}{\clearpage}{}{}

答案3

如果scrbook使用選項openany\cleardoublepage手動插入 s。

這並不完全是問題的答案,但我還是決定用這個方法。

相關內容