将 \part 添加到目录,格式不匹配

将 \part 添加到目录,格式不匹配

在以下 MWE 中,我已将目录和文本中的格式定义\part为居中且大写。如果我的\part文档中只有一个标题,我需要这个标题没有数字,所以我选择了,\part*但我仍然需要它在目录中,所以我选择了,\addcontentsline这会导致目录条目左对齐...我该如何解决这个问题?提前致谢

\documentclass[a4paper,pagsize]{scrartcl}
\usepackage{tocloft}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}

\setcounter{tocdepth}{0}
\cftpagenumbersoff{part}

\renewcommand*{\addparttocentry}[2]{%
  \addtocentrydefault{part}{}{\protect\parbox{\textwidth}{\protect \centering\MakeUppercase{#1. #2}}}
}

\renewcommand*{\partformat}{\Large
\begin{center}
\thepart\autodot 
\end{center}}
\addtokomafont{part}{\centering\large\bfseries\MakeUppercase}

\begin{document}
\tableofcontents
\hrule
\part*{Memo}
\addcontentsline{toc}{part}{Memo}

\section{}
text text
\part{Outlook}
\label{sec:Outlook}
text text
\end{document}

答案1

您应该使用\addpart而不是\part*,这样会自动创建目录条目。请注意,我添加了一个测试,以查看是否有数字,否则您将得到一个点。

添加部分

\documentclass[a4paper,pagsize]{scrartcl}
\usepackage{tocloft}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{etoolbox}

\setcounter{tocdepth}{0}
\cftpagenumbersoff{part}

\renewcommand*{\addparttocentry}[2]{%
  \addtocentrydefault{part}{}{\protect\parbox{\textwidth}{\protect\centering\MakeUppercase{\ifstrempty{#1}{}{#1. }#2}}}
}

\renewcommand*{\partformat}{\Large
\begin{center}
\thepart\autodot 
\end{center}}
\addtokomafont{part}{\centering\large\bfseries\MakeUppercase}

\begin{document}
\tableofcontents
\hrule
\addpart{Memo}

\section{}
text text
\part{Outlook}
\label{sec:Outlook}
text text
\end{document}

答案2

不要将该包tocloft与 KOMA-Script 类一起使用:

\documentclass[a4paper,pagesize]{scrartcl}[2016/05/10]% needs version 3.20 or newer
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}

\setcounter{tocdepth}{0}

\renewcommand*{\addparttocentry}[2]{%
  \ifstr{#1}{}
    {\addtocentrydefault{part}{}{\MakeUppercase{#2}}}
    {\addtocentrydefault{part}{}{#1\autodot\enskip\MakeUppercase{#2}}}%
}

\renewcommand*\raggedpart{\centering}
\renewcommand*\partformat{\thepart\autodot\par\bigskip}
\RedeclareSectionCommand[
  font=\large\MakeUppercase,
  prefixfont=\Large,
  tocnumwidth=0pt,
  toclinefill={},
  tocpagenumberformat=\nopagenumber,
  tocentryformat=\parttocentryformat,
]{part}
\newcommand\nopagenumber[1]{}
\makeatletter
\newcommand\parttocentryformat[1]{%
  \renewcommand\@pnumwidth{0pt}
  \usekomafont{partentry}\centering{#1}%
}
\makeatother

\begin{document}
\tableofcontents
\hrule
\addpart{Memo}

\section{A section}
text text
\part{Outlook}
\label{sec:Outlook}
\KOMAScriptVersion
\end{document}

结果:

在此处输入图片描述


下一个(尚未在 CTAN 上)KOMA-Script 版本 3.25将定义并使用命令\partlineswithprefixcommand来格式化部分标题的布局。然后\MakeUppercase应该从字体设置中删除,因为它不是字体命令。

\documentclass[a4paper,pagesize]{scrartcl}[2017/09/07]% needs KOMA-Script version prerelease 3.25 or newer 
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern}

\setcounter{tocdepth}{0}

\renewcommand*{\addparttocentry}[2]{%
  \ifstr{#1}{}
    {\addtocentrydefault{part}{}{\MakeUppercase{#2}}}
    {\addtocentrydefault{part}{}{#1\autodot\enskip\MakeUppercase{#2}}}%
}

\renewcommand*\raggedpart{\centering}
\renewcommand*\partformat{\thepart\autodot}
\renewcommand*\partlineswithprefixformat[3]{%
  #2\par\bigskip\MakeUppercase{#3}%
}
\RedeclareSectionCommand[
  font=\large,
  prefixfont=\Large,
  tocnumwidth=0pt,
  toclinefill={},
  tocpagenumberformat=\nopagenumber,
  tocentryformat=\parttocentry,
]{part}
\newcommand\nopagenumber[1]{}
\makeatletter
\newcommand\parttocentry[1]{%
  \renewcommand\@pnumwidth{0pt}
  \usekomafont{partentry}\centering{#1}%
}
\makeatother

\begin{document}
\tableofcontents
\hrule
\addpart{Memo}

\section{A section}
text text
\part{Outlook}
\label{sec:Outlook}
\KOMAScriptVersion{}
\end{document}

相关内容