从目录中删除 0

从目录中删除 0

我正在尝试使用 KOMA-Script 创建精美的章节标题,我注意到的一件事是目录始终编号为 0,因为我正在访问章节计数器。有没有办法只对第 1 章及以上章节执行此操作?即,如果是\thechapter > 0则访问它,否则不访问。

下面是一个 MWE,希望它能描述我的意思:

\documentclass{scrreprt}
\KOMAoption{appendixprefix}{false}
\KOMAoption{chapterprefix}{false}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{12pt}
\KOMAoption{headings}{big}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{toc}{indent,bibliography}
\KOMAoption{twoside}{semi}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{true}

% KOMA is complaining and apparently this fixes it =/
\usepackage{scrhack}
\usepackage{listings}

% Split document into multiple parts
\usepackage{subfiles}

% e-TeX tools
\usepackage{etoolbox}

% Colours - put this before typography so additional named colours can be defined.
\usepackage{xcolor}
\definecolor{rmitred}{HTML}{CF102F}

%% Typography Settings
\usepackage{microtype}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\usepackage{xunicode}
\usepackage{xltxtra}

\makeatletter
\setkomafont{chapter}{\let\@hangfrom\chapterhang\let\raggedsection\raggedright\fontsize{28}{28}\mdseries}
\makeatother
\newbox\chapternumberbox
\newbox\chapternamebox
\newcommand{\chapterhang}[2]{%
  \savebox\chapternumberbox{\fontsize{96}{96}\sffamily\color{rmitred}\arabic{chapter}}
  \raisebox{0em}{\usebox\chapternumberbox}
  \parbox[b][1\height][b]{\dimexpr\linewidth-\wd\chapternumberbox}{\raggedright #2}
}

\begin{document}

\tableofcontents

\chapter{Introduction}
\label{ch:introduction}

\section{A Section}

This is an introduction

\chapter{Improving Chapter Styling}

\section{Why}

Why do we want to do this thing? Because fancy.

\section{How}

How do we do this thing?

\chapter{The Second Chapter}

Some important things

\chapter{The Third Chapter}

Lorem ipsum etc

\chapter{Conclusion}

Lorem ipsum etc

\end{document}

答案1

除了缺少字体功能外,代码

\ifnum\value{chapter} > 0
  \savebox{...}% with \arabic{chapter}
\else
  \savebox{...}% Without \arabic{chapter}
\fi

对我有用。

关于计数器值的条件可以用以下方式完成\ifnum\value{countername} .... \if

\documentclass{scrreprt}
\KOMAoption{appendixprefix}{false}
\KOMAoption{chapterprefix}{false}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{12pt}
\KOMAoption{headings}{big}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{toc}{indent,bibliography}
\KOMAoption{twoside}{semi}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{true}

% KOMA is complaining and apparently this fixes it =/
\usepackage{scrhack}
\usepackage{listings}

% Split document into multiple parts
\usepackage{subfiles}

% e-TeX tools
\usepackage{etoolbox}

% Colours - put this before typography so additional named colours can be defined.
\usepackage{xcolor}
\definecolor{rmitred}{HTML}{CF102F}

%% Typography Settings
\usepackage{microtype}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\usepackage{xunicode}
\usepackage{xltxtra}

\makeatletter
\setkomafont{chapter}{\let\@hangfrom\chapterhang\let\raggedsection\raggedright\fontsize{28}{28}\mdseries}
\makeatother
\newbox\chapternumberbox
\newbox\chapternamebox

\newcommand{\chapterhang}[2]{%
%  \savebox\chapternumberbox{\fontsize{96}{96}\rmitfont\color{rmitred}\arabic{chapter}} 
  \ifnum\value{chapter}>0 
  \savebox\chapternumberbox{\fontsize{96}{96}\color{rmitred}\arabic{chapter}}%
  \raisebox{0em}{\usebox\chapternumberbox} 
  \else
  \savebox\chapternumberbox{\fontsize{96}{96}\color{rmitred}}
  \raisebox{0em}{\usebox\chapternumberbox}% 
  \fi
  \parbox[b][1\height][b]{\dimexpr\linewidth-\wd\chapternumberbox}{\raggedright #2}%
}



\begin{document}

\tableofcontents

\chapter{Introduction}
\label{ch:introduction}

\section{A Section}

This is an introduction

\chapter{Improving Chapter Styling}

\section{Why}

Why do we want to do this thing? Because fancy.

\section{How}

How do we do this thing?

\chapter{The Second Chapter}

Some important things

\chapter{The Third Chapter}

Lorem ipsum etc

\chapter{Conclusion}

Lorem ipsum etc

\end{document}

在此处输入图片描述

答案2

请注意\addtokomafont\setkomafont仅应用于切换字体属性。

这里有一个重新定义的建议\chapterformat\chapterlinesformat需要 KOMA-Script 版本 3.19a 或更新版本),它也适用于文档内的未编号章节。

\addtokomafont{chapter}{\fontsize{28}{28}\selectfont\mdseries}
\renewcommand\chapterformat{%
  {\fontsize{96}{96}\selectfont\textcolor{rmitred}{\thechapter}}\ }

\newbox\chapternumberbox
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#2}{}{
      \parbox[b]{\textwidth}{\raggedright#3}%
    }{%
      \savebox\chapternumberbox{#2}%
      \usebox\chapternumberbox%
      \parbox[b]{\dimexpr\textwidth-\wd\chapternumberbox\relax}{\raggedright#3}%
  }\par\nobreak
}

在此处输入图片描述

代码:

\documentclass{scrreprt}[2015/10/03]
\KOMAoption{appendixprefix}{false}
\KOMAoption{chapterprefix}{false}
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{12}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{12pt}
\KOMAoption{headings}{big}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{toc}{indent,bibliography}
\KOMAoption{twoside}{semi}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{true}

% KOMA is complaining and apparently this fixes it =/
\usepackage{scrhack}
\usepackage{listings}

% Split document into multiple parts
\usepackage{subfiles}

% e-TeX tools
\usepackage{etoolbox}

% Colours - put this before typography so additional named colours can be defined.
\usepackage{xcolor}
\definecolor{rmitred}{HTML}{CF102F}

%% Typography Settings
\usepackage{microtype}
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\usepackage{xunicode}
\usepackage{xltxtra}

\addtokomafont{chapter}{\fontsize{28}{28}\selectfont\mdseries}
\renewcommand\chapterformat{%
  {\fontsize{96}{96}\selectfont\textcolor{rmitred}{\thechapter}}\ }

\newbox\chapternumberbox
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#2}{}{
      \parbox[b]{\textwidth}{\raggedright#3}%
    }{%
      \savebox\chapternumberbox{#2}%
      \usebox\chapternumberbox%
      \parbox[b]{\dimexpr\textwidth-\wd\chapternumberbox\relax}{\raggedright#3}%
  }\par\nobreak
}

\begin{document}
\tableofcontents
\chapter{Introduction}
\label{ch:introduction}
\section{A Section}
This is an introduction
\chapter{Improving Chapter Styling}
\section{Why}
Why do we want to do this thing? Because fancy.
\section{How}
How do we do this thing?
\chapter{The Second Chapter}
Some important things
\chapter{The Third Chapter}
Lorem ipsum etc
\chapter{Conclusion}
Lorem ipsum etc
\addchap{An unnumbered chapter}
\end{document}

相关内容