[Tex/LaTex] Changing background color of text in Latex

backgroundscolortext manipulationtext-decorations

How can I make change a text background in LaTeX?
For example using Latex code

\textsc{Extra Curricular Achievements}\

I get
enter image description here

However, I want the background of this heading to be changed so that it looks like this:-
enter image description here

I want the background to be extended to the right upto line break.
Please ignore the underline in the previous image.
Thanks.

Best Answer

I prefer using tcolorbox thinking that in future you may want the background to be fashionable. I have given many options (which are not needed for this particular case) in the tcbset so that you can play with them to suit your needs.

\documentclass{article}
\usepackage[most]{tcolorbox}

\tcbset{
    frame code={}
    center title,
    left=0pt,
    right=0pt,
    top=0pt,
    bottom=0pt,
    colback=gray!70,
    colframe=white,
    width=\dimexpr\textwidth\relax,
    enlarge left by=0mm,
    boxsep=5pt,
    arc=0pt,outer arc=0pt,
    }

\begin{document}
\begin{tcolorbox}
\textsc{Extra Curricular Achievements}
\end{tcolorbox}
\end{document}

enter image description here

Here is another option using framed package.

\documentclass{article}
\usepackage{xcolor}
\usepackage{framed}
\definecolor{shadecolor}{RGB}{180,180,180}
\begin{document}
\begin{snugshade*}
\noindent\textsc{Extra Curricular Achievements}
\end{snugshade*}
\end{document}

enter image description here

Without extra packages:

\documentclass{article}
\usepackage{xcolor}
\definecolor{shadecolor}{RGB}{150,150,150}
\begin{document}
\noindent\colorbox{shadecolor}
{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\textsc{Extra Curricular Achievements}}}

\end{document}

enter image description here

And convert it in to a macro:

\documentclass{article}
\usepackage{xcolor}
\definecolor{shadecolor}{RGB}{150,150,150}
\newcommand{\mybox}[1]{\par\noindent\colorbox{shadecolor}
{\parbox{\dimexpr\textwidth-2\fboxsep\relax}{#1}}}
\begin{document}
\mybox{\textsc{Extra Curricular Achievements}}

\end{document}