2

How can I put section title number in a box and I want the numbers to be shifted like this picture :

enter image description here

This is my code.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\makeatletter
\renewcommand{\@seccntformat}[1]{\llap{\textcolor{red}{\csname the#1\endcsname}\hspace{0.4em}}}                    
\titleformat{\section}[block]
{\hspace{-8.9mm}\normalfont\fontsize{14pt}{16pt}\bfseries\color{blue}}
{\color{red}\thesection}
{2mm}{#1}
{}
\titlespacing{\section}{0pt}{2.8ex}{2ex}
\renewcommand{\subsection}{\@startsection {subsection}{2}{\z@}
    {-15pt \@plus -0.1ex \@minus -.4ex}
    {2ex \@plus.2ex }
    {\normalfont\large\rmfamily\bfseries\color{blue}}}
\renewcommand{\subsubsection}{\@startsection {subsubsection}{3}{\z@}
    {-12pt \@plus -0.1ex \@minus -.2ex}
    {2ex \@plus.2ex }
    {\normalfont\normalsize\rmfamily\bfseries\color{blue}}}                        
\begin{document}
\section{Section one}
\lipsum[3]
\subsection{Subsection one}
\lipsum[5]
\end{document}
2
  • What you want is not very clear? Is it the section numbers in the left margin, or also the subsection and subsubsection numbers?
    – Bernard
    Apr 25 '20 at 18:18
  • all, the section and subsection and subsubsection numbers.
    – moumou85
    Apr 26 '20 at 9:10
3

It all can be done with titlesec. Note I used the simplified version of the parameters in \titlespacing* (with a * to remove the indent of the first paragraph after the sections titles).

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\titlespacing*{\section}{-2mm}{*2.8}{*2}

\titleformat{\section}
{\normalfont\fontsize{14pt}{16pt}\bfseries}
{\llap{\colorbox{DarkSeaGreen}{\color{red}\,\thesection\,}}}
{2mm}{\color{blue}#1}

\titlespacing*{\subsection}{-2mm}{*2.5}{*2}
\titleformat{\subsection}
{\normalfont\large\bfseries}
{\llap{\colorbox{DarkSeaGreen}{\color{red}\thesubsection}}}
{2mm}{\color{blue}#1}

\titlespacing*{\subsubsection}{-2mm}{*2}{*2}
\titleformat{\subsubsection}
{\normalfont\normalsize\bfseries}
{\llap{\colorbox{DarkSeaGreen}{\color{red}\thesubsubsection}}}
{2mm}{\color{blue}#1}

\begin{document}

\section{Section one}
\lipsum[3]
\subsection{Subsection one}
\lipsum[5]

\subsubsection{Subsubsection one}
\lipsum[11]

\end{document} 

enter image description here

1
  • Thank you so much @Bernard.
    – moumou85
    Apr 26 '20 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.