删除标题编号中的最后一个点

删除标题编号中的最后一个点

我当前的文档处理如下:

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魔法。起初,我只保存了没有要打印为自动点的内容。当附录开始时,会进行一些重新定义,例如
- 恢复自动点以打印冒号(:
- 将换行符修补为完全没有内容(无需摆弄空格,默认的 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}

相关内容