步骤 1:初步解决方案

步骤 1:初步解决方案

下图中的页面(来自帖子)已在\fancyhdr包中创建,但我需要类似这样的操作来保持不兼容的\titlesec使用并使用两页布局。

在此处输入图片描述

真的,我不知道如何在\titlesec界面上使用 tikz 代码,但为了节省您的时间,我准备了草稿。虽然不多,但是...

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{geometry}
\geometry{% Please make twoside settings 
   paperwidth=210mm, paperheight=297mm,
   bindingoffset=0mm, asymmetric,
   left=13mm,  %% or inner=23mm
   right=18mm, %% or outer=18mm
   top=11mm, bottom=11mm,
   headsep=3mm,
   footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage[svgnames,x11names]{xcolor}

\newpagestyle{redfancy}[]{% Please insert the settings}
\newpagestyle{bluefancy}[]{% Please insert the settings}

\begin{document}

% 1st red page
\pagestyle{redfancy} % color: Header/Footer/Margin color: Firebrick2
\sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}
\setfoot[\thepage][][]{}{}{\thepage}

\section{First Red section}
\subsection{Red A}
\subsubsection{Red AA}
\subsubsection{Red AB}

\newpage
% 2nd red page
\section{Second Red section}
\subsection{Red B}
\subsubsection{Red BA}
\subsubsection{Red BB}


\newpage % Blue page
\pagestyle{bluefancy} % color: Header/Footer/Margin color: DodgerBlue1
\section{Blue section}

\end{document}

这是期望结果的草图:

在此处输入图片描述


更新

根据那些很好地回答了这个问题的人的要求,我将解释一下我需要从titlesec包中获得什么。

首先是动态设计。在目录、简介、结尾部分,我需要更简单的设计,然后是花哨的色彩设计(红色页面、蓝色页面等)。

在此处输入图片描述

之前,我是这样做的:

%\usepackage[pagestyles]{titlesec}

\newpagestyle{tocandintro}[]{\headrule\setheadrule{0.5mm}\footrule\setfootrule{0.5mm}}

\pagestyle{tocandintro}
\sethead[Table of contents][][]{}{Table of contents}{\textsf{目次}}
\setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}

\tableofcontents

\sethead[Intro][][]{}{}{Intro}
\setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}
<Intro text>
\pagestyle{fancy} % And after - color fancy design

然后,我需要动态颜色标题,如下图所示。

在此处输入图片描述

之前,我是这样做的:

% --- Section
\colorlet{SectionFrameColor}{blue!50}
\colorlet{SectionFillColor}{blue!20}
\colorlet{SectionFontColor}{blue!80}

\titleformat{\section}[hang]{\Large\sffamily\color{SectionFontColor}}
{\begin{tikzpicture}[baseline={([yshift=-.8ex]current bounding box.center)}]
\node[thick,draw=SectionFrameColor,fill=SectionFillColor,rectangle,rounded corners,text=white] {\thesection};
\end{tikzpicture}}{12pt}{}%
[{\titlerule[1pt]}]

\titleformat{\paragraph}[hang]{\bfseries}{}{}{}[]
% And than re-define section color...

正如你所见,除了装饰需求之外,我想改变\段落格式。

答案1

您需要对此进行调整,我不建议这样做,因为我认为使用titlesec将导致比它本身更多的麻烦。但是,如果您坚持:

步骤 1:初步解决方案

此解决方案tikzpagenodesbackground包一起使用。这样页面样式命令就titlesec只处理页眉的内容。页面框架和页脚内容是使用 TiKZ 在页面背景上构建的。确保even odd rule只有框架外部填充颜色,并创建一个“孔”来包含页面内容。

\documentclass[a4paper]{article}

