我想自定义章节编号背景,但它只在使用命令时出现\chapter
,而使用时不会出现\chapter*
。
我还希望只有在使用\chapter
命令时才会出现“章节”一词。
\documentclass[svgnames]{book}
\usepackage{tikz}
\usetikzlibrary{,positioning,matrix,calc, shapes.arrows, shadows.blur,shadows}
%%%%%%%%%%%%%% CHAPTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[explicit]{titlesec}
\newcommand*\chapterlabel{}
\titleformat{\chapter}
{\gdef\chapterlabel{}
\normalfont\sffamily\Huge\bfseries\scshape}
{\gdef\chapterlabel{\thechapter\ }}{0pt}
{\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-3cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
\fill[fill=LightSkyBlue] (0,0) rectangle
(\paperwidth,3cm);
\node[xshift=.5\paperwidth,rectangle,
rounded corners=20pt,inner sep=11pt,
fill=MidnightBlue]
{\color{white}#1};%anchor=east,xshift=.9\paperwidth
\node[yshift=1.5cm, anchor=west,rectangle,inner sep=11pt, fill=MidnightBlue]
{\color{white}\fontsize{30}{30}\selectfont{\chapterlabel}};
\end{tikzpicture}
};
\end{tikzpicture}
}
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}
\begin{document}
\chapter*{title}
\chapter{title}
\end{document}
答案1
如果我很清楚你想要什么,你可以用和numberless
键轻松找到解决方案:\titleformat
\titlespacing*
\documentclass[svgnames]{book}
\usepackage{tikz}
\usetikzlibrary{,positioning,matrix,calc, shapes.arrows, shadows.blur,shadows}
\usepackage{lipsum}
%%%%%%%%%%%%%% CHAPTER %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[explicit]{titlesec}
\newcommand*\chapterlabel{}
\titleformat{\chapter}[block]
{\gdef\chapterlabel{}
\normalfont\sffamily\Huge\bfseries\scshape}
{\gdef\chapterlabel{\chaptername\ \thechapter\ }}{0pt}
{\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-3cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
\fill[fill=LightSkyBlue] (0,0) rectangle
(\paperwidth,3cm);
\node[xshift=.5\paperwidth,rectangle,
rounded corners=20pt,inner sep=11pt,
fill=MidnightBlue]
{\color{white}#1};%anchor=east,xshift=.9\paperwidth
\node[yshift=1.5cm, anchor=west,rectangle,inner sep=11pt, fill=MidnightBlue]
{\color{white}\fontsize{30}{30}\selectfont{\chapterlabel}};
\end{tikzpicture}
};
\end{tikzpicture}
}
\titleformat{name=\chapter, numberless}[block]
{ \normalfont\sffamily\Huge\bfseries\scshape\filcenter}
{}{0pt}
{#1}
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}
\titleformat{name=\chapter, numberless}[block]
{ \normalfont\sffamily\Huge\bfseries\scshape\filcenter}
{}{0pt}
{#1}
\titlespacing*{name=\chapter, numberless}{0pt}{-70pt}{60pt}
\begin{document}
\chapter*{title}
\lipsum[1-10]
\chapter{title}
\lipsum[11-20]
\end{document}