为什么我的节点没有正确对齐页面文本区域?

为什么我的节点没有正确对齐页面文本区域?

以下是 MWE:

\documentclass[fontsize=11pt,pagesize=auto,hidelinks,cleardoublepage=empty,parskip]{scrbook}
\usepackage[paperheight=10in,paperwidth=8in,margin=2cm,heightrounded,bindingoffset=5mm,showframe=false]{geometry}

\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}
    {\gdef\chapterlabel{} \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\%}}{0pt}
    {
        \begin{tikzpicture}[remember picture,overlay]%
            \node(main)[yshift=-7cm] at (current page.north west)
            {%
                \begin{tikzpicture}[remember picture, overlay]
                    \draw[fill=black] (0,0) rectangle (\paperwidth,7cm);
                    \node[anchor=east,yshift=-3cm] at (current page text area.north east) {\color{white}\large Chapter \thechapter};%
                    \node[anchor=east,yshift=-4cm] at (current page text area.north east) {\color{white} #1};%
                \end{tikzpicture}
            };%
        \end{tikzpicture}
    }
\titlespacing*{\chapter}{0pt}{0pt}{3cm}[0pt]

\begin{document}
    \chapter{Introduction}
    \blindtext[4]
\end{document}

其呈现形式为:

在此处输入图片描述

请注意,章节标题的右边缘与正文的右边缘不对齐。这是为什么?我不知道我在这里做错了什么。

答案1

Anode有一个inner sep,即从节点内容到节点边界的给定距离。默认情况下,这是 0.333em,您应该inner sep=0为两个节点设置。

除了 Christian 对titlesecKOMA 的评论之外,你还应该Zarko 在回答中提出的建议,而不是 nest tikzpicture。它有时会引发问题,而且这里完全不需要它。

在此处输入图片描述

\documentclass[fontsize=11pt, pagesize=auto, hidelinks, cleardoublepage=empty, parskip]{scrbook}
\usepackage[paperheight=10in, paperwidth=8in, margin=2cm, heightrounded, bindingoffset=5mm, showframe=false]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}
    {\gdef\chapterlabel{} \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\%}}{0pt}
    {
        \begin{tikzpicture}[remember picture,overlay]%
            \node(main)[yshift=-7cm] at (current page.north west)
            {%
                \begin{tikzpicture}[remember picture, overlay]
                    \draw[fill=black] (0,0) rectangle (\paperwidth,7cm);
                    \node[anchor=east,yshift=-3cm,inner sep=0] at (current page text area.north east) {\color{white}\large Chapter \thechapter};%
                    \node[anchor=east,yshift=-4cm,inner sep=0] at (current page text area.north east) {\color{white} #1};%
                \end{tikzpicture}
            };%
        \end{tikzpicture}
    }
\titlespacing*{\chapter}{0pt}{0pt}{3cm}[0pt]

\begin{document}
    \chapter{Introduction}
    \blindtext[4]
\end{document}

答案2

请注意,使用titlesec带有KOMA-Script 类不推荐。因此,这里有一个不带titlesecand的建议,tikz它也适用于目录等未编号的章节以及长度超过文本宽度的章节标题:

\documentclass[fontsize=11pt,cleardoublepage=empty,parskip,chapterprefix]{scrbook}
\usepackage[paperheight=10in,paperwidth=8in,
            margin=2cm,heightrounded,
            bindingoffset=5mm]{geometry}
\usepackage{xcolor}
\usepackage{blindtext}% only for dummy text

\renewcommand\raggedchapter{\raggedleft}
\RedeclareSectionCommand[
  font=\Huge\scshape\color{white},
  prefixfont=\large,
  beforeskip=-1sp,
  afterskip=1sp
]{chapter}

\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#1}{chapter}{%
    \parbox[b][5cm]{\textwidth}{%
      \makebox[0pt][c]{\textcolor{black}{\rule{2\paperwidth}{\paperheight}}}%
      \raggedchapter
      \raisebox{.5\baselineskip}{\parbox[b]{\textwidth}{\raggedchapter#2#3}}}%
      \par
  }{%
    #2#3% original definition for other levels
  }%
}

\begin{document}
\tableofcontents
\chapter{Introduction}
\Blindtext
\chapter{Chapter with long, long, long title that needs more than one line}
\Blindtext
\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

请注意,您必须使用不同的字体(例如,libertine使用)来获取章节标题\sffamily\bfseries\scshape

答案3

除了什么Torbjørn T.已指出,您不应嵌套tikzpicture

\documentclass[fontsize=11pt,pagesize=auto,
               hidelinks,cleardoublepage=empty,
                parskip]{scrbook}
\usepackage[paperheight=10in,
            paperwidth=8in,margin=2cm,heightrounded,
            bindingoffset=5mm]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{positioning}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\titleformat{\chapter}
    {\gdef\chapterlabel{} \normalfont\sffamily\Huge\bfseries\scshape}
    {\gdef\chapterlabel{\thechapter\%}}{0pt}
    {
    \begin{tikzpicture}[remember picture,overlay,
    every node/.append style={text=white, inner xsep=0pt}]
        \draw[fill=black] (current page.north west) rectangle (\paperwidth,-3cm);
        \node[left, yshift=-3cm, font=\large] at (current page text area.north east) {Chapter \thechapter};%
        \node[left,yshift=-4cm] at (current page text area.north east) {#1};%
    \end{tikzpicture}
    }
\titlespacing*{\chapter}{0pt}{0pt}{3cm}[0pt]

\begin{document}
    \chapter{Introduction}
    \blindtext[4]
\end{document}

上述代码在字体大小和文本颜色的使用方面也做了一些修改。

在此处输入图片描述

相关内容