使用来自的代码章节标题(第一行)的背景边距填充?,我有这个 MWE:
\documentclass[10pt,twoside,openright]{book}
\usepackage[
paperwidth=15cm,
paperheight=20cm,
outer=2.5cm,
inner=2.5cm,
top=2.5cm,
bottom=2.5cm
]{geometry}
% \usepackage[a4,center]{crop} % `crop` line
% \crop[cam,info] % `crop` line
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}
\titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]
\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node (a) {\phantom{\normalfont\Large\bfseries Xy}};
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle (current page text area.west |- a.north) -| (current page text area.east |- a.south) -| cycle;
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west)$}) {#1};
\fi
\end{tikzpicture}}
\begin{document}
\frontmatter
\mainmatter
\part{Introduction}
\chapter*{The first chapter - without thinking anything else}
\newpage
\section{The first section - and some more text inside; and some more text inside; and some more text inside}
Blah, blah, blah...
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] (current page text area.north east) rectangle (current page text area.south west) ;
\end{tikzpicture}
\newpage
\section{This section starts on an even page}
Some text\dots
\end{document}
根据crop
发布的注释行,一切都很好(发送两次短信后 - 单击可获得完整分辨率):
但是,如果我取消注释这些crop
行并重新编译,则输出将被损坏:
就好像current page text area
(甚至\ifodd\value{page}
)无法确定作物是否活跃?!
有什么方法可以使该代码工作,即使crop
处于活动状态?
答案1
这是第三个答案。如图所示,这不会更正 添加的节点的位置。相反,它弥补了在部分缩略图配置的有限上下文中出现的tikzpagenodes
谎言。crop
\documentclass[10pt,twoside,openright]{book}
\usepackage[
paperwidth=15cm,
paperheight=20cm,
outer=2.5cm,
inner=2.5cm,
top=2.5cm,
bottom=2.5cm
]{geometry}
\usepackage[a4,center]{crop} % `crop` line
\crop[cam,info] % `crop` line
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}
\titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]
\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node (a) {\phantom{\normalfont\Large\bfseries Xy}};
\ifx\stockwidth\undefined
\coordinate (b) at (0,0);
\else
\coordinate (b) at (.5\paperwidth-.5\stockwidth,.5\stockheight-.5\paperheight);
\fi
\begin{scope}
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle ({$(current page text area.west) + (b)$} |- a.north) -| ({$(current page text area.east) + (b)$} |- a.south) -| cycle;
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east) + .5*(b)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west) + .5*(b)$}) {#1};
\fi
\end{scope}
\end{tikzpicture}}
\begin{document}
\frontmatter
\mainmatter
\part{Introduction}
\chapter*{The first chapter - without thinking anything else}
\newpage
\section{The first section - and some more text inside; and some more text inside; and some more text inside}
Blah, blah, blah...
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] (current page text area.north east) rectangle (current page text area.south west) ;
\end{tikzpicture}
\newpage
\section{This section starts on an even page}
Some text\dots
\end{document}
答案2
好的,我想我有一些方法,但并不完全确定;解决方法似乎是这样的:
\ifx\CROP@size\undefined
\typeout{CROP@size undefined}
\else
\typeout{CROP@size defined}
% must set via newlength so it survives descent into tikzpicture!
\setlength\gparindent{\parindent}
\setlength\gbaselineskip{\baselineskip}
\def\current@textarea@left{(1in+\hoffset+\currentsidemargin-\Gm@lmargin-\gparindent)}
\def\current@textarea@top{(1in+\voffset+\topmargin+\headheight+\headsep)+2*\[email protected]\gbaselineskip}
\fi
...也就是说,如果crop
已加载,则重新定义\current@textarea@left
和\current@textarea@top
来自tikzpagenodes
,因此左侧由调整\Gm@lmargin-\gparindent
,右侧由- 其中/是通过包调整的边距。2*\[email protected]\gbaselineskip
\Gm@l
tmargin
geometry
\gparindent
和是 parindent/baselineskip 的“全球化”版本\gbaselineskip
- 作为长度 - 否则当上述定义在内部运行时它们不会传播tikzpicture
(这会重置 parindent/baselineskip)。Gm@ 边距是有意义的,但我不知道为什么一开始会有这个位移 - 我通过反复试验找到了修复值,所以买者自慎。
下面的代码有一些额外的调试内容,它输出如下内容:
似乎还有少量的出血 - 这很好;而且,如果你只是评论这些crop
行,一切似乎都有效。
代码:
\documentclass[10pt,twoside,openright]{book}
\usepackage[
paperwidth=15cm,
paperheight=20cm,
outer=3.5cm,
inner=2.5cm,
top=2.5cm,
bottom=3.5cm
]{geometry}
\usepackage[a4,center]{crop}
\crop[cam,info]
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}
\makeatletter
\newlength\gparindent
\newlength\gbaselineskip
\ifx\CROP@size\undefined
\typeout{CROP@size undefined}
\else
\typeout{CROP@size defined}
% from tikzpagenodes:
% NB: \parindent, \baselineskip seem to dissapear inside \sectioncolouration
% \let\gparindent\parindent% NO
% \xdef\gparindent{\parindent}% NO!
% must set via newlength so it survives descent into tikzpicture!
\setlength\gparindent{\parindent}
\setlength\gbaselineskip{\baselineskip}
\typeout{gparindent-Start \the\gparindent}%
\def\current@textarea@left{(1in+\hoffset+\currentsidemargin-\Gm@lmargin-\gparindent)}
\def\current@textarea@top{(1in+\voffset+\topmargin+\headheight+\headsep)+2*\[email protected]\gbaselineskip}
% no need to repeat this:
% \@newtikzpagenode{current page text area}{%
% \def\southwest{\pgfpoint{\current@textarea@left}{\paperheight-\current@textarea@top-\textheight}}%
% \def\northeast{\pgfpoint{\current@textarea@left+\textwidth}{\paperheight-\current@textarea@top}}%
% }
\fi
\makeatother
\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]%
\typeout{gparindent-In-Sect-Col \the\gparindent}%
\node (a) {\phantom{\normalfont\Large\bfseries Xy}};
\path [fill=blue, fill opacity=.2, even odd rule]
(current page.west |- a.north) -| (current page.east |- a.south) -| cycle
(current page text area.west |- a.north) -| (current page text area.east |- a.south) -| cycle;
% (current page text area.south west) -| (current page text area.north east) -| cycle; % debug
\draw[red] (current page text area.north east) rectangle (current page text area.south west); % debug
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west)$}) {#1};
\fi
\end{tikzpicture}}
\titleformat{\section}[hang]{%
\typeout{gparindent-Sect \the\gparindent}%
\setlength\gparindent{\parindent}%
\setlength\gbaselineskip{\baselineskip}%
\normalfont\Large\bfseries\color{black!90}%
}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]
\begin{document}
\frontmatter
\mainmatter
\part{Introduction}
\chapter*{The first chapter - without thinking anything else}
\newpage
\section{The first section - and some more text inside; and some more text inside; and some more text inside}
% debug
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] (current page text area.north east) rectangle (current page text area.south west) ;
\draw[thick] (current page text area.east) rectangle ($(current page text area.west)+(0,1)$) ;
\end{tikzpicture}
\noindent
Blah, blah, blah... \lipsum[1-3]
% \newpage
\section{This section starts on an odd page}
Some text\dots
\end{document}
答案3
crop
似乎从根本上不同意tikzpagenodes
。或者,更确切地说,按照crop
要求撒谎,似乎从根本上不同意。这并不奇怪。如果文本区域不在你说的地方,事情出问题也就不足为奇了。
如果geometry
裁切标记是一种选项,那么这些可以正常工作:
\documentclass[10pt,twoside,openright]{book}
\usepackage[
paper=a4paper,
layoutsize={15cm,20cm},
layoutoffset={30mm,48.5mm},
outer=2.5cm,
inner=2.5cm,
top=2.5cm,
bottom=2.5cm,
showcrop,
]{geometry}
\usepackage{titlesec}
\usepackage{tikz}
\usetikzlibrary{external,calc}
\usepackage{tikzpagenodes}
\titleformat{\section}[hang]{\normalfont\Large\bfseries\color{black!90}}{\thesection}{1.5em}{\tikzexternaldisable\sectioncolouration{\Alph{section}}}[\tikzexternalenable]
\newcommand*\sectioncolouration[1]{%
\begin{tikzpicture}[baseline=(a.base), overlay, remember picture, inner xsep=0pt, outer sep=0pt]
\node (a) {\phantom{\normalfont\Large\bfseries Xy}};
\path [fill=blue, fill opacity=.2, even odd rule] (current page.west |- a.north) -| (current page.east |- a.south) -| cycle (current page text area.west |- a.north) -| (current page text area.east |- a.south) -| cycle;
\ifodd\value{page}
\node at (a -| {$(current page text area.east)!1/2!(current page.east)$}) {#1};
\else
\node at (a -| {$(current page text area.west)!1/2!(current page.west)$}) {#1};
\fi
\end{tikzpicture}}
\begin{document}
\frontmatter
\mainmatter
\part{Introduction}
\chapter*{The first chapter - without thinking anything else}
\newpage
\section{The first section - and some more text inside; and some more text inside; and some more text inside}
Blah, blah, blah...
\begin{tikzpicture}[remember picture,overlay]
\draw[thick] (current page text area.north east) rectangle (current page text area.south west) ;
\end{tikzpicture}
\newpage
\section{This section starts on an even page}
Some text\dots
\end{document}
答案4
我一直在从更普遍的角度研究这个问题。
通过扫描包装的文档crop
,似乎我们想要提取的最相关的值是\stockwidth
和\stockheight
,它们用于存储最终的纸张尺寸。
我们真正需要做的就是重新定义 的主要宏,以tikzpagenodes
解释\stock*
和之间的差异\paper*
。此代码为
\def\current@textarea@left{%
(1in+\hoffset+\currentsidemargin+.5*(\paperwidth-\stockwidth))%
}
\def\current@textarea@top{%
(1in+\voffset+\topmargin+\headheight+\headsep-.5*(\stockheight-\paperheight))%
}
\stock
这只是将和值之间的差值相加一半\paper
来解释偏移量。我们只需要一半的值,因为逻辑页面位于物理页面的中心,因此偏移量为差值的一半。
为了制作此产品的通用版本,我们将执行以下操作:
%fix the crop issue with tikzpagenodes
\@ifpackageloaded{crop}{
\@ifpackagewith{crop}{center}
{
\def\current@textarea@left{%
(1in+\hoffset+\currentsidemargin+.5*(\paperwidth-\stockwidth))}
\def\current@textarea@top{%
(1in+\voffset+\topmargin+\headheight+\headsep-.5*(\stockheight-\paperheight))}
}
{
\def\current@textarea@top{%
(1in+\voffset+\topmargin+\headheight+\headsep-(\stockheight-\paperheight))}
}
}
{%
}
现在,如果加载了裁剪包,我们可以自动调整偏移量。我还针对center
未使用该选项的情况进行了不同的调整,此时 很好\hoffset
,因此不需要调整,而\voffset
需要两倍的调整(因为我们位于页面的左上角)。
我在排版 RTL 和 LTR 文本时使用 XeTeX 对此进行了测试。
这实际上很有趣,因为该center
选项实际上设置了\hoffset
符合\voffset
我们需求的值,但调查这一点并不是必要的。