How to add frame around section using titlesec?












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





























    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



























      1












      1








      1


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







      Ravi

















      asked 4 hours ago









      RaviRavi

      1387




      1387






















          2 Answers
          2






          active

          oldest

          votes


















          2














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



















          2














          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
            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
            1 hour 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
            1 hour ago












          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

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














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
















          2














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














          2












          2








          2







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

























          answered 4 hours ago









          frougonfrougon

          686611




          686611













          • Thank you very much for helping me!

            – Ravi
            3 hours ago



















          • Thank you very much for helping me!

            – Ravi
            3 hours ago

















          Thank you very much for helping me!

          – Ravi
          3 hours ago





          Thank you very much for helping me!

          – Ravi
          3 hours ago











          2














          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
            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
            1 hour 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
            1 hour ago
















          2














          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
            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
            1 hour 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
            1 hour ago














          2












          2








          2







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

























          answered 4 hours ago









          JouleVJouleV

          10.1k22558




          10.1k22558













          • Thanks a million!

            – Ravi
            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
            1 hour 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
            1 hour ago



















          • Thanks a million!

            – Ravi
            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
            1 hour 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
            1 hour ago

















          Thanks a million!

          – Ravi
          3 hours ago





          Thanks a million!

          – Ravi
          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
          1 hour 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
          1 hour 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
          1 hour 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
          1 hour ago


















          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

          Liste der Baudenkmäler in Partschins

          Sámuel Mokry

          Parko Thabor