\usepackage[svgnames,x11names]{xcolor}% you need this before tikz to avoid an option clash
\usepackage{tikz,tikzpagenodes}
\usepackage{geometry}
\geometry{% Please make twoside settings
  paperwidth=210mm, paperheight=297mm,
  bindingoffset=0mm, asymmetric,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage{background}
\newcommand{\colourframe}[2][white]{%
  \clearpage
  \backgroundsetup{%
    angle=0,
    scale=1,
    opacity=1,
    contents={%
      \begin{tikzpicture}[remember picture, overlay]
        \path [fill=#2, even odd rule]
          (current page.south west) rectangle (current page.north east)
          (current page text area.south west) rectangle (current page text area.north east)
          ;
        \node [fill=#1, draw=black, anchor=north, text=black, inner xsep=5mm, inner ysep=1.5mm, yshift=-1.5mm] at (current page text area.south) {\thepage};
      \end{tikzpicture}},
    position=current page.south west,
    nodeanchor=south west
  }%
  \pagestyle{colourfancy}%
  \sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}}

\newpagestyle{colourfancy}[]{}

\begin{document}

  \colourframe[orange]{red}
  \section{First Red section}
  \subsection{Red A}
  \subsubsection{Red AA}
  \subsubsection{Red AB}

  \newpage
  % 2nd red page
  \section{Second Red section}
  \subsection{Red B}
  \subsubsection{Red BA}
  \subsubsection{Red BB}

  \colourframe[cyan]{blue}
  \section{Blue section}

\end{document}

红色和蓝色框架

请注意,我不确定您为什么说想要双面设置,因为您似乎并没有真正使用它们!也就是说,您已将所有内容设置为每页的边距都相同。这意味着外边距有时会大于内边距,有时会小于内边距。

步骤 2:调整内边距和外边距

然而,在您的草图中,输出更接近于:

\documentclass[a4paper,twoside]{article}

...

\geometry{% Please make twoside settings
  paperwidth=210mm, paperheight=297mm,
  bindingoffset=0mm,
  right=13mm,  %% or inner=23mm
  left=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

内边距较大,外边距较小

这样内部边距较大,外部边距较小。更常见的布局会反转这种行为:

\documentclass[a4paper,twoside]{article}

...

\geometry{% Please make twoside settings
  paperwidth=210mm, paperheight=297mm,
  bindingoffset=0mm,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

外边距较大,内边距较小

这样,较大的边距位于外侧,而两个较小的内边距则合并在双页跨页上。

步骤 3:缩小框架

上面的代码不允许页面本身有任何“喘息”空间。也就是说,内容紧贴着框架。这不是最佳选择。最好将框架缩小一点,远离页面内容。这可以使用库来实现calc

\documentclass[a4paper,twoside]{article}

\usepackage[svgnames,x11names]{xcolor}% you need this before tikz to avoid an option clash
\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{geometry}
\geometry{% Please make twoside settings
  bindingoffset=0mm,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage{background}
\newcommand{\colourframe}[2][white]{%
  \clearpage
  \backgroundsetup{%
    angle=0,
    scale=1,
    opacity=1,
    contents={%
      \begin{tikzpicture}[remember picture, overlay]
        \path [fill=#2, even odd rule]
          (current page.south west) rectangle (current page.north east)
          ($(current page text area.south west) - (2mm,2mm)$) rectangle ($(current page text area.north east) + (2mm,2mm)$)
          ;
        \node [fill=#1, draw=black, anchor=north, text=black, inner xsep=5mm, inner ysep=1.5mm, yshift=-1mm] at ($(current page text area.south) - (0,2mm)$) {\thepage};
      \end{tikzpicture}},
    position=current page.south west,
    nodeanchor=south west
  }%
  \pagestyle{colourfancy}%
  \sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}}

\newpagestyle{colourfancy}[]{}

\begin{document}

  \colourframe[orange]{red}
  \section{First Red section}
  \subsection{Red A}
  \subsubsection{Red AA}
  \subsubsection{Red AB}

  \newpage
  % 2nd red page
  \section{Second Red section}
  \subsection{Red B}
  \subsubsection{Red BA}
  \subsubsection{Red BB}

  \colourframe[cyan]{blue}
  \section{Blue section}

\end{document}

我指定了 2 毫米的收缩,但您可以根据需要进行调整。就我个人而言,我会稍微增加边距大小 - 我认为内容看起来太挤了,尤其是因为信息必须包含在这个空间内。但如果您缺少纸张,这似乎确实有效。

这是最终的双页跨页(即第 2 页和第 3 页):

缩小的框架

步骤 4:整合更新问题中的代码

这是将您在问题中添加的额外定制纳入其中的结果:

具有较小内边距的自定义布局

我已经进行了调整\colourframe[]{},以便它还可以配置章节标题、段落布局、页眉和页脚格式的更改,如您的示例所示。章节标题等的颜色基于为框架指定的颜色。与以前一样,可选参数指定包含页码的框的背景颜色。

\documentclass[a4paper,twoside]{article}

\usepackage[svgnames,x11names]{xcolor}% you need this before tikz to avoid an option clash
\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{geometry}
\geometry{% Please make twoside settings
  bindingoffset=0mm,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage{background}
\newcommand{\colourframe}[2][white]{%
  \clearpage
  \backgroundsetup{%
    angle=0,
    scale=1,
    opacity=1,
    contents={%
      \begin{tikzpicture}[remember picture, overlay]
        \path [fill=#2, even odd rule]
          (current page.south west) rectangle (current page.north east)
          ($(current page text area.south west) - (2mm,2mm)$) rectangle ($(current page text area.north east) + (2mm,2mm)$)
          ;
        \node [fill=#1, draw=black, anchor=north, text=black, inner xsep=5mm, inner ysep=1.5mm, yshift=-1mm] at ($(current page text area.south) - (0,2mm)$) {\thepage};
      \end{tikzpicture}},
    position=current page.south west,
    nodeanchor=south west
  }%
  \pagestyle{colourfancy}%
  \sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}%
  \setfoot[][][]{}{}{}%
  \colorlet{SectionFrameColor}{#2!50}%
  \colorlet{SectionFillColor}{#2!20}%
  \colorlet{SectionFontColor}{#2!80}%
  \titleformat{\section}[hang]{%
    \Large\sffamily\color{SectionFontColor}%
  }{%
    \begin{tikzpicture}
      [
        baseline={([yshift=-.8ex]current bounding box.center)},
      ]
      \node [thick, draw=SectionFrameColor, fill=SectionFillColor, rectangle, rounded corners, text=white] {\thesection};
    \end{tikzpicture}%
  }{12pt}{}[{\titlerule[1pt]}]%
  \titleformat{\paragraph}[hang]{\bfseries}{}{}{}[]}

\newpagestyle{colourfancy}[]{}
\newpagestyle{tocandintro}[]{\headrule\setheadrule{0.5mm}\footrule\setfootrule{0.5mm}}
\backgroundsetup{%
  contents={},
}

\begin{document}

  \pagestyle{tocandintro}
  \sethead[Table of contents][][]{}{Table of contents}{\textsf{目次}}
  \setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}

  \tableofcontents

  \clearpage% uncomment if you don't want to have Intro in the header of the final contents page
  \sethead[Intro][][]{}{}{Intro}
  Intro text

  \colourframe[orange]{red}
  \section{First Red section}
  \subsection{Red A}
  \subsubsection{Red AA}
  \subsubsection{Red AB}

  \newpage
  % 2nd red page
  \section{Second Red section}
  \subsection{Red B}
  \subsubsection{Red BA}
  \subsubsection{Red BB}

  \colourframe[cyan]{blue}
  \section{Blue section}

\end{document}

重新添加asymmetric页面布局geometry

具有不对称边距的自定义布局

请注意,边距与您在这种情况下获得的边距非常相似,oneside即为左侧和右侧定义边距而不是內部和外部。

如果您只想要内部更大的边距,请切换左右边距的值而不是通过asymmetric。这将为您提供:

具有更大内边距的自定义布局

答案2

这是一个初步版本(在进一步测试时可能需要进行一些调整):

在此处输入图片描述

代码:

\documentclass[a4paper]{article}
\usepackage[demo]{graphicx}
\usepackage[all]{background}

\usepackage{geometry}
\geometry{% Please make twoside settings 
   paperwidth=210mm, paperheight=297mm,
   bindingoffset=0mm, asymmetric,
   inner=23mm,  %% or inner=23mm
   outer=18mm, %% or outer=18mm
   top=11mm, 
   bottom=11mm,
   headsep=3mm,
   footskip=3mm,
%   showframe,
}


\usepackage{lipsum}
\usepackage{layout}
\usepackage{tikz}
\usetikzlibrary{calc} 
%\usepackage{showframe} 


\usepackage{changepage}
\strictpagecheck

\newcommand*{\HeaderIndent}{2.0cm}%
\newlength{\MinimumPageNodeHeight}
\setlength{\MinimumPageNodeHeight}{3.0ex}

\newcommand*{\HeaderXLocation}{\dimexpr1.0in+\hoffset+\HeaderIndent\relax}%
\newcommand*{\HeaderYLocation}{\dimexpr1.0in+\voffset+\topmargin+\headheight\relax}%
\newcommand*{\FooterXLocation}{\dimexpr1.0in+\hoffset+\oddsidemargin+0.5\textwidth\relax}%
\newcommand*{\FooterYLocation}{\dimexpr1.0in+\voffset+\topmargin+\headheight+\headsep+\textheight+\footskip\relax}%

\newcommand*{\VOffsetTop}{\HeaderYLocation-\headsep}% 
\newcommand*{\VLocationBottom}{\dimexpr1.0in+\voffset+\topmargin+\headheight+\headsep+\textheight+\footskip}% vertical offset
\newcommand*{\VOffsetBottom}{\dimexpr\paperheight-\VLocationBottom-0.5\MinimumPageNodeHeight\relax}% vertical offset
\newcommand*{\HOffsetInner}{\dimexpr1.0in+\hoffset+\oddsidemargin}% horizontal offset
\newcommand*{\HOffsetOuter}{\dimexpr1.0in+\hoffset+\evensidemargin}% horizontal offset

\newcommand*{\PageBorderFillColor}{}%
\newcommand*{\PageNumberFillColor}{}%

\newcommand*{\SetPageStyle}[2]{%
    \xdef\PageBorderFillColor{#1}%
    \xdef\PageNumberFillColor{#2}%
}%


%% http://tex.stackexchange.com/questions/62241/how-to-get-the-current-chapter-name-section-name-subsection-name-etc
\let\Sectionmark\sectionmark
\def\sectionmark#1{\def\CurrentSectionName{#1}\Sectionmark{#1}}


\pagestyle{empty}
\newcommand{\MyTikzLogo}{% For a logo drawn with TikZ
\begin{tikzpicture}[remember picture,overlay,draw=black,ultra thick]
\checkoddpage
\ifoddpage
    \fill [\PageBorderFillColor] (current page.south west) rectangle ++(\HOffsetInner,\paperheight);
    \fill [\PageBorderFillColor] (current page.south west) rectangle ++(\paperwidth,\VOffsetBottom);
    \fill [\PageBorderFillColor] (current page.south east) rectangle ++(-\HOffsetOuter,\paperheight);
    \fill [\PageBorderFillColor] (current page.north east) rectangle ++(-\paperwidth,-\VOffsetTop);
    
    \coordinate (Title Location) at 
            ($(current page.north west)+(\HeaderXLocation,-\HeaderYLocation)+(\oddsidemargin,0)$);
    \coordinate (Page Number Location) at 
            ($(current page.north west)+(\FooterXLocation,-\FooterYLocation)$);
\else
    \fill [\PageBorderFillColor] (current page.south east) rectangle ++(-\HOffsetInner,\paperheight);
    \fill [\PageBorderFillColor] (current page.south east) rectangle ++(-\paperwidth,\VOffsetBottom);
    \fill [\PageBorderFillColor] (current page.south west) rectangle ++(\HOffsetOuter,\paperheight);
    \fill [\PageBorderFillColor] (current page.north east) rectangle ++(-\paperwidth,-\VOffsetTop);
    
    \coordinate (Title Location) at 
            ($(current page.north west)+(\dimexpr\HeaderXLocation,-\HeaderYLocation)+(\evensidemargin,0)+(\marginparsep,0)$);
    \coordinate (Page Number Location) at 
            ($(current page.north east)+(-\FooterXLocation,-\FooterYLocation)$);
\fi

\node [draw=none, fill=none, anchor=south west, text=black, font=\bfseries]
        at (Title Location) {\HeaderTitle};
\node [
        draw=black, 
        fill=\PageNumberFillColor, 
        minimum width=1.5cm, 
        minimum height=\MinimumPageNodeHeight, 
        anchor=center, 
        text=black
    ] at (Page Number Location) {\thepage};
 \end{tikzpicture}
}

\newcommand*{\HeaderTitle}{\thesection\ \CurrentSectionName}

\SetBgContents{\MyTikzLogo}% Set tikz picture

\SetBgPosition{current page.north west}% Select location
\SetBgOpacity{1.0}% Select opacity
\SetBgAngle{0.0}% Select roation of logo
\SetBgScale{1.0}% Select scale factor of logo


\begin{document}
\SetPageStyle{red!75}{orange}
\section{First Red section}
\subsection{Red A}
\subsubsection{Red AA}
\subsubsection{Red AB}

\newpage
% 2nd red page
\section{Second Red section}
\subsection{Red B}
\subsubsection{Red BA}
\subsubsection{Red BB}


\newpage % Blue page
\SetPageStyle{blue!50}{teal}
\section{Blue section}

\newpage % Green page
\SetPageStyle{green!50}{magenta}
\section{Green section}

\end{document}

相关内容