How to add frame around section using titlesec? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Can we define maximum width for a node?Section and subsection colors using titlesecCurrent section title macro using titlesecChapter titles using titlesecHow to create specific section style using titlesec?Managing spacing around section inside a minipage with titlesecCollapsing section ranges in titlesec page headersSection title disappears when using titlesec?Titlesec section headings right-centeredHow to fill section on titlesecGet back standard title format with titlesec

Asymptotics question

Tannaka duality for semisimple groups

How many time has Arya actually used Needle?

Universal covering space of the real projective line?

Connecting Mac Book Pro 2017 to 2 Projectors via USB C

What does this say in Elvish?

Putting class ranking in CV, but against dept guidelines

AppleTVs create a chatty alternate WiFi network

One-one communication

Select every other edge (they share a common vertex)

New Order #6: Easter Egg

Google .dev domain strangely redirects to https

The test team as an enemy of development? And how can this be avoided?

What adaptations would allow standard fantasy dwarves to survive in the desert?

Ore hitori de wa kesshite miru koto no deki nai keshiki; It's a view I could never see on my own

Found this skink in my tomato plant bucket. Is he trapped? Or could he leave if he wanted?

How would a mousetrap for use in space work?

Project Euler #1 in C++

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

Printing attributes of selection in ArcPy?

How to force a browser when connecting to a specific domain to be https only using only the client machine?

How does the math work when buying airline miles?

Why is a lens darker than other ones when applying the same settings?



How to add frame around section using titlesec?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Can we define maximum width for a node?Section and subsection colors using titlesecCurrent section title macro using titlesecChapter titles using titlesecHow to create specific section style using titlesec?Managing spacing around section inside a minipage with titlesecCollapsing section ranges in titlesec page headersSection title disappears when using titlesec?Titlesec section headings right-centeredHow to fill section on titlesecGet back standard title format with titlesec










5















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
usepackageblindtext
usepackagetitlesec, blindtext, color
definecolorseccolorRGB41,48,57
newcommandhsphspace8pt
titleformatsection[hang]Largebfseriesthesectionhsptextcolorseccolorhsp0ptLargebfseries

begindocument
sectionSection Name
blindtext
enddocument


enter image description here










