自定义章节和部分样式(scrbook)

自定义章节和部分样式(scrbook)

我发现了一个非常酷的章节和节标题自定义样式(它来自在 scrbook 中自定义章节样式):

\documentclass[
  11pt,
  pagesize=auto,
  version=last,
  chapterprefix=true
  ]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{charter}

\definecolor{mybluei}{RGB}{28,138,207}
\definecolor{myblueii}{RGB}{131,197,231}

\newcommand\ChapterFont{\usefont{T1}{qhv}{b}{n}\selectfont\huge}
\newcommand\SectionFont{\usefont{T1}{qhv}{b}{n}\selectfont}

\titleformat{\chapter}[display]
  {\normalfont\ChapterFont\huge\color{myblueii}}
  {}
  {0pt}
  {\parbox[b]{70pt}{\mbox{}}%
    \parbox[b]{50pt}{\colorbox{mybluei}{%
      \parbox[b][60pt][t]{45pt}{\centering%
        \color{white}%
        {\itshape\rmfamily\small\chaptertitlename}%
        \vfill{\fontsize{50}{120}\selectfont\thechapter}%
        }%
      }%
    }\hspace{15pt}%
    \parbox[b]{\dimexpr\textwidth-150pt}{%
      \raggedright\scshape#1\vskip6pt%
    }%
  }

\titleformat{name=\chapter,numberless}[display]
  {\normalfont\ChapterFont\huge\color{myblueii}}
  {}
  {0pt}
  {\parbox[b]{70pt}{\mbox{}}%
    \hspace{15pt}%
    \parbox[b]{\dimexpr\textwidth-150pt}{%
      \raggedright\scshape#1\vskip6pt%
    }%
  }

