0

I want to color background of chapter title like this picture.

enter image description here

The color is:

enter image description here

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}

The output of MWE:

enter image description here

1 Answer 1

0

Like this?

(3rd version)

%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}

enter image description here

3
  • Thank you. Can you change the width of color box to paperwidth? and also a little shadow at above and below it? Mar 24, 2021 at 3:52
  • @BahmanMirzakhani. Yes. You're going to have to play with the l/r offsets to get the title and label where you want them to be.
    – Erwann
    Mar 24, 2021 at 3:57
  • @BahmanMirzakhani for the shadows, I recommend you post another question for that only (MWE).
    – Erwann
    Mar 24, 2021 at 4:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .