26

I'm new here, so I'm sorry for any mistakes or error in from my part.

I am trying to make colored textboxes that refer to colored blocks in a figure. My problem is that the colored box does not linebreak at the end of the page.

Does anyone know a way around it?

(I don't know how to make the in-post render of the tex-code, so I'll just attach a screenshot of how it looks like.)

\documentclass[a4paper]{article} 
\usepackage{xcolor}

\begin{document}

\colorbox[RGB]{0,209,0}{This block represents a generic three phase back emf, like a power line, as vectors.} \\
\colorbox[RGB]{232,209,82}{This block converts the vectors to three phase representation.} \\
\colorbox[RGB]{255,66,209}{The Modulator that can perform sinus, symmetric or minswitch modulation, with fixed or random carrier wave frequency.} \\
\colorbox[RGB]{97,189,252}{This is an ideal three level inverter.} \\
\colorbox[RGB]{255,128,0}{This is a model of the grid connection and the load. It also calculates how much current is taken from the DC link.}

\end{document}

enter image description here

(I use Texmaker, Miktex, Windows 10)

Torbjørn T.
  • 206,688

6 Answers6

23

You can use soul:

\documentclass[a4paper]{article}
\usepackage{soul}
\usepackage{xcolor}

\newcommand{\ctext}[3][RGB]{%
  \begingroup
  \definecolor{hlcolor}{#1}{#2}\sethlcolor{hlcolor}%
  \hl{#3}%
  \endgroup
}

\begin{document}

\noindent
\ctext[RGB]{0,209,0}{This block represents a generic three phase back emf, like a power line, as vectors.} \\
\ctext[RGB]{232,209,82}{This block converts the vectors to three phase representation.} \\
\ctext[RGB]{255,66,209}{The Modulator that can perform sinus, symmetric or minswitch modulation, with fixed or random carrier wave frequency.} \\
\ctext[RGB]{97,189,252}{This is an ideal three level inverter.} \\
\ctext[RGB]{255,128,0}{This is a model of the grid connection and the load. It also calculates how much current is taken from the DC link.}

\end{document}

enter image description here

egreg
  • 1,121,712
12

colorbox does not wrap automatically, put the text in a \parbox for a example and it will work:

Other ways: Use tcolorbox, for example:

\documentclass[a4paper]{article} 
\usepackage{xcolor}

\usepackage{showframe}
\usepackage[skins]{tcolorbox}

\tcbset{commonstyle/.style={boxrule=0pt,sharp corners,enhanced jigsaw,nobeforeafter,boxsep=0pt,left=\fboxsep,right=\fboxsep}}

\newtcolorbox{mycolorbox}[1][]{commonstyle,#1}


\newlength\myboxwidth

\setlength{\myboxwidth}{\dimexpr\textwidth-2\fboxsep}

\begin{document}
\parindent=0em
\colorbox[RGB]{0,209,0}{This block represents a generic three phase back emf, like a power line, as vectors.} 

\colorbox[RGB]{232,209,82}{This block converts the vectors to three phase representation.} 

\colorbox[RGB]{255,66,209}{The Modulator that can perform sinus, symmetric or minswitch modulation, with fixed or random carrier wave frequency.} 

\colorbox[RGB]{97,189,252}{This is an ideal three level inverter.} 

\colorbox[RGB]{255,128,0}{This is a model of the grid connection and the load. It also calculates how much current is taken from the DC link.}

\begin{center}
With \verb!\parbox!
\end{center}
\colorbox[RGB]{0,209,0}{\parbox{\myboxwidth}{This block represents a generic three phase back emf, like a power line, as vectors.}} 

\colorbox[RGB]{232,209,82}{\parbox{\myboxwidth}{This block converts the vectors to three phase representation.}} 

\colorbox[RGB]{255,66,209}{\parbox{\myboxwidth}{The Modulator that can perform sinus, symmetric or minswitch modulation, with fixed or random carrier wave frequency.}} 

\colorbox[RGB]{97,189,252}{\parbox{\myboxwidth}{This is an ideal three level inverter.}} 

\colorbox[RGB]{255,128,0}{\parbox{\myboxwidth}{This is a model of the grid connection and the load. It also calculates how much current is taken from the DC link.}}

\definecolor{colorone}{RGB}{0,209,0}
\definecolor{colortwo}{RGB}{232,209,82}
\definecolor{colorthree}{RGB}{255,66,209}
\definecolor{colorfour}{RGB}{97,189,252}
\definecolor{colorfive}{RGB}{255,128,0}

\begin{center}
With \texttt{tcolorbox}
\end{center}

\begin{mycolorbox}[colback=colorone]
This block represents a generic three phase back emf, like a power line, as vectors.
\end{mycolorbox}

\begin{mycolorbox}[colback=colortwo]
This block converts the vectors to three phase representation. 
\end{mycolorbox}

\begin{mycolorbox}[colback=colorthree]
The Modulator that can perform sinus, symmetric or minswitch modulation, with fixed or random carrier wave frequency.
\end{mycolorbox}

\begin{mycolorbox}[colback=colorfour]
This is an ideal three level inverter.
\end{mycolorbox}

\begin{mycolorbox}[colback=colorfive]
This is a model of the grid connection and the load. It also calculates how much current is taken from the DC link.
\end{mycolorbox}

\begin{center}
With \texttt{tcolorbox} and shadows.
\end{center}

\begin{mycolorbox}[colback=colorone,drop lifted shadow]
This block represents a generic three phase back emf, like a power line, as vectors.
\end{mycolorbox}

\end{document}

enter image description here

4

If someone is using luatex and stumbling as well into the soul package there is an easy alternative with the package lua-ul

\documentclass[a4paper]{article} 
\usepackage{xcolor} 
\usepackage{luacolor,lua-ul} %for usage of style attributes - background color

\begin{document}

\highLight[{[RGB]{0,209,0}}]{This block represents a generic three phase back emf, like a power line, as vectors.} \\
\highLight[{[RGB]{232,209,82}}]{This block converts the vectors to three phase representation.} \\
\highLight[{[RGB]{255,66,209}}]{The Modulator that can perform sinus, symmetric or minswitch modulation, with fixed or random carrier wave frequency.} \\
\highLight[{[RGB]{97,189,252}}]{This is an ideal three level inverter.} \\
\highLight[{[RGB]{255,128,0}}]{This is a model of the grid connection and the load. It also calculates how much current is taken from the DC link.}

\end{document}

with the same results. I added it for the sake of completness this additional option. results

LeO
  • 1,451
1

Highlighting with the soul package did not work for me (citing, referencing threw errors in the tufte-book class) and I didn't get tcolorbox package to work either. Based on the answer of user31729 here, I worked out his simplified version using parbox:

\documentclass[a4paper]{article} 
\usepackage{xcolor}
\newcommand{\hly}[1]{\colorbox{yellow}{\parbox{\textwidth}{#1}}}

\begin{document}

\hly{This block represents a generic three phase back emf, like a power line, as vectors.}

\end{document}

enter image description here

thomasl
  • 11
1

Maybe you can use \hl of the package soul, no bugs appear such as those using \colorbox.

Here is an example:

\documentclass[11pt]{article}

\usepackage{color,soul}

\begin{document}

\hl{This is a highlight, we can make a very long sentence more than one line, but the highlight result is also very good.}

\end{document}

Here are my compiled results:

enter image description here

enter image description here

0

I personally don't like the accepted answer.

I used to define colorbox as new command \hll to wrap citations and references and used soul's \hl for highlighting normal text.

Now I use this:

\newcommand{\hll}[1]{\colorbox{yellow}{\parbox{\columnwidth}{#1}}}

\parbox is added to split colorbox into multiple lines and this works irrespective if there are citation and references in your text. You do need to specify the width of the paragraph above, this is for two column scientific journal, you can change it to \textwidth for normal page layout.

The only downside to this approach as that it wraps paragraphs a bit odd and does not split nicely across pages, but is simple and very easy to remove, which is not the case when you need to wrap citations and references separately.