TransWikia.com

Background color in Section titles

TeX - LaTeX Asked by DSgUY on March 19, 2021

I’m still a bit newbie in latex and i’m editing a cheatsheet template that I found with some math formulas and extreme compact form. That is why I want to add a color background to the Sections titles form better reading. I already tried everything I found googling and in latex pages without success.

I managed to locate what I think is the section that I need to change to achieve what I want. My best result was a color backgrounds but with the titles in a new line (the color box was in one line and the title in another) and also the line didn’t stop at the column.

https://github.com/blechturm/MITx_capstone_1/blob/master/content/def.tex
This is where I think I need to change some code:

makeatletter % Author: https://tex.stackexchange.com/questions/218587/how-to-set-one-header-for-each-page-using-multicols
renewcommand{section}{@startsection{section}{1}{0mm}%
                                {.2ex}%
                                {.2ex}%x
                                {color{black}sffamilysmallbfseries}}
renewcommand{subsection}{@startsection{subsection}{1}{0mm}%
                                {.2ex}%
                                {.2ex}%x
                                {sffamilybfseries}}

Any help is appreciated!
Regards.

2 Answers

It is easier to use titlesec. Since you are a bit light on what you want to achieve, I'll just show an example.

documentclass{article}
usepackage[svgnames]{xcolor}
usepackage[explicit]{titlesec}


titleformat{section}
[display]
{sffamilycolor{black}smallbfseries}
{}
{0pt}
{colorbox{Green!30}{Sthesection. #1}}

begin{document}
section{First section}
section{Second section}

end{document} 

enter image description here

Answered by Willie Wong on March 19, 2021

I think I manage to do what I want. Probabibly I'm rewriting the first section configuration but this works. I tried to write the same inside the first renewcommand but it didnt work.

makeatletter % Author: https://tex.stackexchange.com/questions/218587/how-to-set-one-header-for-each-page-using-multicols
renewcommand{section}{@startsection{section}{1}{0mm}%
                                {.2ex}%
                                {.2ex}%x
                                {color{black}sffamilysmallbfseries}
                                }
                                
renewcommand{subsection}{@startsection{subsection}{1}{0mm}%
                                {.2ex}%
                                {.2ex}%x
                                {sffamilybfseries}}
usepackage{titlesec}
titleformat{name=section}[block]
  {color{black}sffamilysmallbfseries}
  {}
  {0pt}
  {colorsection}

newcommand{colorsection}[1]{%
  colorbox{yellow}{parbox{dimexprcolumnwidth-2fboxsep-2fboxrulerelax}{strutthesection~#1strut}}}

enter image description here

Answered by DSgUY on March 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP