每次一个章节采用一种风格

每次一个章节采用一种风格

我看到一些问题可能没有回答我的问题,这个问题更多的是关于装饰方面的。

在阅读一些(彩色插图)教科书时,可能会发现各章节的布局风格有所不同。例如主题颜色和图标。

如果我想在我的文档中添加此功能,我应该怎么做?

如果有人需要 MWE,请在评论中提出请求并告诉我需要哪种 MWE。我不擅长提供 MWE。

编辑:我能否提供一些现有的例子,例如牛津大学出版社出版的 IB 教材,他们提供了一个彩色的示例章节。我希望每个章节都有不同的标题有不同的主题颜色,也有不同的页码并配上相应的颜色(位于页面底部)。但我不需要与示例相同。

答案1

这是一种使用的可能性titlesectikzfancyhdr。代码需要运行三次才能稳定:

\documentclass{book}
\usepackage[margin=2.5cm,centering]{geometry}
\usepackage{lmodern}
\usepackage{tikz}
\usepackage{emptypage}
\usepackage{fancyhdr}
\usepackage{multicol}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\definecolor{color2}{RGB}{255,197,51}
\definecolor{color3}{RGB}{121,163,65}
\definecolor{color1}{RGB}{53,78,85}
\definecolor{color4}{RGB}{0,27,92}

% page style - headers and footers
\pagestyle{fancy}

\fancyheadoffset[L,R]{2.5cm}
\renewcommand\headrulewidth{0pt}
\fancyhf{}
\fancyhead[EL]{%
  \begin{tikzpicture}[remember picture,overlay]
  \node[
    inner sep=0pt,
    fill=\chapterbackcolor,
    font=\fontsize{40}{48}\selectfont\color{white}\sffamily,
    text width=1.5cm,
    anchor=south west,
    text height=1.5cm,
    text depth=0.2cm,
    align=center
    ]
    (chapnum) 
    {\thechapter};
   \node[
     fill=\chapterbackcolor,
     anchor=south west,
     font=\color{white}\sffamily
   ] 
   (mark)
   at ([xshift=1em]chapnum.south east) 
   {\leftmark};
   \draw[\chapterbackcolor,line width=1pt] 
     ([xshift=1em]mark.south east) -- ++(\paperwidth,0);
  \end{tikzpicture}%
}
\fancyhead[OL]{%
  \begin{tikzpicture}[remember picture,overlay]
  \node[
    inner sep=0pt,
    fill=\chapterbackcolor,
    font=\fontsize{40}{48}\selectfont\color{white}\sffamily,
    anchor=north east,
    align=center
    ]
    (chapimage)
    at (current page.north east) 
    {\includegraphics[height=1.5cm,width=4cm]{example-image-a}};
   \node[
     inner sep=0pt,
     anchor=south east,
     font=\color{\chapterbackcolor}\sffamily
   ] 
   (mark)
   at ([xshift=-1em]chapimage.south west) 
   {\rightmark};
   \draw[\chapterbackcolor,line width=1pt] 
     ([yshift=-1.5ex]mark.south east) -- ++(-\paperwidth,0);
  \end{tikzpicture}%
}
\fancyfoot[EL]{%
  \begin{tikzpicture}[remember picture,overlay]
  \node[
    text width=2cm,
    text height=0.5cm,
    text depth=1.5cm,
    fill=\chapterbackcolor!20,
    font=\Large\sffamily,
    anchor=north east,
    align=center
    ]
    {\thepage};
  \end{tikzpicture}  
}
\fancyfoot[OR]{%
  \begin{tikzpicture}[remember picture,overlay]
  \node[
    text width=2cm,
    text height=0.5cm,
    text depth=1.5cm,
    fill=\chapterbackcolor!20,
    font=\Large\sffamily,
    anchor=north west,
    align=center
    ]
    {\thepage};
  \end{tikzpicture}  
}

