How to add frame around section using titlesec?

A solution with a simple tabulary:

\documentclass[10pt, a4paper,svgnames, twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[showframe]{geometry}
\usepackage{blindtext}
\usepackage{array, tabulary}
\usepackage[explicit]{titlesec}
\usepackage{blindtext, xcolor}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}[block]{\Large\bfseries\sffamily\setlength{\fboxrule}{1pt}\color{SlateGrey}}{}{0pt}{\fbox{\begin{tabulary}{\dimexpr\linewidth-\tabcolsep-\fboxrule}{@{}l!{\vline width 1.2pt}L}\thesection &#1 \end{tabulary}} }

\begin{document}

\setcounter{chapter}{2}
    \section{Section Title. Some more more text to have a really very very long section title.}
    \blindtext
    \section{A much shorter section title}
\blindtext

\end{document} 

enter image description here


You can have it with TikZ and explicit option of titlesec:

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
    [hang]
    {\Large\bfseries}
    {}
    {0pt}
    {\tikz\node[draw]{\Large\bfseries\thesection\hsp\textcolor{seccolor}{|}\hsp#1};}
\begin{document}
\section{Section Name}
\blindtext
\end{document}

enter image description here


With very long section titles (with help from this question):

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{calc}
\tikzset{
    max width/.style args={#1}{
        execute at begin node={\begin{varwidth}{#1}},
        execute at end node={\end{varwidth}}
    }
}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
    [hang]
    {\Large\bfseries}
    {}
    {0pt}
    {\tikz\node[draw,max width=\textwidth-.6666em-.4pt]{\Large\bfseries\thesection\hsp\textcolor{seccolor}{|}\hsp#1};}
\begin{document}
\section{Section Name}
\blindtext
\section{This is a very long section name that has to be broken into two lines}
\end{document}

enter image description here


If you want to have "|" instead of "—", as in the attached figure, you can use \textbar:

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{calc}
\tikzset{
    max width/.style args={#1}{
        execute at begin node={\begin{varwidth}{#1}},
        execute at end node={\end{varwidth}}
    }
}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
    [hang]
    {\Large\bfseries}
    {}
    {0pt}
    {\tikz\node[draw,max width=\textwidth-.6666em-.4pt]{\Large\bfseries\thesection\hsp\textcolor{seccolor}{\textbar}\hsp#1};}
\begin{document}
\section{Section Name}
\blindtext
\end{document}

enter image description here


However, a vertical line like this is much better I think.

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage[explicit]{titlesec}
\usepackage{color}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{calc}
\usetikzlibrary{positioning}
\tikzset{
    max width/.style args={#1}{
        execute at begin node={\begin{varwidth}{#1}},
        execute at end node={\end{varwidth}}
    }
}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}
    [hang]
    {\Large\bfseries}
    {}
    {0pt}
    {\tikz[font=\Large\bfseries]{\node[draw,minimum height=.75cm] (x) {\thesection};\node[minimum height=.75cm,below right=0pt and 0pt of x.north west,draw,max width=\textwidth-.6666em-.4pt]{\hspace{.3333em}\phantom{\thesection}\hspace{.3333em}#1};}}
\begin{document}
\section{Section Name}
\blindtext
\section{This is a very long section name that has to be broken into two lines}
\end{document}

enter image description here


This post contains two solutions: a simple one and a more elaborate one.

Simple and crude solution based on \fbox

Let's start with a simple solution based on \fbox, that is easy to understand. We use \textbar for the vertical bar and provide a variant for unnumbered sections (\section*):

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{blindtext}

\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}

\newcommand*{\sectionFont}{%
  \Large\bfseries
}

% For \section
\titleformat{\section}[block]{\sectionFont}{}{0pt}{%
  \fbox{\thesection \hsp \textcolor{seccolor}{\textbar}\hsp #1}}

% For \section*
\titleformat{name=\section, numberless}[block]{\sectionFont}{}{0pt}{%
  \fbox{#1}}

\begin{document}
\chapter{Some chapter}

\section{Section title}

Foo bar.

\section*{Unnumbered section}

\blindtext
\end{document}

enter image description here

More elaborate solution using TikZ

If you want finer control than what \fbox allows (\fboxsep and \fboxrule), using a tikzpicture environment for the frame is a good alternative. Here is an example of what one can do this way. It implements a similar design as in JouleV's solution but tries to do a cleaner positioning in order to ensure that the baseline of the section number is always aligned with the baseline of the first line of the section tile. Since we have all the power of TikZ at hand, we also add a few bells and whistles that are just a few keywords away: rounded corners, drop shadow and a background fill of the title box.

(With a bit more hacking, it should even be possible to detect when the section title takes several lines, and only in this case use vertical centering for the section number!)

\documentclass[a4paper]{report}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{etoolbox}
\usepackage{varwidth}
\usepackage{calc}
\usepackage{blindtext}

\usepackage{tikz}
% Uncomment the following line if you use babel and have a recent enough TikZ:
% \usetikzlibrary{babel}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{shadows}

% Essentially taken from <https://tex.stackexchange.com/a/482926/73317>
\tikzset{
    max width/.style args={#1}{
        execute at begin node={\begin{varwidth}[t]{#1}},
        execute at end node={\end{varwidth}}
    }
}

% Colors used for decorated section titles
\definecolor{sectionDecoRuleColor}{RGB}{70,10,10}
\colorlet{sectionDecoBgColor}{yellow!10}

% Horizontal spacing used for decorated section titles
\newlength{\sectionDecoHsp}
\setlength{\sectionDecoHsp}{8pt}

\newcommand*{\sectionDecoRuleWidth}{0.4pt}

% Boolean flag indicating whether we are working with a \section or a \section*
\newtoggle{sectionDecoIsNumberedSec}

\makeatletter

% High-level command for section decorations. Two variants: one for \section
% and one for \section*.
\newcommand*{\sectionDecoration}{%
  \@ifstar{%
    \togglefalse{sectionDecoIsNumberedSec}%
    \@sectionDecoration\@nil    % \@nil passed instead of the section number
  }{%
    \toggletrue{sectionDecoIsNumberedSec}%
    \@sectionDecoration
  }%
}

% #1: the section number (\@nil if we are working for a \section*)
% #2: the section title
\newcommand*{\@sectionDecoration}[2]{%
  \begin{tikzpicture}
    % The section number
    \iftoggle{sectionDecoIsNumberedSec}{% case of \section
      \node[inner xsep=\sectionDecoHsp, inner ysep=0, outer sep=0,
            anchor=base west]
        (section number) at (0,0)
        {\strut #1};
    }{% case of \section*
      \node[inner sep=0, outer sep=0, anchor=base west]
        (section number) at (0,0)
        {\strut};
    }

    % The section title (which may occupy several lines)
    \path let \p1 = ($(section number.east)-(section number.west)$),
              \n1 = {\linewidth - veclen(\p1) - 2\sectionDecoHsp} in
      node[inner xsep=\sectionDecoHsp, inner ysep=0, outer sep=0,
           anchor=base west, max width=\n1]
        (section title) at (section number.base east)
        {\strut #2%   Useful for testing: \rule{\n1}{1pt}%
         \strut};

    \begin{scope}[on background layer]
      % The frame around {section number + title}
      \node[inner sep=0, outer sep=0, draw, line width=\sectionDecoRuleWidth,
            rounded corners,
            fit={([xshift=0.5\pgflinewidth]section number.north west)
                 ([xshift=-0.5\pgflinewidth]section title.south east)},
            color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
        (frame) {};

      % The vertical line between section number and section title
      \iftoggle{sectionDecoIsNumberedSec}{% case of \section
        \draw[color=sectionDecoRuleColor, line width=\sectionDecoRuleWidth,
              line cap=butt]
          ([yshift=-0.5\pgflinewidth]section title.north west) --
          ([yshift=0.5\pgflinewidth]section title.south west);
      }{% no such line in the case of \section*
      }
    \end{scope}

  % Display key points (only useful for debugging)
  % \begin{scope}[overlay]
  %   \path[radius=1pt, fill=red] (section number.north west) circle {}
  %                               (section number.north east) circle {};
  %   \node[draw,circle,green, inner sep=1.6pt] at (section title.north west) {};
  % \end{scope}
  \end{tikzpicture}%
}

\makeatother

\newcommand*{\sectionTitleFont}{\Large\bfseries}

% For \section
\titleformat{\section}[block]{\sectionTitleFont}{}{0pt}{%
  \sectionDecoration{\thesection}{#1}}

% For \section*
\titleformat{name=\section, numberless}[block]{\sectionTitleFont}{}{0pt}{%
  \sectionDecoration*{#1}}

\begin{document}
\chapter{Some chapter}

\section{A short section title}

\section{A very very very very very very very very very very
            very very very very very very very very very very
            long section title}

Foo bar.

\section*{An unnumbered section}

\blindtext
\end{document}

enter image description here