12

I want to have a colored box around the text/sentences and its width will be fixed to be 80% of the page width. I tried \fcolorbox but it can't break lines. It is possible to do it with TikZ or is there a handy package available? Thx!

enter image description here

1
  • Have you checked out the posting Colored background in inline listings? The immediate issue raised by the posting is about coloring an inline listing text string, but the solutions provided to that question, which involve the \colorbox command, apply more broadly.
    – Mico
    Dec 27 '13 at 16:04
11

with tcolorbox:

\documentclass{article}
\usepackage{tcolorbox}
\definecolor{myviolet}{rgb}{0.73,0.56,0.64}
\newtcolorbox{mybox}{
    arc=0pt,
    boxrule=0pt,
    colback=myviolet,
    width=.8\textwidth,   % this option controls the width of the box
    colupper=white,
    fontupper=\bfseries
}

\begin{document}
\begin{mybox}
   Example
\end{mybox}

\end{document}

enter image description here

7

It seems that an answer with mdframed is missing. Here it is.

\documentclass{article}

\usepackage[framemethod=TikZ]{mdframed}
\definecolor{myviolet}{rgb}{0.73,0.56,0.64}

\usepackage{lipsum}

\newmdenv[%
    rightmargin=.2\textwidth,
    backgroundcolor=myviolet,
    linewidth=0pt,
    fontcolor=white%
]{mybox}

\begin{document}

\lipsum[1]

\begin{mybox}
Example
\end{mybox}

\end{document}

enter image description here

5

Solution Idea

In order to break lines, we can use minipage environment inside a \colorbox.


The Solution

\documentclass{article}

\usepackage[usenames,dvipsnames]{color}

\usepackage{lipsum}

\begin{document}

\lipsum[1]

\medskip

\colorbox{Lavender}{
  \begin{minipage}[c]{0.8\textwidth}
    \large\color{White}\lipsum[2]    
  \end{minipage}}

\medskip

\lipsum[3]

\end{document}

Output

enter image description here


Limitation

Can not handle page breaks.

2
  • a bit of extra space between the end of the box and the following text would look better. Dec 27 '13 at 16:23
  • @barbarabeeton Thanks for your input. Noticed that, did not bother since the basic solution was there. Updated anyway. You can never ignore the looks :-)
    – Masroor
    Dec 27 '13 at 16:29
4

This seems to give what you want, at least as long as you do not need pagebreaks (for such needs look at the framed or mdframed packages):

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[showframe,noheadfoot,nomarginpar,textwidth=15cm,textheight=23cm]{geometry}
\usepackage[x11names]{xcolor} 

\begin{document}

\noindent\colorbox{Thistle3}{\parbox{\dimexpr 0.8\textwidth -2\fboxsep\relax} {\sffamily\bfseries\color{white}Example.  Example. Example.  Example. Example.  Example.  Example.  Example.}}

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.