3

I'm using package "color" to have a green text box with black text, and my code is -

\textbf{\fcolorbox{black}{green!30}{black text on a boxed green background}}

How can I get it to wrap lines please?

If it can't wrap with 'fcolorbox', what command would have the same effect and would wrap please?

boudiccas
  • 319
  • You want to look at the tcolorbox package. – egreg Apr 20 '15 at 18:11
  • If you want this coloured box to wrap inside the text just like other normal text, then you're probably interested in Cool text highlighting ((duplicate)) – Werner Apr 20 '15 at 18:35
  • Thanks @Werner, I knew I answered some Q like this before but couldn't find it through the search. Not that my answer is the accepted one... – Martin Scharrer Apr 20 '15 at 18:45
  • @boudiccas: The soul and ulem packages were written to highlight (underline, background color, etc.) flowing text and might do something similar what you want. Don't think they can create exactly the same style like \fcolorbox, through. – Martin Scharrer Apr 20 '15 at 18:48

2 Answers2

5

I suggest one of my favourite packages: tcolorbox

\documentclass{article}


\usepackage{tcolorbox}
% A new `\tcbox` command



\newtcbox{\highlight}[0]{boxsep=0pt,left=0pt,top=0pt,bottom=0pt,right=0pt,boxrule=0pt,arc=0pt,auto outer arc,colback=green,width=6cm}


\begin{document}

\highlight{black text on a boxed green background}

\begin{tcolorbox}[width=6cm,colback=green]
black text on a boxed green background
\end{tcolorbox}

\begin{tcolorbox}[width=6cm,colback=green,boxrule=5pt]
black text on a boxed green background
\end{tcolorbox}

\begin{tcolorbox}[width=6cm,colback=green,arc=0pt,auto outer arc]
black text on a boxed green background
\end{tcolorbox}


\end{document} 

enter image description here

4

\fcolorbox is like \fbox or \mbox and makes a horizontal (LR) box, so you need

\textbf{\fcolorbox{black}{green!30}{%
   \parbox{3cm}{black text on a boxed green background}}}

replacing 3cm by whatever width you need.

David Carlisle
  • 757,742
  • So I can't use it as an item in a sentence then? Like this In some places you may see a little \textbf{\fcolorbox{black}{green!30}{black text on a boxed green background}} round a word or two, these show that they are a common side-effect, – boudiccas Apr 20 '15 at 18:06
  • @boudiccas no (you could not use \fbox in that way either) if you want coloured highlighting within a paragraph look at ulem or soul packages – David Carlisle Apr 20 '15 at 18:09
  • I'm looking for commands that will give me red, green, blue or yellow boxes, and neither package seem to be able to do it. Any other ideas please? – boudiccas Apr 20 '15 at 18:23