刪除標題編號中的最後一點

刪除標題編號中的最後一點

我目前的文檔是這樣處理的:

1. Some chapter
1.1. Some section
1.1.1. Some subsection

Appendix A.
Some appendix

我想要擁有什麼:

1 Some chapter
1.1 Some section
1.1.1 Some subsection

Appendix A: Some appendix

所以我想刪除主要部分編號的最後一個點。對於附錄,我想要一個冒號而不是同一行中的點和標題。

一個小例子,包括使用的所有包:

\documentclass[appendixprefix=true,11pt,a4paper]{scrreprt}
%The packages used in my document
\usepackage{listings}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{enumitem}
\usepackage{chngcntr}
\usepackage[usenames,dvipsnames]{color}
%Appendix definition (Write "Appendix A")
\makeatletter
\newcommand\appendix@numberline[1]{\appendixname\ #1: }
\g@addto@macro\appendix{%
  \addtocontents{toc}{
    \let\protect\numberline\protect\appendix@numberline}%
}
\makeatother

\begin{document}
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\appendix
\chapter{Last chapter}
\end{document}

如何使冒號和標題在同一行。如何刪除標題的最後一個點?


Symbol1 的答案不起作用。錯誤訊息:

Undefined control sequence. \@makechapterhead ...mdepth >\m@ne \if@mainmatter
\huge \bfseries \@chapapp ... l.108 \chapter{Definition of the Problem}

答案1

以下是如何獲得您想要的輸出KOMA腳本課程:

在此輸入影像描述

\documentclass[appendixprefix=true]{scrreprt}

\usepackage{etoolbox}
\makeatletter
\g@addto@macro{\appendix}{%
  \patchcmd{\@@makechapterhead}% <cmd>
    {\endgraf\nobreak\vskip.5\baselineskip}% <search>
    {\hspace*{-.5em}:\space}% <replace>
    {}{}% <success><failure>
  \patchcmd{\@chapter}% <cmd>
    {\addchaptertocentry{\thechapter}}% <search>
    {\addchaptertocentry{Appendix~\thechapter:}}% <replace>
    {}{}% <success><failure>
  \addtocontents{toc}{%
    \protect\patchcmd{\protect\l@chapter}% <cmd>
      {1.5em}% <search>
      {6.5em}% <replace>
      {}{}}% <success><failure>
}
\renewcommand{\autodot}{}% Remove all end-of-counter dots
\makeatother

\begin{document}

\tableofcontents

\chapter{First chapter}
\section{First section}
\subsection{First subsection}

\appendix
\chapter{Last chapter}

\end{document}

透過重新定義 來刪除點\autodot,而附錄格式是在呼叫時通過\patchcmd來完成的。最終補丁將框的寬度從調整為。此調整特定於目錄中與章節相關的條目,因為它涉及.\@@makechapterhead\appendix\numberline1.5em6.5em\l@chapter

etoolbox提供打補丁的能力。

答案2

從 KOMA-Script 版本 3.20 開始,您可以使用\DeclareTOCStyleEntry.所以這裡有一個新建議:

\documentclass[
  %appendixprefix=true,%<- removed
  11pt,a4paper,
  numbers=noenddot% <- added
]{scrreprt}[2016/05/10]% needs at least version 3.20

\DeclareTOCStyleEntry[
  level=\chaptertocdepth,
  indent=0pt,
  numwidth=2.3em,
  dynnumwidth,
  linefill=\hfill,
  entryformat=\appendixtocformat,
  entrynumberformat=\appendixtocnumberformat,
  pagenumberformat=\appendixtocpagenumberformat
]{tocline}{appendixchapter}
\newcommand*\appendixtocformat[1]{{\usekomafont{chapterentry}#1}}
\newcommand*\appendixtocnumberformat[1]{{\def\autodot{:}\appendixname\ #1}}
\newcommand*\appendixtocpagenumberformat[1]
  {{\usekomafont{chapterentry}\usekomafont{chapterentrypagenumber}#1}}

\usepackage{xpatch}
\xapptocmd\appendix
  {%
    \renewcommand*{\chapterformat}{%
      \mbox{\appendixname{\nobreakspace}\thechapter:%
        \IfUsePrefixLine{}{\enskip}}%
    }%
    \renewcommand*{\chaptermarkformat}{\appendixname\ \thechapter:\enskip}%
    \xpatchcmd{\addchaptertocentry}
      {\addtocentrydefault{chapter}{#1}{#2}}
      {\addtocentrydefault{appendixchapter}{#1}{#2}}%
      {}{\PatchFailed}%
  }{}{\PatchFailed}

\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\appendix
\chapter{Last chapter}
\end{document}

運行三次得到:

在此輸入影像描述

在此輸入影像描述

答案3

給你

\documentclass{book}
\begin{document}
\makeatletter
\def\@makechapterhead#1{\vspace*{50\p@}{\parindent\z@\raggedright\normalfont\ifnum\c@secnumdepth>\m@ne\if@mainmatter\huge\bfseries\@chapapp\space\thechapter: \fi\fi\interlinepenalty\@M\Huge\bfseries#1\par\nobreak\vskip40\p@}}
\makeatother
\chapter{CHAPI}
\section{SECI}
\subsection{SUBSECTI}
\appendix
\chapter{CHAPII}
\section{SECII}
\subsection{SUBSECTII}
\end{document}

答案4

以下內容與@werners 的答案幾乎相同,但略有不同。透過該選項,appendixprefix您可以獲得前綴附錄就在數字之前。這與將標題放在同一行相矛盾(這是它的實現方式)。主要思想是相同的,\@@makechapterhead當魔法發生時進行修補。起初,我只儲存了任何要列印為 autodot 的內容。當附錄開始時,會進行一些重新定義,例如
- 恢復自動點以打印冒號 ( :)
- 將換行符修補為空(無需擺弄空格,默認 KOMA 處於控制狀態)
- 如果您只想要章節條目按所需數量縮進,它會根據的內容動態縮排\appendixTocString。如果您稍後決定不列印該字詞附錄,將其從字串中刪除,它將正確縮排。

在此輸入影像描述

在此輸入影像描述

在此輸入影像描述

\documentclass[
appendixprefix=true,
]{scrreprt}
%The packages used in my document
%\usepackage{scrhack}%patches listings
%\usepackage{listings}
%\usepackage{mathtools}
%\usepackage{graphicx}
%\usepackage{enumitem}
%\usepackage{chngcntr}
%\usepackage[usenames,dvipsnames]{color}
\usepackage{etoolbox}
\renewcommand{\autodot}{}%
\newcommand{\appendixTocString}{\appendixname\space\thechapter\autodot}%
\newlength{\appendixTocStringLength}%
\settowidth{\appendixTocStringLength}{\appendixTocString}%
\addtolength{\appendixTocStringLength}{1.5em}%
\makeatletter%
\gappto{\appendix}{%Doing everything in the appendix%
\patchcmd{\@@makechapterhead}{\endgraf\nobreak\vskip.5\baselineskip}{}{}{}%
\renewcommand{\autodot}{:}%
\addtocontents{toc}{%
    \protect\patchcmd{\protect\l@chapter}%
    {1.5em}%
    {\protect\appendixTocStringLength}%
    {}{}}%
\patchcmd{\@chapter}{\addchaptertocentry{\thechapter}{\scr@ds@tocentry}%
}{%
\addchaptertocentry{\appendixTocString}{\scr@ds@tocentry}}{}{}%
}%
\makeatother%
\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\chapter{Second chapter}
\section{Second section}
\subsection{Second subsection}
\appendix
\chapter{In case the chapter title is really long, we need to
make sure that it gets indented properly.}
\section{appendix section}
\subsection{appendix subsection}
\chapter{appendix chapter}
\section{appendix section}
\subsection{appendix subsection}
\chapter{appendix chapter}
\section{appendix section}
\subsection{appendix subsection}
\chapter{appendix chapter}
\section{appendix section}
\subsection{appendix subsection}

\end{document}

相關內容