在 scrbook 中自定义章节样式

在 scrbook 中自定义章节样式

我想要获得像接下来两张图片中所示的样式(我不需要背景中的图片(抛物线)): 图片 1 图片 2

我原来的 LaTeX 代码(需要定制但应保留原有的字体):

% !TEX TS-program = latex
\documentclass[11pt, pagesize=auto, version=last, chapterprefix=true]{scrbook}
%% En 12pt c'est possible aussi
%% packages utilises
%%---------------------
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc} % pour taper les lettres accentues
%====Perso====%
\usepackage{charter}
\usepackage[expert]{mathdesign}
%=================%
%\usepackage{etoolbox}
%\pretocmd{\ref}{\bgroup\small}{}{}
%\apptocmd{\ref}{\egroup}{}{}
%=============%
\usepackage[frenchb]{babel}
\usepackage[babel=true]{csquotes} % csquotes va utiliser la langue définie dans babel
%%========Headings(perso)======================%
\usepackage{titlesec}
%%%%%Chapter%%%%%%%
\setkomafont{chapter}{\usefont{T1}{qhv}{b}{n}\selectfont\huge}
%%%%%Section%%%%%%%%
\titleformat{\section}[hang]{
    \usefont{T1}{qhv}{b}{n}\selectfont} % "qhv" - TeX Gyre Heros, "b" - bold
    {} 
    {0em}
   {\hspace{-0.4pt}\Large \thesection\hspace{0.6em}}
\begin{document}

\chapter{Test chapter}
\section{Test section}

\end{document}

答案1

在以下解决方案中,只需在使用或包时将绿色规则(例如\color{green} \rule{0.4\textwidth}{0.4\textwidth})替换为。然后您应该应用您喜欢的颜色主题。我只是使用了普通的“蓝色”进行演示。\includegraphics[width=0.4\textwidth]{yourpicname}graphicsgraphicx

代码

\documentclass[11pt, pagesize=auto, version=last, chapterprefix=true]{scrbook}
  \setkomafont{chapter}{\usefont{T1}{qhv}{b}{n}\selectfont\huge}
\usepackage{xcolor,anyfontsize,realboxes}
\renewcommand\chapterformat{%
  \hspace*{-1in} %customize this to your needs
  \makebox[0.42\textwidth][c]{%
    \raisebox{-.5em}[0.4\textwidth][0cm]{%
      \color{green} \rule{0.4\textwidth}{0.4\textwidth}
          \put(-52,24){%
            \Colorbox{blue}{%
            \parbox{0.11\textwidth}{%
            \centering
            \small\color{white}
            \chaptertitlename\par
            {\fontsize{38}{41}\selectfont\thechapter\par}
            \vspace{.5em}
            }}
         }   
    }
  }
}
\newcommand{\sectionformat}{%
  \raisebox{-.65em}[0cm][0cm]{%
    \Colorbox{blue}{%
    \parbox{3em}{%
      \centering
      \color{white}
      \thesection
    }}
  }
}
\usepackage[frenchb]{babel}
\usepackage{titlesec}
  \titleformat{\chapter}
    {\huge\bfseries\color{blue}}{\chapterformat}{0em}{}
  \titleformat{\section}[hang]{%
    \usefont{T1}{qhv}{b}{n}\selectfont\Large\color{blue}} % "qhv" - TeX Gyre Heros, "b" - bold
    {\sectionformat} 
    {0em}
    {\hspace{.75em}}
    [\vspace*{-.75em}\rule{2.25em}{0pt}\rule{\textwidth}{2pt}]

\begin{document}
\chapter{Test chapter}
\section{Test section}
\end{document}

输出

在此处输入图片描述

(请注意,我没有按照您的要求更改字体,尽管示例图片中的字体略有不同。有一些小写字母和斜体。)

答案2

请注意,您不应titlesec与 KOMA-Script 类一起使用。相反,您应该使用\RedeclareSectionCommand\setkomafont并重新定义\chapterlinesformat(或\chapterlineswithprefixformat)和\sectionlinesformat,例如:

\documentclass[
  11pt,% not needed, because it is the default
  pagesize=auto,% not needed, because it is the default
  version=last,% as long as you don't use deprecated options not needed
  ]{scrbook}
\usepackage[french]{babel}% frenchb is deprecated
\usepackage[babel=true]{csquotes}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage{charter}
\usepackage{tgheros}% You could also use \usepackage{helvet}.
\usepackage{graphicx}

\setkomafont{section}{\normalsize}

\newkomafont{chapterformat}{\fontsize{64}{64}\normalfont\bfseries\color{number}}
\newkomafont{chapterformattext}{\normalfont\small\itshape}
\newkomafont{sectionformat}{\Large\color{number}}
\colorlet{numberbackground}{blue}
\colorlet{disposition}{DeepSkyBlue}
\colorlet{number}{white}
\addtokomafont{disposition}{\color{disposition}}
\addtokomafont{chapter}{\scshape}
\newcommand*{\chapterimage}{example-image}
\renewcommand*{\chapterformat}{%
  {%
    \usekomafont{chapterformat}{%
      \raisebox{-\dimexpr\fboxsep+\fboxrule\relax}{%
        \includegraphics[width=.35\textwidth,height=.3\textwidth]{\chapterimage}%
      }%
      \makebox[0pt][r]{%
        \colorbox{numberbackground}{%
          \parbox[b]{1em}{\centering
            {\usekomafont{chapterformattext}{\chapapp\\[-1ex]}}%
            \thechapter% \autodot?
          }%
        }%
      }%
    }%
  }%
  \enskip
}
\makeatletter
\renewcommand*{\chapterlinesformat}[3]{%
  \ifstr{#1}{chapter}{%
    #2\parbox[b]{.65\textwidth}{\raggedchapter #3}%
  }{%
    \@hangfrom{#2}{#3}%
  }%
}
\makeatother
\renewcommand*{\sectionformat}{%
  \colorbox{numberbackground}{%
    \makebox[5em]{\usekomafont{sectionformat}{\thesection\autodot}}%
  }%
  \hskip 1em\nobreak
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
  \ifstr{#1}{section}{%
    \hskip#2#3\nobreak
    \parbox[b]{\dimexpr\linewidth-6em-2\fboxsep-2\fboxrule-#2\relax}{%
      {\raggedsection#4\par}%
      \vskip -\ht\strutbox
      \hskip -1em\rule{\dimexpr\linewidth+1em}{1pt}%
    }%
  }{%
    \@hangfrom{\hskip#2#3}{#4}%
  }%
}
\makeatother
\RedeclareSectionCommand[indent=.1\textwidth]{section}

\begin{document}

\tableofcontents

\chapter{Preliminaries}
\section{Test section}

\chapter{What happens if the chapter title is too long}
\section{What happens if the section title also needs more than one
line}

\end{document}

单行章节

这也适用于多行的章节或节标题:

两行章节

对于未编号的章节,它会打印没有背景图像的标题:

目录

如果在这种情况下您也想要背景图像,则必须将背景图像的输出从 移动\chapterformat\chapterlinesformat

如果要更改目录中章节条目的颜色,您可以更改元素的字体chapterentry,例如:

\addtokomafont{chapterentry}{\normalcolor}

答案3

这是另一个选择:

\documentclass[
  11pt,
  pagesize=auto,
  version=last,
  chapterprefix=true
  ]{scrbook}
\usepackage[frenchb]{babel}
\usepackage[babel=true]{csquotes}
\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}

在此处输入图片描述

自使用以来titlesec,KOMA 类将发出警告。

相关内容