给“hitbox”覆盖 tikzpicture

给“hitbox”覆盖 tikzpicture

当我开始新的一章时,我希望在页面顶部有一个漂亮的横幅。我已经使用带叠加层的 tikzpicture 制作了一个让我满意的横幅。但是我无法正确设置它的“hitbox”(边界框?)。如果我只是在 tikzpicture 之后开始书写,文本将放在顶部,因为横幅的高度大于顶部边距。

为了将文本放在正确的位置,我\vspace*{...}在横幅后面添加了...正确的高度。这对文本很有效。代码和输出如下所示:

\documentclass{report}

\newcommand{\margin}{2.5cm}
\newcommand{\bannerheight}{5cm}
\usepackage[a4paper, margin = \margin]{geometry}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{lipsum}

\definecolor{SnowBlue}{RGB}{230, 240, 250}
\providecommand{\ChapterColorA}{SnowBlue}

\begin{document}
    \stepcounter{chapter}
    ~
    \begin{tikzpicture}[overlay, remember picture, every node/.style = {scale = 2, inner sep = 0cm, outer sep = 0cm}]
        \coordinate (nw) at (current page.north west);
        \coordinate (se) at ($(current page.north east)-(0,\bannerheight)$);
        
        \fill[\ChapterColorA] (nw) rectangle (se);
        
        \node[anchor = north west] (title) at ($(nw)+(\margin, -\margin)$) {\huge\textbf{Cool Chapter}};
        
        \node[anchor = south west] () at (title.north west) {\large\textbf{Chapter \thechapter}};
    \end{tikzpicture}
    ~
    \newline
    \vspace*{\dimexpr(\bannerheight+1ex-\headsep-\headheight-\margin)}
    \newline\newline
    ~
    \lipsum[1]

    \begin{table}
        \centering
        \begin{tabular}{l}
            I \\ am \\ a \\ floating \\ table \\ that \\ 
            likes \\ to \\ hide \\ under \\ the \\ banner
        \end{tabular}
        \caption{A little table. :)}
    \end{table}
\end{document}

但是,如果有一个浮动元素想要位于页面顶部,则会将其放在横幅下方,然后将其\vspace*{...}应用到表格和文本之间。请参阅下面的输出:

然后我想到使用\newgeometry{top = \dimexpr(\bannerheight+1ex)}which 可以很好地处理文本和表格,但这样我还必须使用\restoregeometrywhich ,因为这意味着需要手动分页。当然,这是不可取的。

我怎样才能让文档的所有其他元素尊重横幅的存在,而不用在每个新章节进行手动调整?

编辑:我根据 Tom 的善意建议进行了更改。由于\@makechapterhead似乎只影响章节的图形部分(和一些换行惩罚),我觉得我可以简单地重新定义整个内容而不会有太大风险。无论如何,文本或浮动与横幅重叠的问题不再存在。代码和输出可以在下面找到。:)

\documentclass[oneside]{book}
\usepackage{lipsum}

\newcommand{\margin}{2.5cm}
\newcommand{\bannerheight}{5cm}
\usepackage[a4paper, margin = \margin]{geometry}

\newcommand{\titlecolor}{black}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\mybanner}%
{%
    \fill[blue!30] (nw) rectangle (se);
}

