Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
add comment

4 Answers

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

share|improve this answer
add comment

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.

share|improve this answer
    
a bit of extra space between the end of the box and the following text would look better. –  barbara beeton 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
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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