\titleformat{\section}
  {\normalfont\small\sffamily\SectionFont\color{myblueii}}
  {\colorbox{mybluei}{%
    \parbox[c][16pt][c]{40pt}{%
      \centering\textcolor{white}{\SectionFont\Large\rmfamily\thesection}%
      }%
    }%
  }
  {1em}
  {#1}
  [\vspace{-1.2\baselineskip}%
    \color{myblueii}\hspace*{\dimexpr40pt+2\fboxsep\relax}%
    \rule{\dimexpr\textwidth-40pt-2\fboxsep\relax}{1pt}%
  ]

\begin{document}

\chapter{Preliminaries}
\section{Test section}

\end{document}

我对此有两个问题。

首先,我想让章节标题稍微大一点,并且节标题的名称也应该更大。该怎么做?

其次,我希望章节标题不缩进,即它应该居中。

编辑:我现在已经尝试了 esdd 的方法来不破坏 KOMA 级,但现在遇到了几个问题:

  1. 在目录列表中,章节标题为蓝色,但节标题为黑色。是否可以将章节标题也设为黑色?

  2. 是否可以像 Benjaming 的例子那样将线画得稍微低一些?对我来说,这条较低的线看起来更好。请参见下图。

  3. 本杰明还制作了不带编号的子小节标题(因为否则样式太多)。另请参见下图。这也可以吗?

在此处输入图片描述

编辑 2:现在除了标题下方的那行之外,一切都几乎完美了。在您的示例中,它看起来很棒,但如果我在文档中使用它,那行就会显示得更低。我尝试了不同的方法,但找不到解决方案。您知道问题出在哪里了吗?我也不知道我的模板是否完美,或者是否存在其他错误。请参见下面的示例。

主文本

\documentclass[a4paper,twoside,12pt,chapterprefix=false]{scrbook}

\usepackage{amsmath,amssymb,amsthm}
\usepackage[footnotesize,sl,SL,hang,tight]{subfigure}
\usepackage{longtable}
\usepackage[font={small,sl},hang,labelfont=bf]{caption}
\usepackage{booktabs}

\ifpdfoutput{%
    \usepackage[pdftex]{graphicx}
    \usepackage[]{pdfpages}
}{%
    \usepackage{graphicx}
}
\usepackage{rotating}

\usepackage{scrlayer-scrpage}
\KOMAoptions{headinclude}

% **** Defining chapter style start ****

\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{charter}

\definecolor{mybluei}{RGB}{28,138,207}
\definecolor{myblueii}{RGB}{131,197,231}

\addtokomafont{disposition}{\usefont{T1}{qhv}{b}{n}\selectfont\color{myblueii}}

\addtokomafont{chapter}{\fontsize{30pt}{30pt}\selectfont}
\newkomafont{chapternumber}{\fontsize{50}{120}\selectfont\color{white}}
\newkomafont{chaptername}{\itshape\rmfamily\small\color{white}}
\addtokomafont{chapterentry}{\normalcolor}% entrys in tableofcontents not blue

\addtokomafont{section}{\fontsize{14pt}{14pt}\selectfont}
\newkomafont{sectionnumber}{\fontsize{18pt}{18pt}\selectfont\rmfamily\color{white}}

\addtokomafont{subsection}{\fontsize{12pt}{12pt}\selectfont}
\newkomafont{subsectionnumber}{\fontsize{16pt}{16pt}\selectfont\rmfamily\color{white}}

\setcounter{secnumdepth}{\subsectionnumdepth}% subsubsection and lower unnumbered

\renewcommand\chapterformat{%
  \raisebox{-6pt}{\colorbox{mybluei}{%
      \parbox[b][60pt]{45pt}{\centering%
        {\usekomafont{chaptername}{\chaptername}}%
        \vfill{\usekomafont{chapternumber}{\thechapter\autodot}}%
        \vspace{6pt}%
      }}}\enskip}

\renewcommand\sectionformat{%
  \setlength\fboxsep{5pt}%
  \colorbox{mybluei}{%
      \parbox{40pt}{\centering\usekomafont{sectionnumber}{\thesection\autodot}}}%
    \quad%
  }

\renewcommand\subsectionformat{%
  \setlength\fboxsep{5pt}%
  \colorbox{mybluei}{%
    \parbox{60pt}{\centering\usekomafont{subsectionnumber}{\thesubsection\autodot}}}%
\quad%
}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \@hangfrom{\hspace*{#2}#3}{#4}%
  \ifstr{#1}{section}{%
    \par\nobreak%
    \ifstr{#3}{}{%
      \rule[\dp\strutbox]{\textwidth}{1pt}}{%
      \hspace*{50pt}\rule[\dp\strutbox]{\dimexpr\textwidth-50pt}{1pt}}%
  }{}%
  \ifstr{#1}{subsection}{%
    \par\nobreak%
    \ifstr{#3}{}{%
      \rule[\dp\strutbox]{\textwidth}{1pt}}{%
      \hspace*{70pt}\rule[\dp\strutbox]{\dimexpr\textwidth-70pt}{1pt}}%
  }{}%
}
\makeatother

% **** Defining chapter style end ****

% Define an own layer page style for the title page
\DeclareNewLayer[
  background,
  head,
  addvoffset=\headsep,
  height=1.5cm,
  addheight=\dp\strutbox,
  contents={%
    \includegraphics[height=1.5cm]{figures/ETH_logo.pdf}%
    \hfill%
    \includegraphics[height=1.4cm]{figures/CGL_logo.pdf}%
  }
  ]{titlepage.head}
\DeclareNewPageStyleByLayers{titlepage}{titlepage.head}

\usepackage{times}
\usepackage{helvet}
\usepackage[T1]{fontenc}

\ifpdfoutput{%
    \usepackage[pdftex,
        bookmarks,
        bookmarksopen=true,
        bookmarksnumbered=true,
        pdfauthor={Rafael Wampfler},
        pdftitle={Engagement Detector: Modelling engagement dynamics in virtual environments},
        pdfsubject={The report of the master thesis about the modelling engagement dynamics in virtual environments.},
        pdfkeywords={machine learning, engagement dynamics, classification, virtual environments, calcularis, learning, minecraft},
        colorlinks=false,
        linkcolor=black,
        citecolor=black,
        filecolor=black,
        urlcolor=black,
        anchorcolor=black,
        menucolor=black,
        breaklinks=true,
        pageanchor=true,
        plainpages=false,
        linkbordercolor=red,
        citebordercolor=red,
        urlbordercolor=red,
        pdfpagelabels=true]{hyperref}
}{}

\ifpdfoutput{%
    \pdfcompresslevel=9
    \pdfoutput=1
    \DeclareGraphicsExtensions{.pdf,.png}
}{}

\bibliographystyle{alpha}

\topmargin -0.5in
\textheight 9.3in
\textwidth 6.3in
\oddsidemargin 0.18in
\evensidemargin -0.22in
\parskip 0.1in
\parindent 0in

\renewcommand{\arraystretch}{1.5}
\renewcommand{\baselinestretch}{1}

\begin{document}

%% Define leading chapter pages
%
\input{studchapter}
\newpagestyle{mychapterpagestyle}{{\protect\mychpstyleintl}{\protect\mychpstyleintl}}{}
\newpagestyle{myappendixpagestyle}{{\protect\mychpstyleintl}{\protect\mychpstyleintl}}{}
%%

\hypersetup{pageanchor=false}

\hypersetup{pageanchor=true}
\pagenumbering{roman}
\setcounter{page}{1}

%\include{abstract}

%\include{acknowledgment}

\tableofcontents
\cleardoublepage

\addcontentsline{toc}{chapter}{List of Figures}
\listoffigures
\cleardoublepage

\addcontentsline{toc}{chapter}{List of Tables}
\listoftables
\cleardoublepage

\pagenumbering{arabic}
\renewcommand*{\chapterpagestyle}{mychapterpagestyle}
\renewcommand*{\chapterformat}{} % show chapter titles only (no numbers)

% ---- MAIN PART ----

  \chapter{Chapter Title}
  \section{Section Title}
  \blindtext
  \addsec{Unnumbered Section Title}
  \blindtext
  \subsection{Subsection Title}
  \blindtext
  \subsubsection{Subsubsection Title}
  \blindtext
  \blinddocument

% ---- END MAIN PART ----

\appendix
\clearpage
\renewcommand*{\chapterpagestyle}{myappendixpagestyle}

%\include{appendix}

\clearpage
\renewcommand*{\chapterpagestyle}{empty}

\addcontentsline{toc}{chapter}{Bibliography}
\bibliography{references}

\end{document}

章节目录

\renewcommand*{\chapterheadstartvskip}{\vspace*{215pt}}

\def\mychpstyleintl{%
{\noindent\setlength{\tabcolsep}{0pt}\setlength{\arrayrulewidth}{2pt}%
\begin{tabular}{c}
\\[100pt]
\begin{tabular}{lr}
\begin{tabular}{p{0.6\linewidth}}
\\
\end{tabular}
&
\begin{tabular}{p{0.4\linewidth}}
\rightline{{%
\sffamily%
\fontseries{bx}%
\fontshape{n}%
\fontsize{100}{120}%choose baselineskip to be 1.2 times font size
\selectfont
\thechapter}}
\end{tabular}
\end{tabular}\\[300pt]
\end{tabular}
}}

在此处输入图片描述

答案1

这是一个没有包的示例titlesec

\documentclass[numbers=noenddot]{scrbook}[2015/10/03]
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{charter}

\definecolor{mybluei}{RGB}{28,138,207}
\definecolor{myblueii}{RGB}{131,197,231}

\addtokomafont{disposition}{\usefont{T1}{qhv}{b}{n}\selectfont\color{myblueii}}

\addtokomafont{chapter}{\fontsize{30pt}{30pt}\selectfont}
\newkomafont{chapternumber}{\fontsize{50}{120}\selectfont\color{white}}
\newkomafont{chaptername}{\itshape\rmfamily\small\color{white}}

\addtokomafont{section}{\fontsize{14pt}{14pt}\selectfont}
\newkomafont{sectionnumber}{\fontsize{18pt}{18pt}\selectfont\rmfamily\color{white}}

\addtokomafont{subsection}{\fontsize{12pt}{12pt}\selectfont}
\newkomafont{subsectionnumber}{\fontsize{16pt}{16pt}\selectfont\rmfamily\color{white}}

\renewcommand\chapterformat{%
  \raisebox{-6pt}{\colorbox{mybluei}{%
    \parbox[b][60pt]{45pt}{\centering%
      {\usekomafont{chaptername}{\chapapp}}%
      \vfill{\usekomafont{chapternumber}{\thechapter\autodot}}%
      \vspace{6pt}%
}}}\enskip}

\renewcommand\sectionformat{%
  \setlength\fboxsep{5pt}%
  \raisebox{-4pt}{\colorbox{mybluei}{%
    \enskip\usekomafont{sectionnumber}{\thesection\autodot}\enskip}%
  \quad%
}}

\renewcommand\subsectionformat{%
  \setlength\fboxsep{5pt}%
  \raisebox{-4pt}{\colorbox{mybluei}{%
    \enskip\usekomafont{subsectionnumber}{\thesubsection\autodot}\enskip}%
  \quad%
}}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \makebox[0pt][l]{\rule[-5pt]{\textwidth}{1pt}}%
  \@hangfrom{#3}{#4}%
}
\makeatother

\usepackage{blindtext}% only for dummy text
\begin{document}
\tableofcontents
\chapter{Chapter Title}
\section{Section Title}
\blindtext
\addsec{Unnumbered Section Title}
\blindtext
\subsection{Subsection Title}
\blindtext
\subsubsection{Subsubsection Title}
\blindtext
\blinddocument
\end{document}

在此处输入图片描述


更新由于问题的更新:

要获取目录中的黑色章节条目,请使用

\addtokomafont{chapterentry}{\normalcolor}% entrys in tableofcontents not blue

如果所有小节都不应编号,请使用

\setcounter{secnumdepth}{\subsectionnumdepth}% subsubsection and lower unnumbered

下面是规则较低的示例:

\documentclass[numbers=noenddot]{scrbook}[2015/10/03]
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{charter}

\definecolor{mybluei}{RGB}{28,138,207}
\definecolor{myblueii}{RGB}{131,197,231}

\addtokomafont{disposition}{\usefont{T1}{qhv}{b}{n}\selectfont\color{myblueii}}

\addtokomafont{chapter}{\fontsize{30pt}{30pt}\selectfont}
\newkomafont{chapternumber}{\fontsize{50}{120}\selectfont\color{white}}
\newkomafont{chaptername}{\itshape\rmfamily\small\color{white}}
\addtokomafont{chapterentry}{\normalcolor}% entrys in tableofcontents not blue

\addtokomafont{section}{\fontsize{14pt}{14pt}\selectfont}
\newkomafont{sectionnumber}{\fontsize{18pt}{18pt}\selectfont\rmfamily\color{white}}

\addtokomafont{subsection}{\fontsize{12pt}{12pt}\selectfont}
\newkomafont{subsectionnumber}{\fontsize{16pt}{16pt}\selectfont\rmfamily\color{white}}

\setcounter{secnumdepth}{\subsectionnumdepth}% subsubsection and lower unnumbered

\renewcommand\chapterformat{%
  \raisebox{-6pt}{\colorbox{mybluei}{%
      \parbox[b][60pt]{45pt}{\centering%
        {\usekomafont{chaptername}{\chapapp}}%
        \vfill{\usekomafont{chapternumber}{\thechapter\autodot}}%
        \vspace{6pt}%
      }}}\enskip}

\renewcommand\sectionformat{%
  \setlength\fboxsep{5pt}%
  \colorbox{mybluei}{%
      \parbox{40pt}{\centering\usekomafont{sectionnumber}{\thesection\autodot}}}%
    \quad%
  }
  
\renewcommand\subsectionformat{%
  \setlength\fboxsep{5pt}%
  \colorbox{mybluei}{%
    \parbox{60pt}{\centering\usekomafont{subsectionnumber}{\thesubsection\autodot}}}%
\quad%
}

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \@hangfrom{\hspace*{#2}#3}{#4}%
  \ifstr{#1}{section}{%
    \par\nobreak%
    \ifstr{#3}{}{%
      \rule[\dp\strutbox]{\textwidth}{1pt}}{%
      \hspace*{50pt}\rule[\dp\strutbox]{\dimexpr\textwidth-50pt}{1pt}}%
  }{}%
  \ifstr{#1}{subsection}{%
    \par\nobreak%
    \ifstr{#3}{}{%
      \rule[\dp\strutbox]{\textwidth}{1pt}}{%
      \hspace*{70pt}\rule[\dp\strutbox]{\dimexpr\textwidth-70pt}{1pt}}%
  }{}%
}
\makeatother
    
\usepackage{blindtext}% only for dummy text
\begin{document}
  \tableofcontents
  \chapter{Chapter Title}
  \section{Section Title}
  \blindtext
  \addsec{Unnumbered Section Title}
  \blindtext
  \subsection{Subsection Title}
  \blindtext
  \subsubsection{Subsubsection Title}
  \blindtext
  \blinddocument
\end{document}

在此处输入图片描述

在此处输入图片描述


更新 2因为问题有第二次更新

您正在使用\parskip因此\par必须删除的垂直插入空间:

\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \@hangfrom{\hspace*{#2}#3}{#4}%
  \ifstr{#1}{section}{%
    \parskip\z@% <- added, deactivates the parskip locally
    \par\nobreak
    \ifstr{#3}{}{%
      \rule[\dp\strutbox]{\textwidth}{1pt}}{%
      \hspace*{50pt}\rule[\dp\strutbox]{\dimexpr\textwidth-50pt}{1pt}}%
  }{}%
  \ifstr{#1}{subsection}{%
    \parskip\z@% <- added, deactivates the parskip locally
    \par\nobreak
    \ifstr{#3}{}{%
      \rule[\dp\strutbox]{\textwidth}{1pt}}{%
      \hspace*{70pt}\rule[\dp\strutbox]{\dimexpr\textwidth-70pt}{1pt}}%
  }{}%
}
\makeatother

一些补充说明:

不要\parskip手动设置!请改用 KOMA-Script 选项parskip=half-。此选项还有其他值,如halfhalf+和。有关更多说明,请half*参阅fullscrguien.pdf(KOMA-Script 文档)。

我建议使用包geometry设置页边距。

要获取目录中列表和参考书目的条目,请使用 KOMA-Script 选项listof=totocbibilography=totoc而不是\addcontentsline{toc}{chapter}{...}

您重新定义了\chapterformat两次。决定要使用哪一个并删除另一个。

新定义的标题mychapterpagestyle在论文外面。

答案2

这符合你的条件吗?

\documentclass[
  11pt,
  pagesize=auto,
  version=last,
  chapterprefix=true
  ]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{charter}

\definecolor{mybluei}{RGB}{28,138,207}
\definecolor{myblueii}{RGB}{131,197,231}

\newcommand\ChapterFont{\usefont{T1}{qhv}{b}{n}\selectfont}
\newcommand\SectionFont{\usefont{T1}{qhv}{b}{n}\selectfont}

\newlength{\chapterfontsize}
\setlength{\chapterfontsize}{30pt}
\newlength{\sectionfontsize}
\setlength{\sectionfontsize}{14pt}
\newlength{\subsectionfontsize}
\setlength{\subsectionfontsize}{12pt}


\titleformat{\chapter}[display]
  {\normalfont\ChapterFont\fontsize{\chapterfontsize}{\chapterfontsize}\color{myblueii}}%
  {}
  {0pt}
  {% <-- REMOVED INDENTATION
    \parbox[b]{50pt}{\colorbox{mybluei}{%
      \parbox[b][60pt][t]{45pt}{\centering%
        \color{white}%
        {\itshape\rmfamily\small\chaptertitlename}%
        \vfill{\fontsize{50}{120}\selectfont\thechapter}%
        \vspace{6pt}%
        }%
      }%
    }\hspace{15pt}%
    \parbox[b]{\dimexpr\textwidth-150pt}{%
      \raggedright\scshape#1\vskip6pt%
    }%
  }

\titleformat{name=\chapter,numberless}[display]
  {\normalfont\ChapterFont\fontsize{\chapterfontsize}{\chapterfontsize}\color{myblueii}}
  {}
  {0pt}
  {{\mbox{}}%
    \parbox[b]{\dimexpr\textwidth-150pt}{%
      \raggedright\scshape#1\vskip6pt%
    }%
  }

\titleformat{\section}
  {\normalfont\fontsize{\sectionfontsize}{\sectionfontsize}\sffamily\SectionFont\color{myblueii}} %
  {\colorbox{mybluei}{%
    \parbox[c][1.2\sectionfontsize][c]{2\sectionfontsize}{%
      \centering\textcolor{white}{\SectionFont\fontsize{1.2\sectionfontsize}{1.2\sectionfontsize}\rmfamily\thesection}%
      }%
    }%
  }
  {1em}
  {#1}
  [\vspace{-1\baselineskip}%
    \color{myblueii}\hspace*{\dimexpr2\sectionfontsize+2\fboxsep\relax}%
    \rule{\dimexpr\textwidth-2\sectionfontsize-2\fboxsep\relax}{1pt}%
  ]

\titleformat{name=\section,numberless}[display]
  {\normalfont\fontsize{\sectionfontsize}{\sectionfontsize}\sffamily\SectionFont\color{myblueii}}
  {}
  {0pt}
  {{\mbox{}}%
    \parbox[b]{\dimexpr\textwidth-150pt}{%
      \raggedright#1\vskip6pt%
    }%
  }
  [\vspace{-1\baselineskip}\color{myblueii}\rule{\textwidth}{1pt}]

\titleformat{\subsection}
  {\normalfont\fontsize{\subsectionfontsize}{\subsectionfontsize}\sffamily\SectionFont\color{myblueii}} %
  {\colorbox{mybluei}{%
    \parbox[c][1.2\subsectionfontsize][c]{3\subsectionfontsize}{%
      \centering\textcolor{white}{\SectionFont\fontsize{1.2\subsectionfontsize}{1.2\subsectionfontsize}\rmfamily\thesubsection}%
      }%
    }%
  }
  {1em}
  {#1}
  [\vspace{-1\baselineskip}%
    \color{myblueii}\hspace*{\dimexpr3\subsectionfontsize+2\fboxsep\relax}%
    \rule{\dimexpr\textwidth-3\subsectionfontsize-2\fboxsep\relax}{1pt}%
  ]

\titleformat{\subsubsection}
    {\normalfont\fontsize{\subsectionfontsize}{1.2\subsectionfontsize}\sffamily\SectionFont\color{myblueii}}
    {}
    {1em}
    {#1}


\begin{document}

\chapter*{Abstract}
\chapter{Chapter Title}
\section{Section Title}
\subsection{Subsection Title}
\subsubsection{Subsubsection Title}

\end{document}

相关内容