\makeatletter%
\def\@makechapterhead#1%
{%
    \tikz%
    [%
        remember picture,%
        overlay,%
        title/.style={inner sep = 0cm, outer sep = 0cm, color = \titlecolor, scale = 2}%
    ]%
    {%
        \coordinate (nw) at (current page.north west);
        \coordinate[yshift=-\bannerheight] (se) at (current page.north east);
        \coordinate (c) at ($(nw)!0.5!(se)$);
        
        \clip (nw) rectangle (se);%
        \tikzset{shift={(c)}}
        \mybanner%
        
        \node[anchor=north west, title] (title) at ($(nw)+(\margin, -\margin)$) {\bfseries\Huge#1};%
        \node[anchor=south west, title] () at (title.north west) {\bfseries\Large\@chapapp\space \thechapter};%
    }%
    \vspace{\dimexpr(-\margin+\bannerheight+1em)}%
    \par%
}%
\makeatother%

\begin{document}
    \chapter{Cool Chapter}
    
    \lipsum[1-3]
    
    \renewcommand{\titlecolor}{white}
    \renewcommand{\mybanner}
    {
        \node[yshift=2cm] {\includegraphics[width=\paperwidth]{test-image}};
        \fill[violet, opacity=0.3] (nw) rectangle (se);
    }  
    
    \chapter{Cooler Chapter}
    
    \lipsum[4]
\end{document}

在此处输入图片描述 在此处输入图片描述

答案1

您可以修补\@makechapterhead\@makeschapterhead如果您想要星号版本,也可以修补 ,并\chapter*{}添加横幅(例如\tableofcontents)。默认情况下,浮动不会出现在章节标题页中。

\documentclass{report}
\usepackage{etoolbox}
\usepackage{lipsum}
\newcommand{\margin}{2.5cm}
\newcommand{\bannerheight}{5cm}
\usepackage[a4paper, margin = \margin]{geometry}
\usepackage{tikz}
\definecolor{SnowBlue}{RGB}{230, 240, 250}
\makeatletter
\patchcmd{\@makechapterhead}{\vspace*{50\p@}}{\relax}{}{}
\patchcmd{\@makechapterhead}{\@chapapp}{%
\tikz[remember picture,overlay,shift={(current page.north west)}]{
\fill[SnowBlue] (0,0) rectangle ([yshift=-\bannerheight]current page.north east);
}\@chapapp%
}{}{}
\patchcmd{\@makeschapterhead}{\vspace*{50\p@}}{\relax}{}{}
\patchcmd{\@makeschapterhead}{\bfseries}{%
\bfseries\tikz[remember picture,overlay,shift={(current page.north west)}]{
\fill[SnowBlue] (0,0) rectangle ([yshift=-\bannerheight]current page.north east);
}%
}{}{}
\makeatother

\begin{document}
\tableofcontents
\chapter{Cool Chapter}
\lipsum[1-14]
\begin{table}
\centering
\begin{tabular}{l}
I \\ am \\ a \\ floating \\ table \\ that \\ 
likes \\ to \\ hide \\ under \\ the \\ banner
\end{tabular}
\caption{A little table. :)}
\end{table}
\end{document}

在此处输入图片描述

编辑:您可以通过在那里放置一个宏来修补该命令(例如\mybanner),并且每次在开始新的章节之前,您只需用不同的内容重新定义该宏:

\documentclass{report}
\usepackage{etoolbox}
\usepackage{lipsum}
\newcommand{\margin}{2.5cm}
\newcommand{\bannerheight}{5cm}
\usepackage[a4paper, margin = \margin]{geometry}
\usepackage{tikz}
\definecolor{SnowBlue}{RGB}{230, 240, 250}
\newcommand{\mybanner}{%
\tikz[remember picture,overlay,shift={(current page.north west)}]{
\fill[SnowBlue] (0,0) rectangle ([yshift=-\bannerheight]current page.north east);
}%
}
\makeatletter
\patchcmd{\@makechapterhead}{\vspace*{50\p@}}{\relax}{}{}
\patchcmd{\@makechapterhead}{\@chapapp}{%
\mybanner\@chapapp%
}{}{}
\patchcmd{\@makeschapterhead}{\vspace*{50\p@}}{\relax}{}{}
\patchcmd{\@makeschapterhead}{\bfseries}{%
\bfseries\mybanner%
}{}{}
\makeatother

\begin{document}
\tableofcontents
\chapter{Cool Chapter}
\lipsum[1-14]
\begin{table}
\centering
\begin{tabular}{l}
I \\ am \\ a \\ floating \\ table \\ that \\ 
likes \\ to \\ hide \\ under \\ the \\ banner
\end{tabular}
\caption{A little table. :)}
\end{table}
\renewcommand{\mybanner}{%
\tikz[remember picture,overlay,shift={(current page.north west)}]{
\fill[yellow,opacity=0.4] (0,0) rectangle ([yshift=-\bannerheight]current page.north east);
}%
}
\chapter{Cool Chapter 2}
\renewcommand{\mybanner}{%
\tikz[remember picture,overlay,shift={(current page.north west)}]{
\node [opacity=0.2] at ([shift={(0.5*\paperwidth,-0.5*\bannerheight)}]0,0) {\includegraphics[width=\paperwidth,height=\bannerheight]{example-image-plain}};
}%
}
\chapter{Cool Chapter 3}
\end{document}

在此处输入图片描述 在此处输入图片描述

相关内容