TransWikia.com

Chapter title in a colored box (custom chapter command)

TeX - LaTeX Asked by Gregg on August 14, 2021

I try to put my document’s title in a colored box. I looked for it on google and on this forum but I didn’t find anything I like (titleformat, …). I’ve already renew the chapter command (in my .sty file) and I would like to find a way to put the colored box in this renewed command… Unfortunately, I have a lot to learn yet and to understand in Latex 🙁

% Chapter Title redefinition
renewcommand{chapter}{if@openrightcleardoublepageelseclearpagefi%
                    thispagestyle{plain}%
                    @startsection{chapter}{0}{z@}%
                    {-3.5ex @plus -1ex @minus -.2ex}%
                    {2.3ex @plus.2ex}%
                    {noindentfontsize{18}{20}fontspec{Arial}selectfontcolor{my_custom_color}}}

[EDIT]
For now, I’ve that :
enter image description here

And I would like to have that
enter image description here

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Package definition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
ProvidesPackage{ffgTemplate}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Required Package
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
RequirePackage[a4paper, top=2cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}  % Gestion des marges
RequirePackage[french]{babel}                                  % Gestion du français
RequirePackage{graphicx}                                       % Gestion des images
RequirePackage{xcolor}                                         % Gestion des couleurs
RequirePackage{hyperref}                                       % Gestion des hyperliens
RequirePackage{fancyhdr}                                       % Gestion des en-têtes et pieds de pages.
RequirePackage{pifont}                                         % Gestion des symbols (ding{55} dans le footer)
RequirePackage{array}                                          % Gestion (améliorée) des tableaux
RequirePackage{ulem}                                           % Gestion (avancée) des soulignements
RequirePackage{textcomp}                                       % Caractères additionnels
RequirePackage{mathtools, amsmath, amstext, amssymb, amsthm}   % Package de l'AMS + mathtools
RequirePackage{gensymb}                                        % symbole degree
RequirePackage{cancel}
RequirePackage{nicefrac}                                       % Gestion des fractions
RequirePackage{xspace}                                         % Gestion intelligente de l'espace après une commande
RequirePackage{lmodern}                                        % Police de caractère
RequirePackage{calc}                                           % Syntaxe naturelle pour les calculs
RequirePackage{framed}
RequirePackage{tcolorbox}                                      % Pour créer des boites colorées.

% Layout fisrt page
RequirePackage{eso-pic}                                % Gestion du background de la page de garde.
RequirePackage[absolute]{textpos}                      % Gestion des positions du texte (option de config : showboxes)
RequirePackage{fontspec}                               % Gestion de "arial" ET "arial narrow" comme font.
RequirePackage{anyfontsize}                            % Gestion des tailles de police hors dictionnaire
RequirePackage{shadowtext}                             % Gestion des ombres
% RequirePackage{UniversalSans}

% Layout Document 
RequirePackage{titlesec}                               % Gestion des sections
RequirePackage{titletoc}                               % Gestion de la table des matière
RequirePackage{titling}                                % Gestion des titres
RequirePackage{enumitem}                               % Gestion des listes

% Tools kit
RequirePackage{fancybox}                               % pour dessiner des boites ;-)
RequirePackage{etoolbox}                               % Renew command & robust command
RequirePackage{float}

% other package
RequirePackage{bm}                                     % Notation mathématique en gras
RequirePackage{lscape}                                 % Gestion du mode "paysage"

frenchbsetup{StandardItemLabels=true}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Define global variable for document
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
deffederationName{F.f.G.}
deffederationAcronym{FfG}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% (Re)Define parametres for document
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
setmainfont{Arial}
addtocaptionsfrench{deftablename{Tableau}}                  % Définition du mot pour les tableaux
addtocaptionsfrench{deffigurename{Figure}}                  % Définition du mot pour les Figures

% Color definition
definecolor{ffg_darkblue}{RGB}{21,29,76}
definecolor{ffg_lightblue}{RGB}{0, 120, 190}
definecolor{ffg_red}{RGB}{241, 83, 60}
definecolor{ffg_title_color}{RGB}{12, 97, 158}
definecolor{footer_color}{RGB}{128, 128, 128}
definecolor{flyingblue}{RGB}{49, 123, 181}
definecolor{greymoreknowledge}{RGB}{200, 200, 200}

% Parametres for TextPos
setlength{TPHorizModule}{1mm}
textblockorigin{5mm}{5mm}                                      % start everything near the top-left corner
setlength{parindent}{0pt}                                     % Pas d'indentation au placement du texte