\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ \MakeUppercase{#1}}}

% chapter style
\newcommand\chapterbackcolor{}

\renewcommand\chapterbackcolor{%
  \ifcase\value{chapter}color4\or color1\or color2\else color3\fi}

\makeatletter
\newcommand\introduction[1]{\def\@introduction{#1}}
\titleformat{\chapter}[block]
  {\normalfont\Large\bfseries\sffamily}
  {}
  {0em}
  {%
    \begin{tikzpicture}[remember picture,overlay]
    \node[fill=\chapterbackcolor,text width=\paperwidth,anchor=north,minimum height=7cm]
      (titleback)
      at (current page.north)
      {};
    \node[font=\huge\color{white},anchor=north west]
      (title) 
      at ([xshift=1cm,yshift=-1cm]titleback.north west) 
      {{\fontsize{40}{48}\selectfont\thechapter}\quad\MakeUppercase{#1}};
    \node[fill=white,anchor=north west,rounded corners,text width=\the\dimexpr\paperwidth-2cm\relax]
      (intro) 
      at ([yshift=-0.5cm]title.south west) 
      {Introduction\\\vspace*{-.35\baselineskip}%
        \begin{minipage}[c][2.2cm][c]{\linewidth}
        \begin{multicols}{2}
          \normalfont\normalsize\@introduction
        \end{multicols}
        \end{minipage}%
      };
    \end{tikzpicture}%
  }
\titleformat{name=\chapter,numberless}[block]
  {\normalfont\Large\bfseries\sffamily}
  {}
  {0em}
  {%
    \begin{tikzpicture}[remember picture,overlay]
    \node[fill=\chapterbackcolor,text width=\paperwidth,anchor=north,minimum height=7cm]
      (titleback)
      at (current page.north)
      {};
    \node[font=\huge\color{white},anchor=north west]
      (title) 
      at ([xshift=1cm,yshift=-1cm]titleback.north west) 
      {\MakeUppercase{#1}};
    \end{tikzpicture}%
  }
\makeatother
\titlespacing*{\chapter}
  {0pt}{0pt}{5cm}

\newcommand\Text{% dummy text just for the example
Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel justo vitae lacus
tincidunt ultrices. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In
hac habitasse platea \columnbreak dictumst. Integer tempus convallis augue. Etiam facilisis.
Nunc elementum fermentum wisi. Aenean placerat. Ut imperdiet, enim sed
gravida sollicitudin, felis odio placerat quam, ac pulvinar elit purus eget enim.
}

\begin{document}

\mbox{}\thispagestyle{empty}\clearpage% just for the example

\tableofcontents

\introduction{\Text}
\chapter{circular motion and gravitation}
\section{Circular motion}
\lipsum[1-28]

\introduction{\Text}
\chapter{circular motion and gravitation}
\section{Circular motion}
\lipsum[1-28]

\introduction{\Text}
\chapter{circular motion and gravitation}
\section{Circular motion}
\lipsum[1-18]

\end{document}

在此处输入图片描述

使用命令选择不同的颜色,该命令使用计数器的值\chapterbackcolor来获取其值;颜色已从包中定义;实际形式只有四种颜色:一种用于未编号的章节,另外三种用于前三个编号的章节。可以简单地定义更多颜色并将它们添加到更多章节的子句中:\ifcasechapter\definecolorxcolor\ifcase

\definecolor{color2}{RGB}{255,197,51}
\definecolor{color3}{RGB}{121,163,65}
\definecolor{color1}{RGB}{53,78,85}
\definecolor{color4}{RGB}{0,27,92}

\renewcommand\chapterbackcolor{%
  \ifcase\value{chapter}color4\or color1\or color2\else color3\fi}

所有章节标题和页眉元素的设计基本上是使用\node内的 s将元素放置在所需位置tikzpicture

仍有改进的空间:每章第一页的页码(plain使用功能轻松实现页面样式的重新定义fancyhdr),一种从一章更改另一章的标题图像的方法,但与迄今为止所做的相比,这些都是简单的任务。

相关内容