彩色章节标题背景(纸张宽度)

彩色章节标题背景(纸张宽度)

我想要给章节标题的背景添加类似这张图片的颜色。

在此处输入图片描述

颜色是:

在此处输入图片描述

MWE(xelatex):

\documentclass{book}

\usepackage{titlesec}
\usepackage{tikz} % or maybe with tcolorbox package

\usepackage{xepersian} % Last package
\settextfont{Amiri} % This font has been installed with TeX Live

\titleformat{\chapter}[display]
    {\normalfont\fontsize{16}{19.2}\selectfont\bfseries}%
    {\filleft\chaptertitlename\ \tartibi{chapter}}{1ex}%
    {\filright\fontsize{16}{19.2}\selectfont}
\titlespacing*{\chapter}{0pt}{50pt}{2\baselineskip}

\begin{document}

    \chapter{آزمایش}

\end{document}

MWE 的输出:

在此处输入图片描述

答案1

像这样?

(第三版)

%XeTeX
\documentclass[draft]{book}
\usepackage[showframe]{geometry}
\usepackage{changepage}
\usepackage{xcolor}
% \definecolor{fgcol}{rgb}{51,51,255}
\definecolor{fgcol}{HTML}{3333ff}
%\definecolor{fgcol}{rgb}{0,0,1}
%\usepackage{titlesec}
\usepackage[explicit]{titlesec}
%\usepackage{tikz} % or maybe with tcolorbox package
\usepackage{xepersian} % Last package
\settextfont{Amiri} % This font has been installed with TeX Live

% https://tex.stackexchange.com/a/18578/112708
    \newlength{\myl}
    \setlength{\myl}{\textwidth}
    \addtolength{\myl}{-\marginparsep}
    % \the\myl
    \makeatletter
    \newlength{\lmargin}
    \setlength{\lmargin}{\dimexpr\Gm@lmargin}
    \newlength{\rmargin}
    \setlength{\rmargin}{\dimexpr\Gm@rmargin}
    \newlength{\loffset}
    \setlength{\loffset}{\lmargin}
    \newlength{\roffset}
    \setlength{\roffset}{\rmargin}
    \makeatletter

\titleformat{\chapter}[display]
    {\normalfont\fontsize{16}{19.2}\selectfont\bfseries}%
    {\filleft%
      \hspace{\loffset}
      \chaptertitlename\ \tartibi{chapter}}{1ex}%
    {\begin{adjustwidth}{-\lmargin}{-\rmargin}
        \colorbox{fgcol}{\begin{minipage}{\paperwidth}
          %{\myl} % only across body
            \vspace{2em}
          \color{white}
          \filright\fontsize{16}{19.2}\selectfont #1
          \hspace{\roffset}
            \vspace{2em}
        \end{minipage}}\end{adjustwidth}}
        
\titlespacing*{\chapter}{0pt}{50pt}{2\baselineskip}

\begin{document}
%\newgeometry{margin=0in}
\chapter{آزمایش}
%\restoregeometry

\end{document}

在此处输入图片描述

相关内容