单个章节的每一页中都有垂直矩形

单个章节的每一页中都有垂直矩形

我想创建一个特殊的章节,其中每页的外侧都会有一个垂直矩形。

我希望这个盒子只出现在这种特殊的章节中。

我在这里找到的代码是这样的:

\documentclass[a4paper,12pt]{book}
 \usepackage[all]{background}

 \usepackage{lipsum}
  \usepackage{showframe}
  \usepackage{tikz}
  \usetikzlibrary{calc}
  \newcommand*{\FrameXStart}{0.0}% in cm
  \newcommand*{\FrameXEnd}{2.0}% in cm
  \newcommand*{\FrameYClip}{0.0}% shift in cm of the frame, both from top and bottom
  \newcommand*{\FrameTextShift}{-1.5}% shift in cm from the top
  \newcommand*{\PageNumberLocation}{4.5}% in cm, from bottom
  \newcommand*{\ChapterName}{}%
  \newcommand*{\OldChapter}{}%
  \let\OldChapter\chapter
  \def\chapter#1{\def\ChapterName{#1}\OldChapter{#1}}%

  \newcommand*{\SectionName}{}%
  \newcommand*{\OldSection}{}%
  \let\OldSection\section
  \def\section#1{\def\SectionName{#1}\OldSection{#1}}%

   \newcommand*{\FrameTitle}{\ChapterName: \SectionName}%

   \tikzset{Frame Style/.style={fill=blue, draw=none, fill opacity=1}}
   \tikzset{Title Style/.style={scale=2.75, text=white, rotate=-90, anchor=west}}
   \tikzset{Title Top Shift/.style={xshift=\Midpoint cm, yshift=\FrameTextShift cm}}
   \tikzset{Title Bottom Shift/.style={xshift=-\Midpoint cm, yshift=\FrameTextShift
    cm}}

    \tikzset{Page Number Style/.style={scale=2.0, text=white, draw=none}}
    \tikzset{Page Number Odd Shift/.style={ xshift= \Midpoint cm, 
    yshift=\PageNumberLocation cm}}
     \tikzset{Page Number Even Shift/.style={xshift=-\Midpoint cm, 
     yshift=\PageNumberLocation cm}}

    \pgfmathsetmacro{\Midpoint}{0.5*(\FrameXStart+\FrameXEnd)}
    \newcommand{\MyGraphicLogo}{% For imported graphic logo
      \begin{tikzpicture}[remember picture,overlay]
       \checkoddpage
     \ifoddpage
       \draw [Frame Style] 
        ($(current page.north west)+(\FrameXStart cm,-\FrameYClip cm)$) rectangle
        ($(current page.south west)+(\FrameXEnd   cm, \FrameYClip cm)$);
        \node [Title Top Shift, Title Style] at (current page.north west) {};
         \node [Page Number Odd Shift, Page Number Style] at (current page.south west) 
        {};
      \else
     \draw [Frame Style] 
       ($(current page.north east)+(-\FrameXStart cm,-\FrameYClip cm)$) rectangle
        ($(current page.south east)+(-\FrameXEnd   cm, \FrameYClip cm)$);
      \node [Title Bottom Shift, Title Style] at (current page.north east) {};
       \node [Page Number Even Shift, Page Number Style] at (current page.south east) 
       {};
       \fi 
        \end{tikzpicture}}

         \SetBgContents{\MyGraphicLogo}% Select frame to be drawn
        \SetBgOpacity{1.0}% Select opacity
       \SetBgAngle{0.0}% Select roation of logo
       \SetBgScale{1.0}% Select scale factor of logo

       \begin{document}
         \chapter{Chapter Title}
          \section{Section Title 1}
          \lipsum[1-6]
         \section{Section Title 2}
         \lipsum[2-9]
       \end{document}

答案1

这是一个使用这个时间的选项background包裹:

\documentclass{book}
\usepackage[a6paper]{geometry}% just for the example
\usepackage[contents={},scale=1,opacity=1,color=black,angle=0]{background}
\usepackage{lipsum}% just to generate text for the example

\AddEverypageHook{%
\ifnum\value{chapter}=2\relax
\backgroundsetup{contents={\tikz\fill[blue!20] (0,0) rectangle (1cm,5cm);}}
  \ifodd\value{page}
    \backgroundsetup{position={current page.east},hshift=-20pt}
  \else
    \backgroundsetup{position={current page.west},hshift=20pt}
  \fi
\fi
\BgMaterial
}

\begin{document}

\chapter{Test Chapter One}
\lipsum[1]
\chapter{Test Chapter Two}
\lipsum[1]
\chapter{Test Chapter Three}
\lipsum[1]

\end{document}

在此处输入图片描述

由于没有给出矩形及其精确位置的描述,我使用了一个填充的矩形并选择了一些特定的位置,但这些设置可以轻松更改。

答案2

使用以下代码,在第 1 章的每一页上绘制一个划定边距区域的蓝色矩形;可以随意更改章节编号和矩形颜色。

请注意,我使用该oneside选项来获取更简洁的输出屏幕截图。

在此处输入图片描述

\documentclass[oneside]{book}
%\usepackage[showframe]{geometry}
\usepackage{xifthen}
\usepackage{everypage}
\usepackage{tikzpagenodes}
\usepackage{lipsum} % dummy text
\usepackage{xcolor}

\AddEverypageHook{%
\ifthenelse{\thechapter=1}{
        \tikz[remember picture,overlay] {%
            \draw [blue] (current page marginpar area.south west)
                rectangle (current page marginpar area.north east);
        }
%\fi
}{}
}


\begin{document}   
\lipsum[1]

\chapter{foo}

\lipsum[2-4]

\chapter{bar}

\lipsum[5-7]
\end{document}

相关内容