1

I just stumbled upon this question about fancy heading. I want to know if there is a way for the rectangle to cover the section name no matter how long or short it is?

\documentclass{article}
\usepackage{titlesec}
\usepackage{tikz}    

\titleformat{\section}[block]{
\LARGE\bfseries\tikz[overlay] \shade[bottom color=gray!20, middle color= gray!10, top color=white] (0,-1ex) rectangle (\textwidth,1em);
}{\thesection}{0.5cm}{}

\begin{document}
\section{section name}
\end{document}
2
  • Not clear what you mean. Do you mean you want the rectangle to be solid? Or over several lines of title? Jun 3, 2015 at 8:25
  • I want to find a way that the rectangle adjust to the width of the section name, i.e. If I have a short section name I want the rectangle adjust to this and not to \textwidth
    – Very23
    Jun 3, 2015 at 17:50

2 Answers 2

2

Let me extend the answer of Aubrey Blumsohn with a little modification.

With tcbox width=auto limited, the box will switch to break the lines, if the maximum width is reached. This feature is quite new (2015-03-23). You may need to update the tcolorbox package for it.

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usepackage[skins,breakable]{tcolorbox}

\titleformat{\section}
  {\LARGE\bfseries\selectfont}
  {}{0pt}
  {\tcbox[
      tcbox width=auto limited,% <-----------
      enhanced,
      %boxrule=0pt,
      rightrule=0pt,
      toprule=0pt,
      bottomrule=0pt,
      leftrule=3pt,
      arc=0pt,
      outer arc=0pt,
      left=5pt,
      interior code={\shade[bottom color=gray!20, top color=white] (frame.north west) rectangle (frame.south east);},
    ]{\thesection\hskip0.7em#1}}

\begin{document}
\section{Section name}
\section{Section name longer}
\section{Section name extremely long}
\section{Section name extremely long and longer}
\section{Section name extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long }
\end{document}

enter image description here

3

Tcolorbox may be overkill here, but it is generally useful for flexible titles. You haven't said what you want to happen to your shading if the title is extremely long, so I have just let it run off the page -- two line shading will look lousy

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{tikz}    
\usepackage[skins,breakable]{tcolorbox}    

\titleformat{\section}
  {\LARGE\bfseries\selectfont}
  {}{0pt}
  {\begin{tcolorbox}[
      enhanced,
      %boxrule=0pt,
      rightrule=0pt,
      toprule=0pt,
      bottomrule=0pt,
      leftrule=3pt,
      arc=0pt,
      outer arc=0pt,
      hbox,
      %breakable,
      left=5pt,
      interior code={\shade[bottom color=gray!20, top color=white] (frame.north west) rectangle (frame.south east);},
    ]\thesection\hskip0.7em#1\end{tcolorbox}}

\begin{document}
\section{Section name}
\section{Section name longer}
\section{Section name extremely long}
\section{Section name extremely long and longer}
\section{Section name extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long extremely long }
\end{document}

enter image description here

3
  • Nice solution, but I wondering, what is use of section title which is cut out by page border?
    – Zarko
    Jun 3, 2015 at 20:24
  • 1
    Probably none, but then a mix of 1 line and 2 line shaded titles looks dreadful anyway, so if you want section titles in this format I think the idea is to keep them short. You can make them wrap by commenting the hbox and uncommenting the breakable. I don't think there is a way to make this sort of tcolorbox both breakable and shorten by length (but I may be wrong). Jun 3, 2015 at 20:58
  • Thanks @AubreyBlumsohn actually Im working with shorts section names, so It will be OK
    – Very23
    Jun 4, 2015 at 0:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .