我想在左侧写成“第 1 章。章节名称”,在右侧,标题具有与图像相同的背景颜色。这是我所做的。我是 Latex 的新手,我发现为标题添加背景颜色非常困难。
\usepackage[utf8]{inputenc}
\usepackage[english, vietnamese]{babel}
\usepackage{wrapfig}
\usepackage[dvipsnames]{xcolor}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{color,graphicx,multicol,hyperref,geometry,graphicx,amsmath,tikz,amssymb,pgf,tkz-tab,amsfonts,amsthm,array,fancyhdr}
\usepackage{lipsum}
\usepackage{amsxtra, latexsym, amscd, enumerate, ifthen, amstext}
\usepackage{array, tabularx, longtable}
\usepackage{mathrsfs}
\usepackage[all]{xy}
\usepackage{subcaption}
\usepackage{csquotes}
\usepackage{textcmds}
\DeclareRobustCommand{\divby}{\mathrel{\vbox{\baselineskip.65ex\lineskiplimit0pt\hbox{.}\hbox{.}\hbox{.}}}}
\DeclareGraphicsExtensions{.pdf,.jpg,.png,.bmp}
\let\mod=\undefined
\let\top=\undefined
\DeclareMathOperator{\im}{Im}
\DeclareMathOperator{\Ker}{Ker}
\DeclareMathOperator{\rk}{rank}
\newtheorem{defi}{Definition}[section]
\newtheorem{theo}[defi]{Theorem}
\newtheorem{cor}[defi]{Corollary}
\newtheorem{lem}{Lemma}[section]
\newtheorem{prop}[defi]{Proposition}
\newtheorem{exam}{Example}
\newtheorem{rema}{Remark}[section]
\newtheorem{construction}{Construction}[section]
\newtheorem{prob}{Problem}[section]
\newtheorem{ques}{Question}
\newtheorem{exe}{Exercise}
\newcommand{\Np}{\mathbb{N^*}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\pr}{\textnormal{P}}
\newcommand{\E}{\textnormal{E}}
\renewcommand{\baselinestretch}{1.3}
\newcommand{\parallelsum}{\mathbin{\!/\mkern-5mu/\!}}
\newcommand{\B}{\hfill$\qedsymbol$}
\usepackage{centernot}
\usepackage{indentfirst}
\usepackage{setspace}
\usepackage[shortlabels]{enumitem}
%\setlist{leftmargin=5mm}
%Tạo frame
\usepackage[most]{tcolorbox}
%\renewcommand\familydefault{\sfdefault}
%san - mathversion
%\DeclareMathVersion{sans}
%\SetSymbolFont{operators}{sans}{OT1}{cmbr}{m}{n}
%\SetSymbolFont{letters}{sans}{OML}{cmbrm}{m}{it}
%\SetSymbolFont{symbols}{sans}{OMS}{cmbrs}{m}{n}
%\SetMathAlphabet{\mathit}{sans}{OT1}{cmbr}{m}{sl}
%\SetMathAlphabet{\mathbf}{sans}{OT1}{cmbr}{bx}{n}
%\SetMathAlphabet{\mathtt}{sans}{OT1}{cmtl}{m}{n}
%\SetSymbolFont{largesymbols}{sans}{OMX}{iwona}{m}{n}
%\sffamily\mathversion{sans}
\newtcolorbox[auto counter]{problem}[1][]{%
enhanced,
breakable,
colback=white,
colbacktitle=white,
coltitle=black,
fonttitle=\bfseries,
boxrule=1.25pt,
titlerule=.2pt,
toptitle=3pt,
bottomtitle=3pt,
title=Problem~\thetcbcounter,
#1}
\newtcolorbox[auto counter]{activity}[1][]{%
enhanced,
breakable,
colback=white,
colbacktitle=white,
coltitle=black,
fonttitle=\bfseries,
boxrule=1.25pt,
titlerule=.2pt,
toptitle=3pt,
bottomtitle=3pt,
title=Activity~\thetcbcounter,
#1}
\usepackage{fancyhdr,lipsum}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{fancy}
\fancypagestyle{plain}{%
\renewcommand{\headrulewidth}{0pt}%
\fancyhf{}%
}
\renewcommand{\chaptermark}[1]{%
\markboth{\bfseries\chaptername
\ \thechapter.\ #1}{}}
\fancyhead[R]{\bf \thepage}
\fancyhead[L]{\leftmark}
\fancyfoot{}
\usepackage{titlesec}
\titleformat{\chapter}[display]
{\bfseries\huge}
{\filright\huge\chaptertitlename~\thechapter}
{0.5cm}
{\titlerule[2.5pt]\vspace{0.1cm}\filright}
[{\vspace{0.1cm}\titlerule[2.5pt]}]
\begin{document}
\chapter{chapter 1}
\newpage
\chapter{chapter 2}
\end{document}
Thank you guys very much!
答案1
您可以将彩色盒子作为fancyhdr
设置的一部分。
\documentclass{book}
\usepackage{blindtext}
\usepackage[dvipsnames]{xcolor}
\usepackage{fancyhdr}
\colorlet{headpagecolour}{RoyalBlue}
\colorlet{headmarkcolour}{headpagecolour!20}
\newcommand*{\headpage}[2]{%
\setlength{\fboxsep}{\headboxsep}%
\colorbox{headpagecolour}{%
\makebox[\headpageboxwidth][#1]{%
\strut\textcolor{white}{\textbf{#2}}}}}
\newcommand*{\headmark}[2]{%
\setlength{\fboxsep}{\headboxsep}%
\colorbox{headmarkcolour}{%
\makebox[\headmarkboxwidth][#1]{%
\strut\nouppercase{\textbf{#2}}}}}
\newlength{\headboxsep}
\newlength{\headpageboxwidth}
\newlength{\headmarkboxwidth}
\setlength{\headboxsep}{3mm}
\setlength{\headwidth}{\dimexpr\paperwidth-\oddsidemargin-1in}
\setlength{\headheight}{\dimexpr\ht\strutbox+\dp\strutbox+2\headboxsep}
\setlength{\headpageboxwidth}{1.5cm}
\setlength{\headmarkboxwidth}{\dimexpr\headwidth-\headpageboxwidth-4\headboxsep}
\fancyhf{}
\fancyhead[LO]{\headmark{l}{\leftmark}}
\fancyhead[RO]{\headpage{r}{\thepage\hspace{1em}}}
\makeatletter
\if@twoside
\fancyhead[LE]{\headpage{l}{\hspace{1em}\thepage}}
\fancyhead[RE]{\headmark{r}{\leftmark}}
\fi
\makeatother
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\begin{document}
\blinddocument
\end{document}