我正在使用回忆录课我已经对章节进行了样式设置,如代码和屏幕截图 1 所示。
但是,对于一些标题较长的章节,我希望 tikz 框的颜色和间距有所不同,并使标题文本在换行时正确对齐。
不过,对于较长的标题,我似乎无法覆盖默认章节样式 - 我想使用名为 chapterLongerTitle 的 \newcommand(参见屏幕截图 2 和 \chapterLongerTitle 的代码)。我在 stackexchange 上查看了与此主题相关的多个问题,但无果。有人能帮忙吗?
\usepackage{calc, xcolor, tikz}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MY DEFAULT CHAPTER STYLING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifNoChapNumber
\makeatletter\setlength\midchapskip{0pt}\makechapterstyle{myStyle}
{
\renewcommand\chapternamenum{} % erase default style
\renewcommand\printchaptername{} % erase default style
\renewcommand\printchapternum{} % erase default style
\renewcommand\chapnumfont{\Large\sffamily}
\renewcommand\chaptitlefont{\huge\bfseries\sffamily}
\renewcommand\printchaptertitle[1]{%
\begin{tikzpicture}[scale=1.7]
\fill[red] (0,0) rectangle (0.5,0.5);
\fill[red] (0,1.5) rectangle (0.5,1);
\fill[red] (1,-0.5) rectangle (0.5,0);
\fill[red] (1,2) rectangle (0.5,1.5);
\end{tikzpicture}
\ifNoChapNumber{ % if there is no chapter number
\vspace*{-6.4em}\hspace*{3.4em}%
\chaptitlefont\textcolor{purple}{##1} % chapter title
\vspace*{2em}
}
\else{ % else, if there is a chapter number
\vspace*{-9em}\hspace*{3.2em}%
\chapnumfont\textcolor{red}{Chapter \thechapter} % chapter number
\vspace*{0.5em}\hspace*{2.3em}%
\chaptitlefont\textcolor{purple}{ ##1} % chapter title
\vspace*{2em}
}
\fi
\NoChapNumberfalse
}
\renewcommand\printchapternonum{\NoChapNumbertrue}
}
\makeatother
\chapterstyle{myStyle}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STYLING FOR LONGER CHAPTER TITLE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\chaptercopy\chapter % \let creates a backup of the original \chapter definition as \chaptercopy in this case
\newcommand\chapterLongerTitle[1]{\chaptercopy{#1}}{%
\renewcommand\printchaptertitle[1]{%
\begin{tikzpicture}[scale=1.7]
\fill[yellow] (0,0) rectangle (0.5,0.5); % bottom yellow square
\fill[yellow] (0,2) rectangle (0.5,1.5); % top yellow square
\fill[blue] (1,-0.5) rectangle (0.5,0); % bottom blue square
\fill[blue] (1,2.5) rectangle (0.5,2); % top blue square
\end{tikzpicture}
\vspace*{-9em}\hspace*{3.2em}%
\chapnumfont\textcolor{red}{Chapter \thechapter} % chapter number
\vspace*{0.5em}\hspace*{2.3em}%
\chaptitlefont\textcolor{purple}{ #1} % chapter title
\vspace*{2em}
}
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT BEGINS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\chapter{Introduction}
\chapterLongerTitle{A two-line title goes here in but not aligning properly}
\end{document}
谢谢
答案1
tikz
这是一个例子,你的想法是对的,把所有东西都放到建筑中比移动东西来配合装饰要容易得多
编辑:添加了overlay
让装饰不占用空间并因此进入边距的选项。如果不这样做,我们会因为\parbox
文本宽度而得到一个过满的水平框。
\documentclass[a4paper]{memoir}
\setulmarginsandblock{2cm}*1
\checkandfixthelayout
\usepackage{xcolor, tikz}
\usetikzlibrary{calc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MY DEFAULT CHAPTER STYLING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifNoChapNumber
\makeatletter
\setlength\midchapskip{0pt}
\makechapterstyle{myStyle}
{
\renewcommand\chapternamenum{} % erase default style
\renewcommand\printchaptername{} % erase default style
\renewcommand\printchapternum{} % erase default style
\renewcommand\chapnumfont{\Large\sffamily}
\renewcommand\chaptitlefont{\huge\bfseries\sffamily}
\renewcommand\printchaptertitle[1]{%
\begin{tikzpicture}
\node[inner sep=0pt,anchor=north west] (title) at (0,0)
{\parbox{\textwidth}{\raggedright\chaptitlefont ##1}};
\coordinate (decobot) at (title.south west);
\coordinate (decotop) at (title.north west);
\ifNoChapNumber\else
\node[inner sep=0pt,anchor=south west] (chapname) at
($(title.north west)+(0,0.2)$) {\chapnumfont\@chapapp~\thechapter};
\coordinate (decotop) at (chapname.north west);
\fi
\def\DecoAdj{0.2}
\coordinate (decotop) at ($(decotop)+(-\DecoAdj,\DecoAdj)$);
\coordinate (decobot) at ($(decobot)+(-\DecoAdj,-\DecoAdj)$);
\fill[red,overlay] (decotop) rectangle ++(-\DecoAdj,-\DecoAdj);
\fill[red,overlay] (decotop) rectangle ++(\DecoAdj,\DecoAdj);
\fill[red,overlay] (decobot) rectangle ++(\DecoAdj,-\DecoAdj);
\fill[red,overlay] (decobot) rectangle ++(-\DecoAdj,\DecoAdj);
\end{tikzpicture}
}
\renewcommand\printchapternonum{\NoChapNumbertrue}
}
\makeatother
\chapterstyle{myStyle}
% just to show all 3 examples on one page
\let\clearforchapter\relax
\begin{document}
\chapter{Introduction}
\chapter{A two-line title goes here in but not aligning properly}
\chapter*{No number}
\end{document}