share|improve this question




























    5















    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
    usepackageblindtext
    usepackagetitlesec, blindtext, color
    definecolorseccolorRGB41,48,57
    newcommandhsphspace8pt
    titleformatsection[hang]Largebfseriesthesectionhsptextcolorseccolorhsp0ptLargebfseries

    begindocument
    sectionSection Name
    blindtext
    enddocument


    enter image description here










    share|improve this question


























      5












      5








      5


      1






      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
      usepackageblindtext
      usepackagetitlesec, blindtext, color
      definecolorseccolorRGB41,48,57
      newcommandhsphspace8pt
      titleformatsection[hang]Largebfseriesthesectionhsptextcolorseccolorhsp0ptLargebfseries

      begindocument
      sectionSection Name
      blindtext
      enddocument


      enter image description here










      share|improve this question
















      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
      usepackageblindtext
      usepackagetitlesec, blindtext, color
      definecolorseccolorRGB41,48,57
      newcommandhsphspace8pt
      titleformatsection[hang]Largebfseriesthesectionhsptextcolorseccolorhsp0ptLargebfseries

      begindocument
      sectionSection Name
      blindtext
      enddocument


      enter image description here







      sectioning pdftex titlesec framed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 3 at 8:17







      Ravi

















      asked Apr 3 at 7:14









      RaviRavi

      33619




      33619




















          3 Answers
          3






          active

          oldest

          votes


















          6














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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          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
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolortextbarhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          usetikzlibrarypositioning
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikz[font=Largebfseries]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.3333emphantomthesectionhspace.3333em#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          enter image description here






          share|improve this answer




















          • 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.

            – JouleV
            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!

            – JouleV
            Apr 3 at 14:40


















          5














          A solution with a simple tabulary:



          documentclass[10pt, a4paper,svgnames, twoside]report
          usepackage[utf8]inputenc
          usepackage[T1]fontenc
          usepackage[showframe]geometry
          usepackageblindtext
          usepackagearray, tabulary
          usepackage[explicit]titlesec
          usepackageblindtext, xcolor
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection[block]Largebfseriessffamilysetlengthfboxrule1ptcolorSlateGrey0ptfboxbegintabularydimexprlinewidth-tabcolsep-fboxrule@l!vline width 1.2ptLthesection &#1 endtabulary

          begindocument

          setcounterchapter2
          sectionSection Title. Some more more text to have a really very very long section title.
          blindtext
          sectionA much shorter section title
          blindtext

          enddocument


          enter image description here






          share|improve this answer























          • Thanks man. I really appreciate it.

            – Ravi
            Apr 3 at 14:38


















          5














          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
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageblindtext

          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt

          newcommand*sectionFont%
          Largebfseries


          % For section
          titleformatsection[block]sectionFont0pt%
          fboxthesection hsp textcolorseccolortextbarhsp #1

          % For section*
          titleformatname=section, numberless[block]sectionFont0pt%
          fbox#1

          begindocument
          chapterSome chapter

          sectionSection title

          Foo bar.

          section*Unnumbered section

          blindtext
          enddocument


          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
          usepackagelmodern
          usepackage[T1]fontenc
          usepackage[utf8]inputenc
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageetoolbox
          usepackagevarwidth
          usepackagecalc
          usepackageblindtext

          usepackagetikz
          % Uncomment the following line if you use babel and have a recent enough TikZ:
          % usetikzlibrarybabel
          usetikzlibrarybackgrounds
          usetikzlibrarycalc
          usetikzlibraryfit
          usetikzlibraryshadows

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



          % Colors used for decorated section titles
          definecolorsectionDecoRuleColorRGB70,10,10
          colorletsectionDecoBgColoryellow!10

          % Horizontal spacing used for decorated section titles
          newlengthsectionDecoHsp
          setlengthsectionDecoHsp8pt

          newcommand*sectionDecoRuleWidth0.4pt

          % Boolean flag indicating whether we are working with a section or a section*
          newtogglesectionDecoIsNumberedSec

          makeatletter

          % High-level command for section decorations. Two variants: one for section
          % and one for section*.
          newcommand*sectionDecoration%
          @ifstar%
          togglefalsesectionDecoIsNumberedSec%
          @sectionDecoration@nil % @nil passed instead of the section number
          %
          toggletruesectionDecoIsNumberedSec%
          @sectionDecoration
          %


          % #1: the section number (@nil if we are working for a section*)
          % #2: the section title
          newcommand*@sectionDecoration[2]%
          begintikzpicture
          % The section number
          iftogglesectionDecoIsNumberedSec% 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) - 2sectionDecoHsp 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: rulen11pt%
          strut;

          beginscope[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.5pgflinewidth]section number.north west)
          ([xshift=-0.5pgflinewidth]section title.south east),
          color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
          (frame) ;

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

          endscope

          % Display key points (only useful for debugging)
          % beginscope[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) ;
          % endscope
          endtikzpicture%


          makeatother

          newcommand*sectionTitleFontLargebfseries

          % For section
          titleformatsection[block]sectionTitleFont0pt%
          sectionDecorationthesection#1

          % For section*
          titleformatname=section, numberless[block]sectionTitleFont0pt%
          sectionDecoration*#1

          begindocument
          chapterSome chapter

          sectionA short section title

          sectionA 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
          enddocument


          enter image description here






          share|improve this answer

























          • Thank you very much for helping me!

            – Ravi
            Apr 3 at 8:19











          • 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








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "85"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482915%2fhow-to-add-frame-around-section-using-titlesec%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          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
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolortextbarhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          usetikzlibrarypositioning
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikz[font=Largebfseries]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.3333emphantomthesectionhspace.3333em#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          enter image description here






          share|improve this answer




















          • 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.

            – JouleV
            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!

            – JouleV
            Apr 3 at 14:40















          6














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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          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
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolortextbarhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          usetikzlibrarypositioning
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikz[font=Largebfseries]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.3333emphantomthesectionhspace.3333em#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          enter image description here






          share|improve this answer




















          • 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.

            – JouleV
            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!

            – JouleV
            Apr 3 at 14:40













          6












          6








          6







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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          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
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolortextbarhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          usetikzlibrarypositioning
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikz[font=Largebfseries]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.3333emphantomthesectionhspace.3333em#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          enter image description here






          share|improve this answer















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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolorhsp#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          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
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikznode[draw,max width=textwidth-.6666em-.4pt]Largebfseriesthesectionhsptextcolorseccolortextbarhsp#1;
          begindocument
          sectionSection Name
          blindtext
          enddocument


          enter image description here




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



          documentclass[10pt,a4paper,twoside]report
          usepackageblindtext
          usepackage[explicit]titlesec
          usepackagecolor
          usepackagetikz
          usepackagevarwidth
          usepackagecalc
          usetikzlibrarypositioning
          tikzset
          max width/.style args=#1
          execute at begin node=beginvarwidth#1,
          execute at end node=endvarwidth


          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection
          [hang]
          Largebfseries

          0pt
          tikz[font=Largebfseries]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.3333emphantomthesectionhspace.3333em#1;
          begindocument
          sectionSection Name
          blindtext
          sectionThis is a very long section name that has to be broken into two lines
          enddocument


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 3 at 9:19

























          answered Apr 3 at 8:03









          JouleVJouleV

          14.6k22665




          14.6k22665







          • 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.

            – JouleV
            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!

            – JouleV
            Apr 3 at 14:40












          • 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.

            – JouleV
            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!

            – JouleV
            Apr 3 at 14:40







          1




          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





          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




          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.

          – JouleV
          Apr 3 at 10:32





          @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.

          – JouleV
          Apr 3 at 10:32




          3




          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





          @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




          1





          @frougon Great!

          – JouleV
          Apr 3 at 14:40





          @frougon Great!

          – JouleV
          Apr 3 at 14:40











          5














          A solution with a simple tabulary:



          documentclass[10pt, a4paper,svgnames, twoside]report
          usepackage[utf8]inputenc
          usepackage[T1]fontenc
          usepackage[showframe]geometry
          usepackageblindtext
          usepackagearray, tabulary
          usepackage[explicit]titlesec
          usepackageblindtext, xcolor
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection[block]Largebfseriessffamilysetlengthfboxrule1ptcolorSlateGrey0ptfboxbegintabularydimexprlinewidth-tabcolsep-fboxrule@l!vline width 1.2ptLthesection &#1 endtabulary

          begindocument

          setcounterchapter2
          sectionSection Title. Some more more text to have a really very very long section title.
          blindtext
          sectionA much shorter section title
          blindtext

          enddocument


          enter image description here






          share|improve this answer























          • Thanks man. I really appreciate it.

            – Ravi
            Apr 3 at 14:38















          5














          A solution with a simple tabulary:



          documentclass[10pt, a4paper,svgnames, twoside]report
          usepackage[utf8]inputenc
          usepackage[T1]fontenc
          usepackage[showframe]geometry
          usepackageblindtext
          usepackagearray, tabulary
          usepackage[explicit]titlesec
          usepackageblindtext, xcolor
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection[block]Largebfseriessffamilysetlengthfboxrule1ptcolorSlateGrey0ptfboxbegintabularydimexprlinewidth-tabcolsep-fboxrule@l!vline width 1.2ptLthesection &#1 endtabulary

          begindocument

          setcounterchapter2
          sectionSection Title. Some more more text to have a really very very long section title.
          blindtext
          sectionA much shorter section title
          blindtext

          enddocument


          enter image description here






          share|improve this answer























          • Thanks man. I really appreciate it.

            – Ravi
            Apr 3 at 14:38













          5












          5








          5







          A solution with a simple tabulary:



          documentclass[10pt, a4paper,svgnames, twoside]report
          usepackage[utf8]inputenc
          usepackage[T1]fontenc
          usepackage[showframe]geometry
          usepackageblindtext
          usepackagearray, tabulary
          usepackage[explicit]titlesec
          usepackageblindtext, xcolor
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection[block]Largebfseriessffamilysetlengthfboxrule1ptcolorSlateGrey0ptfboxbegintabularydimexprlinewidth-tabcolsep-fboxrule@l!vline width 1.2ptLthesection &#1 endtabulary

          begindocument

          setcounterchapter2
          sectionSection Title. Some more more text to have a really very very long section title.
          blindtext
          sectionA much shorter section title
          blindtext

          enddocument


          enter image description here






          share|improve this answer













          A solution with a simple tabulary:



          documentclass[10pt, a4paper,svgnames, twoside]report
          usepackage[utf8]inputenc
          usepackage[T1]fontenc
          usepackage[showframe]geometry
          usepackageblindtext
          usepackagearray, tabulary
          usepackage[explicit]titlesec
          usepackageblindtext, xcolor
          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt
          titleformatsection[block]Largebfseriessffamilysetlengthfboxrule1ptcolorSlateGrey0ptfboxbegintabularydimexprlinewidth-tabcolsep-fboxrule@l!vline width 1.2ptLthesection &#1 endtabulary

          begindocument

          setcounterchapter2
          sectionSection Title. Some more more text to have a really very very long section title.
          blindtext
          sectionA much shorter section title
          blindtext

          enddocument


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 3 at 13:26









          BernardBernard

          177k778210




          177k778210












          • Thanks man. I really appreciate it.

            – Ravi
            Apr 3 at 14:38

















          • Thanks man. I really appreciate it.

            – Ravi
            Apr 3 at 14:38
















          Thanks man. I really appreciate it.

          – Ravi
          Apr 3 at 14:38





          Thanks man. I really appreciate it.

          – Ravi
          Apr 3 at 14:38











          5














          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
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageblindtext

          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt

          newcommand*sectionFont%
          Largebfseries


          % For section
          titleformatsection[block]sectionFont0pt%
          fboxthesection hsp textcolorseccolortextbarhsp #1

          % For section*
          titleformatname=section, numberless[block]sectionFont0pt%
          fbox#1

          begindocument
          chapterSome chapter

          sectionSection title

          Foo bar.

          section*Unnumbered section

          blindtext
          enddocument


          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
          usepackagelmodern
          usepackage[T1]fontenc
          usepackage[utf8]inputenc
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageetoolbox
          usepackagevarwidth
          usepackagecalc
          usepackageblindtext

          usepackagetikz
          % Uncomment the following line if you use babel and have a recent enough TikZ:
          % usetikzlibrarybabel
          usetikzlibrarybackgrounds
          usetikzlibrarycalc
          usetikzlibraryfit
          usetikzlibraryshadows

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



          % Colors used for decorated section titles
          definecolorsectionDecoRuleColorRGB70,10,10
          colorletsectionDecoBgColoryellow!10

          % Horizontal spacing used for decorated section titles
          newlengthsectionDecoHsp
          setlengthsectionDecoHsp8pt

          newcommand*sectionDecoRuleWidth0.4pt

          % Boolean flag indicating whether we are working with a section or a section*
          newtogglesectionDecoIsNumberedSec

          makeatletter

          % High-level command for section decorations. Two variants: one for section
          % and one for section*.
          newcommand*sectionDecoration%
          @ifstar%
          togglefalsesectionDecoIsNumberedSec%
          @sectionDecoration@nil % @nil passed instead of the section number
          %
          toggletruesectionDecoIsNumberedSec%
          @sectionDecoration
          %


          % #1: the section number (@nil if we are working for a section*)
          % #2: the section title
          newcommand*@sectionDecoration[2]%
          begintikzpicture
          % The section number
          iftogglesectionDecoIsNumberedSec% 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) - 2sectionDecoHsp 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: rulen11pt%
          strut;

          beginscope[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.5pgflinewidth]section number.north west)
          ([xshift=-0.5pgflinewidth]section title.south east),
          color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
          (frame) ;

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

          endscope

          % Display key points (only useful for debugging)
          % beginscope[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) ;
          % endscope
          endtikzpicture%


          makeatother

          newcommand*sectionTitleFontLargebfseries

          % For section
          titleformatsection[block]sectionTitleFont0pt%
          sectionDecorationthesection#1

          % For section*
          titleformatname=section, numberless[block]sectionTitleFont0pt%
          sectionDecoration*#1

          begindocument
          chapterSome chapter

          sectionA short section title

          sectionA 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
          enddocument


          enter image description here






          share|improve this answer

























          • Thank you very much for helping me!

            – Ravi
            Apr 3 at 8:19











          • 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















          5














          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
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageblindtext

          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt

          newcommand*sectionFont%
          Largebfseries


          % For section
          titleformatsection[block]sectionFont0pt%
          fboxthesection hsp textcolorseccolortextbarhsp #1

          % For section*
          titleformatname=section, numberless[block]sectionFont0pt%
          fbox#1

          begindocument
          chapterSome chapter

          sectionSection title

          Foo bar.

          section*Unnumbered section

          blindtext
          enddocument


          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
          usepackagelmodern
          usepackage[T1]fontenc
          usepackage[utf8]inputenc
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageetoolbox
          usepackagevarwidth
          usepackagecalc
          usepackageblindtext

          usepackagetikz
          % Uncomment the following line if you use babel and have a recent enough TikZ:
          % usetikzlibrarybabel
          usetikzlibrarybackgrounds
          usetikzlibrarycalc
          usetikzlibraryfit
          usetikzlibraryshadows

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



          % Colors used for decorated section titles
          definecolorsectionDecoRuleColorRGB70,10,10
          colorletsectionDecoBgColoryellow!10

          % Horizontal spacing used for decorated section titles
          newlengthsectionDecoHsp
          setlengthsectionDecoHsp8pt

          newcommand*sectionDecoRuleWidth0.4pt

          % Boolean flag indicating whether we are working with a section or a section*
          newtogglesectionDecoIsNumberedSec

          makeatletter

          % High-level command for section decorations. Two variants: one for section
          % and one for section*.
          newcommand*sectionDecoration%
          @ifstar%
          togglefalsesectionDecoIsNumberedSec%
          @sectionDecoration@nil % @nil passed instead of the section number
          %
          toggletruesectionDecoIsNumberedSec%
          @sectionDecoration
          %


          % #1: the section number (@nil if we are working for a section*)
          % #2: the section title
          newcommand*@sectionDecoration[2]%
          begintikzpicture
          % The section number
          iftogglesectionDecoIsNumberedSec% 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) - 2sectionDecoHsp 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: rulen11pt%
          strut;

          beginscope[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.5pgflinewidth]section number.north west)
          ([xshift=-0.5pgflinewidth]section title.south east),
          color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
          (frame) ;

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

          endscope

          % Display key points (only useful for debugging)
          % beginscope[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) ;
          % endscope
          endtikzpicture%


          makeatother

          newcommand*sectionTitleFontLargebfseries

          % For section
          titleformatsection[block]sectionTitleFont0pt%
          sectionDecorationthesection#1

          % For section*
          titleformatname=section, numberless[block]sectionTitleFont0pt%
          sectionDecoration*#1

          begindocument
          chapterSome chapter

          sectionA short section title

          sectionA 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
          enddocument


          enter image description here






          share|improve this answer

























          • Thank you very much for helping me!

            – Ravi
            Apr 3 at 8:19











          • 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













          5












          5








          5







          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
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageblindtext

          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt

          newcommand*sectionFont%
          Largebfseries


          % For section
          titleformatsection[block]sectionFont0pt%
          fboxthesection hsp textcolorseccolortextbarhsp #1

          % For section*
          titleformatname=section, numberless[block]sectionFont0pt%
          fbox#1

          begindocument
          chapterSome chapter

          sectionSection title

          Foo bar.

          section*Unnumbered section

          blindtext
          enddocument


          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
          usepackagelmodern
          usepackage[T1]fontenc
          usepackage[utf8]inputenc
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageetoolbox
          usepackagevarwidth
          usepackagecalc
          usepackageblindtext

          usepackagetikz
          % Uncomment the following line if you use babel and have a recent enough TikZ:
          % usetikzlibrarybabel
          usetikzlibrarybackgrounds
          usetikzlibrarycalc
          usetikzlibraryfit
          usetikzlibraryshadows

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



          % Colors used for decorated section titles
          definecolorsectionDecoRuleColorRGB70,10,10
          colorletsectionDecoBgColoryellow!10

          % Horizontal spacing used for decorated section titles
          newlengthsectionDecoHsp
          setlengthsectionDecoHsp8pt

          newcommand*sectionDecoRuleWidth0.4pt

          % Boolean flag indicating whether we are working with a section or a section*
          newtogglesectionDecoIsNumberedSec

          makeatletter

          % High-level command for section decorations. Two variants: one for section
          % and one for section*.
          newcommand*sectionDecoration%
          @ifstar%
          togglefalsesectionDecoIsNumberedSec%
          @sectionDecoration@nil % @nil passed instead of the section number
          %
          toggletruesectionDecoIsNumberedSec%
          @sectionDecoration
          %


          % #1: the section number (@nil if we are working for a section*)
          % #2: the section title
          newcommand*@sectionDecoration[2]%
          begintikzpicture
          % The section number
          iftogglesectionDecoIsNumberedSec% 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) - 2sectionDecoHsp 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: rulen11pt%
          strut;

          beginscope[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.5pgflinewidth]section number.north west)
          ([xshift=-0.5pgflinewidth]section title.south east),
          color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
          (frame) ;

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

          endscope

          % Display key points (only useful for debugging)
          % beginscope[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) ;
          % endscope
          endtikzpicture%


          makeatother

          newcommand*sectionTitleFontLargebfseries

          % For section
          titleformatsection[block]sectionTitleFont0pt%
          sectionDecorationthesection#1

          % For section*
          titleformatname=section, numberless[block]sectionTitleFont0pt%
          sectionDecoration*#1

          begindocument
          chapterSome chapter

          sectionA short section title

          sectionA 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
          enddocument


          enter image description here






          share|improve this answer















          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
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageblindtext

          definecolorseccolorRGB41,48,57
          newcommandhsphspace8pt

          newcommand*sectionFont%
          Largebfseries


          % For section
          titleformatsection[block]sectionFont0pt%
          fboxthesection hsp textcolorseccolortextbarhsp #1

          % For section*
          titleformatname=section, numberless[block]sectionFont0pt%
          fbox#1

          begindocument
          chapterSome chapter

          sectionSection title

          Foo bar.

          section*Unnumbered section

          blindtext
          enddocument


          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
          usepackagelmodern
          usepackage[T1]fontenc
          usepackage[utf8]inputenc
          usepackagexcolor
          usepackage[explicit]titlesec
          usepackageetoolbox
          usepackagevarwidth
          usepackagecalc
          usepackageblindtext

          usepackagetikz
          % Uncomment the following line if you use babel and have a recent enough TikZ:
          % usetikzlibrarybabel
          usetikzlibrarybackgrounds
          usetikzlibrarycalc
          usetikzlibraryfit
          usetikzlibraryshadows

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



          % Colors used for decorated section titles
          definecolorsectionDecoRuleColorRGB70,10,10
          colorletsectionDecoBgColoryellow!10

          % Horizontal spacing used for decorated section titles
          newlengthsectionDecoHsp
          setlengthsectionDecoHsp8pt

          newcommand*sectionDecoRuleWidth0.4pt

          % Boolean flag indicating whether we are working with a section or a section*
          newtogglesectionDecoIsNumberedSec

          makeatletter

          % High-level command for section decorations. Two variants: one for section
          % and one for section*.
          newcommand*sectionDecoration%
          @ifstar%
          togglefalsesectionDecoIsNumberedSec%
          @sectionDecoration@nil % @nil passed instead of the section number
          %
          toggletruesectionDecoIsNumberedSec%
          @sectionDecoration
          %


          % #1: the section number (@nil if we are working for a section*)
          % #2: the section title
          newcommand*@sectionDecoration[2]%
          begintikzpicture
          % The section number
          iftogglesectionDecoIsNumberedSec% 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) - 2sectionDecoHsp 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: rulen11pt%
          strut;

          beginscope[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.5pgflinewidth]section number.north west)
          ([xshift=-0.5pgflinewidth]section title.south east),
          color=sectionDecoRuleColor, fill=sectionDecoBgColor, drop shadow]
          (frame) ;

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

          endscope

          % Display key points (only useful for debugging)
          % beginscope[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) ;
          % endscope
          endtikzpicture%


          makeatother

          newcommand*sectionTitleFontLargebfseries

          % For section
          titleformatsection[block]sectionTitleFont0pt%
          sectionDecorationthesection#1

          % For section*
          titleformatname=section, numberless[block]sectionTitleFont0pt%
          sectionDecoration*#1

          begindocument
          chapterSome chapter

          sectionA short section title

          sectionA 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
          enddocument


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 3 at 15:04

























          answered Apr 3 at 8:07









          frougonfrougon

          1,079712




          1,079712












          • Thank you very much for helping me!

            – Ravi
            Apr 3 at 8:19











          • 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

















          • Thank you very much for helping me!

            – Ravi
            Apr 3 at 8:19











          • 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
















          Thank you very much for helping me!

          – Ravi
          Apr 3 at 8:19





          Thank you very much for helping me!

          – Ravi
          Apr 3 at 8:19













          Thanks, I really appreciate it. It will also help someone.

          – Ravi
          Apr 3 at 14:28





          Thanks, I really appreciate it. It will also help someone.

          – Ravi
          Apr 3 at 14:28




          1




          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





          @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

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482915%2fhow-to-add-frame-around-section-using-titlesec%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

          Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

          Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High