彩色框中的章节标题(自定义章节命令)

彩色框中的章节标题(自定义章节命令)

我尝试将文档的标题放在彩色框中。我在谷歌和论坛上搜索过,但没有找到我喜欢的东西(\titleformat,……)。我已经更新了章节命令(在我的 .sty 文件中),我想找到一种方法将彩色框放在这个更新的命令中……不幸的是,我还有很多东西要学,要理解 Latex :-(

% Chapter Title redefinition
\renewcommand{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi%
                    \thispagestyle{plain}%
                    \@startsection{chapter}{0}{\z@}%
                    {-3.5ex \@plus -1ex \@minus -.2ex}%
                    {2.3ex \@plus.2ex}%
                    {\noindent\fontsize{18}{20}\fontspec{Arial}\selectfont\color{my_custom_color}}}

[编辑] 就目前而言,我已经: 在此处输入图片描述

我希望 在此处输入图片描述

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% 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
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
\def\federationName{F.f.G.}
\def\federationAcronym{FfG}


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% (Re)Define parametres for document
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
\setmainfont{Arial}
\addto\captionsfrench{\def\tablename{Tableau}}                  % Définition du mot pour les tableaux
\addto\captionsfrench{\def\figurename{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

\setlength\fboxrule{0.75pt}
\newcommand{\mybox}[1]{\fbox{$~~~ \displaystyle#1 ~~~$}}

\newcommand\NextYear{%
   \advance\year by 1 \the\year\advance\year by -1}
    

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
% Image's first page definition
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
\newcommand\BackgroundPic{%
    \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
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
\def\maketitle{%
    \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}{{\the\year}\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 \the\year~-~\NextYear}}} \hspace{2.5cm}}
}


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

% Chapter Title redefinition
\renewcommand{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi%
                    \thispagestyle{plain}%
                    \@startsection{chapter}{0}{\z@}%
                    {-3.5ex \@plus -1ex \@minus -.2ex}%
                    {2.3ex \@plus.2ex}%
                    {\noindent\fontsize{18}{20}\fontspec{Arial}\selectfont\color{ffg_title_color}}}

% \titleformat{\chapter}{}{}{0em}{\colorbox{blue}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\textcolor{white}{\thesection\quad#1}}}}
% \titleformat{name=\section,numberless}
% {\normalfont\Large\bfseries}{}{0em}{\colorbox{blue}{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\textcolor{white}{#1}}}}

% Section Title redefinition
\renewcommand{\section}{\@startsection{section}{1}{\z@}%
                        {-3.5ex \@plus -1ex \@minus -.2ex}%
                        {2.3ex \@plus.2ex}%
                        {\noindent\fontsize{14}{12}\fontspec{Arial}\selectfont\color{ffg_lightblue}}}


% SubSection Title redefinition
\renewcommand{\subsection}{\@startsection{subsection}{2}{\z@}%
                        {-3.25ex\@plus -1ex \@minus -.2ex}%
                        {1.5ex \@plus .2ex}%
                        {\noindent\fontsize{13}{20}\fontspec{Arial}\selectfont\color{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{}\noindent\ignorespaces}{
        \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 loin\ldots}, breakable}
\newtcolorbox{dangerbox}[1]{colback=red!5!white, colframe=red!100!black, fonttitle=\bfseries, title={Important}, breakable} %breakable

先感谢您,

答案1

这是一种使用titlesec和的方法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}{\dimexpr\linewidth-\fboxsep}{l>{\raggedright\arraybackslash}X}#1\end{tabularx}}}
\titleformat{\chapter}
 {\color{white}\sffamily\bfseries\huge}
 {}
 {0em}
 {\bgparbox{\thechapter & #1}}%

\begin{document}

\chapter{Introduction. \break A detailed introduction}

\lipsum[1] 

\end{document} 

在此处输入代码

答案2

非常感谢 Bernard。以下是我的部分解决方案:

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

在此处输入图片描述 为什么是“部分”解决方案?因为它并不完美:标题下方有太多“空间”。如您所见,标题上方的垂直空间(在彩色框中)与标题下方的空间(始终在彩色框中)并不相同(更小)。我尝试解决这个问题(使下方部分变小),但其他所有部分都是我想要的完美。

相关内容