How to add frame around section using titlesec?

Multi tool use
Multi tool use












2















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]{Largebfseries}{thesectionhsptextcolor{seccolor}{|}hsp}{0pt}{Largebfseries}

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


enter image description here










share|improve this question





























    2















    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]{Largebfseries}{thesectionhsptextcolor{seccolor}{|}hsp}{0pt}{Largebfseries}

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


    enter image description here










    share|improve this question



























      2












      2








      2


      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}
      usepackage{blindtext}
      usepackage{titlesec, blindtext, color}
      definecolor{seccolor}{RGB}{41,48,57}
      newcommand{hsp}{hspace{8pt}}
      titleformat{section}[hang]{Largebfseries}{thesectionhsptextcolor{seccolor}{|}hsp}{0pt}{Largebfseries}

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


      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}
      usepackage{blindtext}
      usepackage{titlesec, blindtext, color}
      definecolor{seccolor}{RGB}{41,48,57}
      newcommand{hsp}{hspace{8pt}}
      titleformat{section}[hang]{Largebfseries}{thesectionhsptextcolor{seccolor}{|}hsp}{0pt}{Largebfseries}

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


      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 5 hours ago







      Ravi

















      asked 6 hours ago









      RaviRavi

      1457




      1457






















          3 Answers
          3






          active

          oldest

          votes


















          3














          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]
          {Largebfseries}
          {}
          {0pt}
          {tikznode[draw]{Largebfseriesthesectionhsptextcolor{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]
          {Largebfseries}
          {}
          {0pt}
          {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
          {Largebfseries}
          {}
          {0pt}
          {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
          {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{.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






          share|improve this answer


























          • Thanks a million!

            – Ravi
            5 hours ago






          • 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
            3 hours ago






          • 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
            3 hours ago











          • @JouleV I haven't expected so much. Thanks again.

            – Ravi
            1 hour ago






          • 1





            @Ravi You are welcome.

            – JouleV
            1 hour ago



















          3














          Solution based on fbox, using textbar for the vertical bar, and with 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}{%
          Largebfseries
          }

          % 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



          If you want finer control than what fbox allows (fboxsep and fboxrule), using a tikzpicture environment for the frame is a good alternative.






          share|improve this answer


























          • Thank you very much for helping me!

            – Ravi
            5 hours ago



















          1














          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]{Largebfseriessffamilysetlength{fboxrule}{1pt}color{SlateGrey}}{}{0pt}{fbox{begin{tabulary}{dimexprlinewidth-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






          share|improve this answer
























            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









            3














            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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {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{.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






            share|improve this answer


























            • Thanks a million!

              – Ravi
              5 hours ago






            • 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
              3 hours ago






            • 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
              3 hours ago











            • @JouleV I haven't expected so much. Thanks again.

              – Ravi
              1 hour ago






            • 1





              @Ravi You are welcome.

              – JouleV
              1 hour ago
















            3














            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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {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{.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






            share|improve this answer


























            • Thanks a million!

              – Ravi
              5 hours ago






            • 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
              3 hours ago






            • 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
              3 hours ago











            • @JouleV I haven't expected so much. Thanks again.

              – Ravi
              1 hour ago






            • 1





              @Ravi You are welcome.

              – JouleV
              1 hour ago














            3












            3








            3







            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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {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{.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






            share|improve this answer















            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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {Largebfseries}
            {}
            {0pt}
            {tikznode[draw,max width=textwidth-.6666em-.4pt]{Largebfseriesthesectionhsptextcolor{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]
            {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{.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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 4 hours ago

























            answered 6 hours ago









            JouleVJouleV

            10.1k22558




            10.1k22558













            • Thanks a million!

              – Ravi
              5 hours ago






            • 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
              3 hours ago






            • 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
              3 hours ago











            • @JouleV I haven't expected so much. Thanks again.

              – Ravi
              1 hour ago






            • 1





              @Ravi You are welcome.

              – JouleV
              1 hour ago



















            • Thanks a million!

              – Ravi
              5 hours ago






            • 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
              3 hours ago






            • 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
              3 hours ago











            • @JouleV I haven't expected so much. Thanks again.

              – Ravi
              1 hour ago






            • 1





              @Ravi You are welcome.

              – JouleV
              1 hour ago

















            Thanks a million!

            – Ravi
            5 hours ago





            Thanks a million!

            – Ravi
            5 hours ago




            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
            3 hours ago





            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
            3 hours ago




            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
            3 hours ago





            @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
            3 hours ago













            @JouleV I haven't expected so much. Thanks again.

            – Ravi
            1 hour ago





            @JouleV I haven't expected so much. Thanks again.

            – Ravi
            1 hour ago




            1




            1





            @Ravi You are welcome.

            – JouleV
            1 hour ago





            @Ravi You are welcome.

            – JouleV
            1 hour ago











            3














            Solution based on fbox, using textbar for the vertical bar, and with 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}{%
            Largebfseries
            }

            % 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



            If you want finer control than what fbox allows (fboxsep and fboxrule), using a tikzpicture environment for the frame is a good alternative.






            share|improve this answer


























            • Thank you very much for helping me!

              – Ravi
              5 hours ago
















            3














            Solution based on fbox, using textbar for the vertical bar, and with 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}{%
            Largebfseries
            }

            % 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



            If you want finer control than what fbox allows (fboxsep and fboxrule), using a tikzpicture environment for the frame is a good alternative.






            share|improve this answer


























            • Thank you very much for helping me!

              – Ravi
              5 hours ago














            3












            3








            3







            Solution based on fbox, using textbar for the vertical bar, and with 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}{%
            Largebfseries
            }

            % 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



            If you want finer control than what fbox allows (fboxsep and fboxrule), using a tikzpicture environment for the frame is a good alternative.






            share|improve this answer















            Solution based on fbox, using textbar for the vertical bar, and with 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}{%
            Largebfseries
            }

            % 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



            If you want finer control than what fbox allows (fboxsep and fboxrule), using a tikzpicture environment for the frame is a good alternative.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago

























            answered 6 hours ago









            frougonfrougon

            696611




            696611













            • Thank you very much for helping me!

              – Ravi
              5 hours ago



















            • Thank you very much for helping me!

              – Ravi
              5 hours ago

















            Thank you very much for helping me!

            – Ravi
            5 hours ago





            Thank you very much for helping me!

            – Ravi
            5 hours ago











            1














            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]{Largebfseriessffamilysetlength{fboxrule}{1pt}color{SlateGrey}}{}{0pt}{fbox{begin{tabulary}{dimexprlinewidth-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






            share|improve this answer




























              1














              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]{Largebfseriessffamilysetlength{fboxrule}{1pt}color{SlateGrey}}{}{0pt}{fbox{begin{tabulary}{dimexprlinewidth-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






              share|improve this answer


























                1












                1








                1







                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]{Largebfseriessffamilysetlength{fboxrule}{1pt}color{SlateGrey}}{}{0pt}{fbox{begin{tabulary}{dimexprlinewidth-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






                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}
                usepackage{blindtext}
                usepackage{array, tabulary}
                usepackage[explicit]{titlesec}
                usepackage{blindtext, xcolor}
                definecolor{seccolor}{RGB}{41,48,57}
                newcommand{hsp}{hspace{8pt}}
                titleformat{section}[block]{Largebfseriessffamilysetlength{fboxrule}{1pt}color{SlateGrey}}{}{0pt}{fbox{begin{tabulary}{dimexprlinewidth-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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 42 mins ago









                BernardBernard

                175k776207




                175k776207






























                    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







                    nst,QD3hevYZaEIz
                    LvRKWflMF7s3N

                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)