4

I want exact this section style but I am unable to add frame around it. Could you please help me? Here is my code snippet :

\documentclass[10pt,a4paper,twoside]{report}
\usepackage{blindtext}
\usepackage{titlesec, blindtext, color}
\definecolor{seccolor}{RGB}{41,48,57}
\newcommand{\hsp}{\hspace{8pt}}
\titleformat{\section}[hang]{\Large\bfseries}{\thesection\hsp\textcolor{seccolor}{|}\hsp}{0pt}{\Large\bfseries}

\begin{document}
    \section{Section Name}
    \blindtext
\end{document}

enter image description here

5

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

  • 1
    Nice answer. Maybe the relative positioning of TikZ nodes should use anchors on the baseline, and \strut to ensure the top (resp. bottom) lines from section number and title nodes are at the same height, rather than north west and a hardcoded minimum height... – frougon Apr 3 at 10:23
  • 1
    @frougon Thanks for the suggestion. Actually I use this way because I am not so familiar with anchor. Maybe I will consider it when I have much free time. – user156344 Apr 3 at 10:32
  • 3
    @JouleV Okay, I've added a more elaborate example in my answer that uses anchors and struts the way I was suggesting in my previous comment. TikZ is awesome, enjoy! :) – frougon Apr 3 at 14:25
  • 1
    @frougon Great! – user156344 Apr 3 at 14:40
4

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

4

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

  • Thanks, I really appreciate it. It will also help someone. – Ravi Apr 3 at 14:28
  • 1
    @Ravi You're welcome. The hardest part was to take into account the line width so that the box can be of length \linewidth if the title is long enough, but no more (otherwise overfull \hbox). Of course, one could use \hss or something like that around the tikzpicture to hide small overflows, but it's cleaner when properly computed. Also slightly tricky was to draw the fill color behind the title text, because the node to be filled has its dimensions that depend on the sec num and title nodes. But I found a way. :) You can even choose a diff. color for the vert. line and get a nice result. – frougon Apr 3 at 14:40

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.