个性化标题 2

个性化标题 2

看到我之前的问题(已解决),我又有疑问了。现在我也个性化了内容,目录样式与章节样式相冲突。还有另一个关于此问题的帖子 问题

现在,我写下了这段代码:

\documentclass[twoside,fleqn]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage{blindtext}
\usepackage{xwatermark}
\usepackage{wallpaper}
\usepackage{grffile}
\usepackage[explicit]{titlesec}
\usepackage{bm,graphicx,empheq}
\usepackage{units,cancel,mathtools,lipsum,lmodern}
\usepackage{varwidth,verbatim,setspace}
\usepackage{anyfontsize,rotating,framed,titletoc}
\usepackage{amsmath,amssymb,contour}
\usepackage{booktabs,caption,multirow,marginnote}
\usepackage[titles]{tocloft}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{etoolbox}
\usepackage{xpatch}
\usepackage{listings}
\usepackage{wasysym}
\usepackage{float}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black,bookmarksdepth=3, urlcolor=black}
\usepackage[open,openlevel=0,color=red]{bookmark}
\floatstyle{boxed}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings,decorations.text}
\usetikzlibrary{patterns}


\newcommand*{\fancychapterstyle}{%
  \titleformat{\chapter}[display]
  {}{\begin{tikzpicture}[remember picture, overlay]%
\pgftext[right,x=14.5cm,y=0.2cm]{\color{red}\Huge\sf\bfseries \thechapter};%
\draw[fill=red,draw=red] (13,-.75) rectangle (20,1);%
\clip (13,-.75) rectangle (20,1);
\pgftext[right,x=14.5cm,y=0.2cm]{\color{white}\Huge\sf\bfseries\thechapter};%
\end{tikzpicture}}
  {20pt}{\tcbset{enhanced,width=\textwidth, interior style=white,before skip=1mm,after skip=1mm,
colbacktitle=white,boxrule=0mm,colframe=black,
left=0mm,right=0mm,boxsep=0mm,sharp corners}
\begin{tcolorbox}
    \begin{tikzfadingfrompicture}[name=tikz]
        \node[text width=15cm,text=transparent!20]
        {\fontsize{50}{70}\selectfont\textbf{\textsf{\chaptertitlename}}\\};
    \end{tikzfadingfrompicture}
    \begin{tikzpicture}
        \node[text width=15cm-2mm,text opacity=0,shade,path fading=tikz,fit fading=false,top color=violet,bottom color=red]
        {\fontsize{50}{70}\selectfont{\selectfont\textbf{\textsc{\chaptertitlename}}}\\};
    \end{tikzpicture}
    \end{tcolorbox}}
  \titlespacing*{\chapter}{0pt}{50pt}{40pt}
}



\begin{document}
\tableofcontents %just inserted here to show that have a different style from the other chapters.
\fancychapterstyle
\chapter{one}

\chapter{very long long title number two}

\end{document}

但我想要的不是 \chaptertitlename,而是章节的标题(它会打印单词“chapter”)。我尝试什么都不写,但没有成功。有什么想法吗?

答案1

将代码最小化到必要的范围并删除奇怪的字体选择(特别是我删除了\scshape大多数无衬线字体不可用的字体)。

由于您使用选项调用titlesecexplicit因此章节标题可用为#1,而不是\chaptertitlename。 在您的例子中,您必须使用##1因为您将代码放在宏定义中(我不确定它的用处)。

\documentclass[twoside,fleqn]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}

\usepackage[explicit]{titlesec}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings,decorations.text}
\usetikzlibrary{patterns}

\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  linkcolor=black,
  anchorcolor=black,
  citecolor=black,
  bookmarksdepth=3,
  urlcolor=black
}
\usepackage[open,openlevel=0,color=red]{bookmark}

\newcommand*{\fancychapterstyle}{%
  \titleformat{\chapter}[display]
    {}
    {\begin{tikzpicture}[remember picture, overlay]%
     \pgftext[right,x=14.5cm,y=0.2cm]{\color{red}\Huge\sffamily\bfseries\thechapter};%
     \draw[fill=red,draw=red] (13,-.75) rectangle (20,1);%
     \clip (13,-.75) rectangle (20,1);
     \pgftext[right,x=14.5cm,y=0.2cm]{\color{white}\Huge\sffamily\bfseries\thechapter};%
     \end{tikzpicture}}
     {20pt}
     {\tcbset{
        enhanced,width=\textwidth, interior style=white,before skip=1mm,after skip=1mm,
        colbacktitle=white,boxrule=0mm,colframe=black,
        left=0mm,right=0mm,boxsep=0mm,sharp corners
     }%
     \begin{tcolorbox}
     \begin{tikzfadingfrompicture}[name=tikz]
       \node[
         text width=15cm,
         text=transparent!20
       ]{\fontsize{50}{70}\sffamily\bfseries\ ##1\\};
     \end{tikzfadingfrompicture}
     \begin{tikzpicture}
       \node[
         text width=15cm-2mm,
         text opacity=0,
         shade,
         path fading=tikz,
         fit fading=false,
         top color=violet,
         bottom color=red
      ]{\fontsize{50}{70}\sffamily\bfseries ##1\\};
     \end{tikzpicture}
     \end{tcolorbox}}
  \titlespacing*{\chapter}{0pt}{50pt}{40pt}
}

\begin{document}
\fancychapterstyle

\tableofcontents %just inserted here to show that have a different style from the other chapters.
\chapter{one}

\chapter{very long long title number two}

\end{document}

目录页中的样式与其他章节相同,因为我将声明移到了\fancychapterstyle它前面。

在此处输入图片描述

相关内容