119

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.

Rico
  • 6,097
sairajat
  • 1,345

3 Answers3

249

I know this question has been answered very extensively. But not what I wanted or thought was the question based on the title. Therefore if others get in here looking for a possibility for colouring behind a word than this snippet is much easier:

\colorbox{blue!30}{blue}

or

\textcolor{blue!30}{blue}

resulting in:

Color result of the above commands

This is possible by only adding \usepackage{xcolor}. Just some extra info :)

Colour Several Lines It is correct that the above methods does not work for several lines, if you to be more than one line you can do:

{\color{green} the text you want to write}

This can however also be wrapped in a function so it is easier to use several places during edits, e.g., for colouring new text or whatever:

\newcommand{\added}[1]{{\color{green}[added]:#1}}

JTIM
  • 2,869
  • 30
    text inside \colorbox can not be split into several lines if it is too long. So the macro is most suited for coloring a few words. If you want to color several sentences which spread across several lines, it is better to use soul – jdhao Nov 22 '16 at 12:31
  • @Hao if you have a minimal example with the usings, post it or edit the answer that would be cool! :) – JTIM Nov 22 '16 at 14:02
  • 4
    The given link have some illustrating examples about how to use the soul package. – jdhao Nov 22 '16 at 14:18
  • 3
    sorry for off-topic, but hard to resist: I like how LaTeX is not easy at all -- you need to use soul to put some colour behind some text. – xealits Jan 26 '17 at 15:34
  • 3
    Exactly what I needed, no immense amount of packages, no huge newcommands, simply the colorbox. Thanks mate! – Dennis May 12 '17 at 13:33
  • 17
    This example would have taken me fewer seconds to parse if you replaced the text "{blue}" with some other text. :) – CPBL Nov 09 '17 at 02:46
  • @CPBL it has to be an extra challenge :D – JTIM Nov 09 '17 at 09:23
  • 2
    What do the ! and 30 represent? – mpr Apr 09 '18 at 22:08
  • 3
    @mpr it is a fraction of the colour. – JTIM Apr 11 '18 at 07:30
  • @jdhao added method for colouring several lines without using soul, had forgot it when you wrote :D – JTIM Apr 11 '18 at 07:35
  • @xealits you do not need to use soul – JTIM Apr 11 '18 at 07:36
  • 1
    @JTIM, the \color{} command will only color the text, not the text background. – jdhao Apr 11 '18 at 09:09
  • @jdhao The link you posted about soulgives me the impression you can not have different background colours in the same document. Is that correct? – Lars Abrahamsson Jul 28 '18 at 19:38
  • @LarsAbrahamsson, It is possible. You can use groups to achieve what you want (in the group, use \sethlcolor to change color). – jdhao Sep 05 '18 at 03:56
  • Bare in mind that the \hl command does not accept diacritics by default (see the documentation, section 5.1). – Olivier Feb 05 '23 at 14:13
  • Plus, the thing with the soul package is that the margin around the text is minimal. In order to get something similar to the xcolor boxes, there is a way to add more top and bottom margin, redefining the correct leaders, like with \def\SOUL@ulleaders{\leaders\hrule\@depth 1ex\@height 2.5ex\relax}, based on this answer. Nevertheless, I cannot find how to add left and right margin to it. – Olivier Feb 05 '23 at 15:26
58

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}

Dai Bowen
  • 6,117
8

Just to add to the last comment. One can use the `minipage' environment to extend over several lines:

\colorbox{blue!10}{
\begin{minipage}{\textwidth}
\color{RoyalBlue} 

One line.

Another line.

The final line.
\end{minipage}
}
Zarko
  • 296,517