setlengthfboxrule{0.75pt}
newcommand{mybox}[1]{fbox{$~~~ displaystyle#1 ~~~$}}

newcommandNextYear{%
   advanceyear by 1 theyearadvanceyear by -1}
    

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Image's first page definition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
newcommandBackgroundPic{%
    put(0,0){%
        parbox[b][paperheight]{paperwidth}{%
            vfill
            centering
            includegraphics[scale=1]{firstpage.png}%
            vfill
        }
    }
}

shadowoffset{3pt}
shadowrgb{0.8, 0.8, 0.8}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% First Page's definition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
defmaketitle{%
    null
    thispagestyle{empty}%

    % Titre du document
    begin{textblock}{200}(0,3)
        centering
        {fontsize{33}{42}selectfont {bfseries textcolor{ffg_darkblue}{FORMATION~formationType~discipline}par}}
        {fontsize{33}{42}selectfont {bfseries textcolor{ffg_red}{Module :moduleTitle}par}}
    end{textblock}

    % Type de document
    begin{textblock}{200}(0,8)
        begin{center}
            {fontsize{28}{42}selectfont textcolor{ffg_lightblue}{Formation des Cadres}par}%
        end{center}
    end{textblock}

    % Années
    begin{textblock}{200}(0,11.5)
        centering
        begin{center}
            {fontsize{30}{84}selectfont textcolor{white}{{theyear}hspace{3cm} {NextYear}}par}%
        end{center}
    end{textblock}

    null
    cleardoublepage
}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Other page definition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% setlength{headheight}{30pt}
fancypagestyle{followingpage}{%
    fancyhf{}

    %% Footer
    fancyfootoffset{3cm}
    fancyfoot[C]{hspace{2.5cm} {fontsize{9}{10}selectfont textit{textcolor{footer_color}{Formation des Cadres~discipline hfill p.thepage hfill Edition theyear~-~NextYear}}} hspace{2.5cm}}
}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Titles layout redefinition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
makeatletter

% Chapter Title redefinition
renewcommand{chapter}{if@openrightcleardoublepageelseclearpagefi%
                    thispagestyle{plain}%
                    @startsection{chapter}{0}{z@}%
                    {-3.5ex @plus -1ex @minus -.2ex}%
                    {2.3ex @plus.2ex}%
                    {noindentfontsize{18}{20}fontspec{Arial}selectfontcolor{ffg_title_color}}}

% titleformat{chapter}{}{}{0em}{colorbox{blue}{parbox{dimexprtextwidth-2fboxseprelax}{textcolor{white}{thesectionquad#1}}}}
% titleformat{name=section,numberless}
% {normalfontLargebfseries}{}{0em}{colorbox{blue}{parbox{dimexprtextwidth-2fboxseprelax}{textcolor{white}{#1}}}}

% Section Title redefinition
renewcommand{section}{@startsection{section}{1}{z@}%
                        {-3.5ex @plus -1ex @minus -.2ex}%
                        {2.3ex @plus.2ex}%
                        {noindentfontsize{14}{12}fontspec{Arial}selectfontcolor{ffg_lightblue}}}


% SubSection Title redefinition
renewcommand{subsection}{@startsection{subsection}{2}{z@}%
                        {-3.25ex@plus -1ex @minus -.2ex}%
                        {1.5ex @plus .2ex}%
                        {noindentfontsize{13}{20}fontspec{Arial}selectfontcolor{ffg_title_color}}}

makeatother

pagestyle{followingpage}
AtBeginDocument{
    setcounter{secnumdepth}{3}
    setcounter{tocdepth}{3}
    AddToShipoutPicture*{BackgroundPic}
}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Redefine Theoreme & Definition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
newtheoremstyle{new_theorem_style}
{topsep}       % space before
{topsep}       % space after
{
    itshape
}               % body font
{}              % indent
{bfseries}     % header font
{}              % punctuation
{newline}      % after theorem header
{
    thmname{#1}~thmnumber{#2}thmnote{ - #3}*[-1.5ex]
}% header specification (empty for default)

theoremstyle{new_theorem_style}
newtheorem*{definitionT}{color{ffg_title_color}{Définition}}
newenvironment{definition}
{
    begin{leftbar}
        begin{definitionT}
            mbox{}noindentignorespaces}{
        end{definitionT}
    end{leftbar}
}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Define colorbox
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
newtcolorbox{knowledgebox}{colback=ffg_red!5!white, colframe=ffg_red!100!black, fonttitle=bfseries, title={Savoir}, breakable} %breakable
newtcolorbox{skillsbox}{colback=flyingblue!5!white, colframe=flyingblue!100!black, fonttitle=bfseries, title={Savoir faire}, breakable} %breakable
newtcolorbox{morebox}{colback=greymoreknowledge!5!white, colframe=greymoreknowledge!100!black, fonttitle=bfseries, title={Allez plus loinldots}, breakable}
newtcolorbox{dangerbox}[1]{colback=red!5!white, colframe=red!100!black, fonttitle=bfseries, title={Important}, breakable} %breakable

Thank you in advance,

2 Answers

Here is a way to go, with titlesec and tabularx:

documentclass{book} 
usepackage[T1]{fontenc}
usepackage{xcolor}
usepackage{tabularx} 
usepackage{lipsum}

usepackage[explicit]{titlesec}%
newcommand{bgparbox}[1]{colorbox{blue}{setlength{fboxsep}{6pt}%

begin{tabularx}{dimexprlinewidth-fboxsep}{l>{raggedrightarraybackslash}X}#1end{tabularx}}}
titleformat{chapter}
 {color{white}sffamilybfserieshuge}
 {}
 {0em}
 {bgparbox{thechapter & #1}}%

begin{document}

chapter{Introduction. break A detailed introduction}

lipsum[1] 

end{document} 

enter code here

Correct answer by Bernard on August 14, 2021

Thank you so much Bernard. Here's my partial solution :

definecolor{ffg_title_color}{RGB}{12, 97, 158}
colorlet{shadecolor}{ffg_title_color}
titleformat{chapter}[display]
    {fontsize{16pt}{12pt}selectfontcolor{white}begin{shaded*}}
    {thechapter qquad #1}
    {0pt}
    {end{shaded*}}
titlespacing{chapter}{0cm}{0cm}{0cm}

enter image description here Why "partial" solution ? Because it's not perfect : there is to much "space" under the title. As you can see, the vertical space above the title (in the colored box) is not the same (smaller) than the space under the title (always in the colored box). I try yet to solve this problem (make the under part smaller) but all the other parts are perfect what I want.

Answered by Gregg on August 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP