Sectioning – How to Add Frame Box Around Section, Subsection, and Subsubsection Numbering

boxframednumberingsectioning

To make it seem like the following, I would like to add a frame box to the numbering of each section, subsection, and subsubsection.
enter image description here

but the following code doesn't seem to work for 'subsubsection'.

\documentclass[12pt,a4paper]{report}
\usepackage[french]{babel} 
\usepackage[a4paper,margin=0.5in]{geometry}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[Glenn]{fncychap}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\renewcommand\thesection{\fbox{\Roman{section}}}
\renewcommand\thesubsection{\fbox{\arabic{subsection}}}
\renewcommand\thesubsubsection{\fbox{\begin{english}\textit{\alph{subsubsection}}\end{english}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
    \chapter{}
    \section{Section}
    \subsection{Subsection 1}
    \subsection{Subsection 2}
    \subsubsection{\fbox{a} subSubsection}
    \subsubsection{\fbox{b} subSubsection}
    \section{section}
    \subsection{subsection}
    \subsubsection{\fbox{a} subsubsection}
\end{document}

Best Answer

Some remarks, in no particular order.

  • The report document class executes \setcounter{secnumdepth}{2}, i.e., it doesn't number subsubsection-level headers automatically. Run

    \setcounter{secnumdepth}{3} 
    

    to tell LaTeX to number subsubsection-level headers as well.

  • It's a really bad idea to incorporate the \fbox instruction in the (re)definition of \thesection, \thesubsection, and \thesubsubsection. Why? Because the frameboxes will show up in all cross-references to sections, subsections, and subsubsection -- almost certainly not what you want.

  • Instead, employ the low-level \@seccntformat macro -- see the code below for an application -- to tell LaTeX to draw frameboxes just in the headers.

  • Since you're using alphabetic letter characters for subsubsection level headers, you should take precautions to ensure that the exact same framebox is used regardless of whether the letter has neither ascender or descender (e.g., a and z), just an ascender (e.g., b), just a descender ('y' or 'j'), or both (italic-shape f). The sizing irregularity issue may be fixed by inserting a \vphantom{f} directive in the definition of \subsubsection@cntformat.

enter image description here

\documentclass[12pt,a4paper]{report}
\usepackage[french]{babel} 
\usepackage[margin=0.5in]{geometry}

\usepackage[Glenn]{fncychap}
\setcounter{secnumdepth}{3} % <-- new

\renewcommand\thesection{\Roman{section}}
\renewcommand\thesubsection{\arabic{subsection}}
\renewcommand\thesubsubsection{\alph{subsubsection}}

% Use method proposed in "The LaTeX Companion", 2nd ed., to determine
% how the section-like counters are displayed in sectioning headers
\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
    {\csname the#1\endcsname\space}%    default
    {\csname #1@cntformat\endcsname}}%  enable individual control
\newcommand\section@cntformat{%       section
   \fbox{\thesection}\quad}       
\newcommand\subsection@cntformat{%    subsection
   \fbox{\thesubsection}\quad} 
\newcommand\subsubsection@cntformat{% subsubsection
    \fbox{\itshape\thesubsubsection\vphantom{f}}\quad}
\makeatother

\begin{document}
\arabic{secnumdepth}
    \chapter{}
    \section{Section}
    \subsection{Subsection 1}
    \subsection{Subsection 2}
    \subsubsection{subSubsection}
    \subsubsection{subSubsection}
    \section{Section}
    \subsection{Subsection}
    \subsubsection{subSubsection} 
\end{document}