1

I am currently using the following code to produce a fancy section title in LaTeX. However, I am be having trouble figuring out how to get rid of the additional horizontal space before and after the title (Introduction). I'd like the underline to extend only as far as the end of the title with no additional space. I'd also like the blue box's width to be vary according to the width of the section number inside it (so no extra space as in the picture below). How can I achieve this? enter image description here

\documentclass{article}

\usepackage{lipsum} \usepackage{xcolor} \usepackage{calc} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc}

\usepackage{xcolor, lipsum} \renewcommand*\thesection{\arabic{section}} \usepackage[explicit,calcwidth]{titlesec} \definecolor{myBlue}{HTML}{0088FF}

\titleformat{\section}[block]{\Large\bfseries\sffamily} {\rlap{\color{red}\rule[-6pt]{\titlewidth}{1pt}}\colorbox{myBlue}{ \raisebox{0pt}[13pt][3pt]{\makebox[60pt]{ \selectfont\color{white}{\thesection}} }}} {15pt} {\color{myBlue}#1} \titlespacing*{\section}{0pt}{0mm}{0mm}

\begin{document}

\section{Introduction} \lipsum[1]

\end{document}

wrb98
  • 876

1 Answers1

1

Don't forget about spaces generated by endlines.

Almost always the explicit option to titlesec is unneeded, like in your case.

I reformatted the code to show more clearly the various nestings. Note the % at the endlines that would generate a space.

The \selectfont declaration you had was useless and has been removed.

\titleformat{\section}[block]
  {\Large\bfseries\sffamily}
  {%
   \rlap{\color{red}\rule[-6pt]{\titlewidth}{1pt}}%
   \colorbox{myBlue}{%
     \raisebox{0pt}[13pt][3pt]{%
       \makebox[60pt]{%
         \color{white}\thesection
       }%
     }%
   }%
  }
  {15pt}
  {\color{myBlue}}

enter image description here

\documentclass{article}

\usepackage{lipsum} \usepackage{xcolor} \usepackage{calc} \usepackage[T1]{fontenc} \usepackage[calcwidth]{titlesec}

\usepackage{lipsum}

\renewcommand*\thesection{\arabic{section}}

\definecolor{myBlue}{HTML}{0088FF}

\titleformat{\section}[block] {\Large\bfseries\sffamily} {% \rlap{\color{red}\rule[-6pt]{\titlewidth}{1pt}}% \colorbox{myBlue}{% \raisebox{0pt}[13pt][3pt]{\makebox[60pt]{% \color{white}\thesection}% }% }% } {15pt} {\color{myBlue}} \titlespacing*{\section}{0pt}{0mm}{0mm}

\begin{document}

\section{Introduction} \lipsum[1]

\end{document}

egreg
  • 1,121,712