在下面的代码中,如果章节编号为 2 个字符或更多,则会与章节名称冲突。如何将章节编号移近文档边缘,如下图所示:
这是我当前的代码:
\documentclass[oneside,12pt,letterpaper,draft]{book}
\usepackage[usenames,dvipsnames,svgnames,table,xcdraw]{xcolor}
\usepackage{tikz}
\usepackage[explicit,calcwidth]{titlesec}
%======================================================================================
% CHAPTER THUMB
%======================================================================================
\usetikzlibrary{tikzmark,calc,arrows,shapes,decorations.pathreplacing,pgfplots.groupplots, matrix}
\tikzset{every picture/.style={remember picture}}
\pagestyle{plain}
%======================================================================================
% PAGE HEADERS
%======================================================================================
\def\subsectiontitle{}
\renewcommand{\sectionmark}[1]{\markright{\sffamily\normalsize#1}{}}
\renewcommand{\subsectionmark}[1]{\def\subsectiontitle{#1}}
\usepackage{etoolbox,fancyhdr} % Required for header and footer configuration
\usepackage{csquotes}
\patchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{fancy}}{}{}
\newif\ifchapterwidthstar
\newcommand\printchapternumber{\ifchapterwidthstar\else\thechapter\fi}
\makeatletter
\patchcmd\@chapter{\@afterheading}{\chapterwidthstarfalse\@afterheading}{}{}
\patchcmd\@schapter{\@afterheading}{\chapterwidthstartrue\@afterheading}{}{}
\makeatother
\hrule width\headwidth height\headrulewidth depth\headrulewidth
\definecolor{line}{RGB}{63,200,244}
\definecolor{fillh}{RGB}{225,233,251}
\renewcommand{\headrulewidth}{0pt}
\newcommand\hdheight{1in}
\newcommand\ftheight{.5in}
\newsavebox\headimageodd
\newsavebox\headimageeven
\NewDocumentCommand{\headimages}{m O{} m O{}}{%
% #1 - mandotory argument: first image
% #2 - optional argument for first image
% #3 - mandotory argument: second image
% #4 - optional argument for second image
\savebox{\headimageeven}{%
\includegraphics[#2]{#1}%
}%
\savebox{\headimageodd}{%
\includegraphics[#4]{#3}%
}%
}
\headimages{example-image-a}[scale=0.30]{example-image-b}[scale=0.15]
\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{}{0pt}{\Huge#1}
\titlespacing*{\chapter} {0pt}{10pt}{30pt}
\titleformat{\chapter}[display]
{}{}{0pt}{\normalfont\fontsize{30}{30}\bfseries#1}[\leavevmode\thispagestyle{fancy}]
\titlespacing*{\chapter} {0pt}{10pt}{15pt}
\renewcommand{\chaptermark}[1]{\markboth{\normalsize\bfseries \ #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\sffamily\Large#1}{}}
\fancyhf{}
\fancyhead[LO]{\rightmark%
\begin{tikzpicture}[overlay,remember picture]
\node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
text height=4cm,align=center,anchor=north east]
at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $)
{\rotatebox{90}{\parbox{4cm}{%
\centering\textcolor{white}{\bfseries\scshape\rightmark \\ \sffamily\subsectiontitle}}}};
\end{tikzpicture}} % Print the nearest section name on the left side of odd pages
\fancyhead[O]{%
\begin{tikzpicture}[overlay, remember picture]%
\fill[blue!20] (current page.north west) rectangle ($(current page.north east)+(0,-\hdheight)$);
\draw[line] ([yshift=-\hdheight]current page.north west) -- ([yshift=-\hdheight]current page.north east);
\ifnum\value{chapter}=0
\node[anchor=south west, text width=12cm, text=white, font=\fontsize{.42cm}{1.5cm}\selectfont\bfseries] at ($(current page.north west)+(.5\hdheight,-\hdheight)$) {\raggedleft\textbf{\MakeUppercase{This is very very very long chapter heading}}};
\else
\node[anchor=south west, text width=1.8cm, text=white, font=\fontsize{2cm}{1.5cm}\selectfont] (oddpagenum) at ($(current page.north west)+(.5\hdheight,-\hdheight)$) {\textbf{\printchapternumber}};
\node[anchor=south west, text width=12cm, text=white, font=\fontsize{.4cm}{1.5cm}\selectfont] (chapter) at (oddpagenum.south east) {\textbf{\MakeUppercase{This is very very ver long chapter heading}}};
\fi
\node[anchor=north east, minimum size=1in, inner xsep=5mm] at (current page.north east) {\usebox\headimageodd};
\end{tikzpicture}
}%
\fancyhead[E]{%
\begin{tikzpicture}[overlay, remember picture]%
\fill[blue!20] (current page.north west) rectangle ($(current page.north east)+(0,-\hdheight)$);
\draw[line] ([yshift=-\hdheight]current page.north west) -- ([yshift=-\hdheight]current page.north east);
\node[align=right, anchor=south east, text width=12cm, text=white, font=\fontsize{.4cm}{1.5cm}\selectfont] (evenpagenum) at ($(current page.north east)+(-.5\hdheight,-\hdheight)$) {\Huge\textbf{\rightmark}};
\node[anchor=north west, minimum size=1in, inner xsep=5mm] at (current page.north west) {\usebox\headimageeven};
\end{tikzpicture}
}
% Removes the header from odd empty pages at the end of chapters
\makeatletter
\renewcommand{\cleardoublepage}{
\clearpage\ifodd\c@page\else
\hbox{}
\vspace*{\fill}
\thispagestyle{empty}
\newpage
\fi}
\makeatother
\begin{document}
\part{Text Part 1}
Text to be included as a paragraph below custom part. The text should be on the same page just under the heading.
\renewcommand\thechapter{P1}
\chapter{Problem 1}
Here is my text for Problem 1.
\chapter{Solution 1}
Here is my text for Solution 1.
\end{document}