Tcolorbox in LaTeX – Creating a Box Surrounded by Text

adjustboxboxtcolorboxtikz-styles

How is it possible for me to put a box of adjustable size surrounded by a text in a latex document ?

For instance : my inspiration is from the yellow boxes in this image :

enter image description here

\documentclass{article}
\usepackage{tikz, adjustbox}
    
\usepackage[most]{tcolorbox}

\begin{document}
Paragraphe de test pour essayer de placer une boîte 

\tcbset{colback=green!5!white,colframe=green!75!black,fonttitle=\bfseries, width = 5cm}
\begin{tcolorbox}[title = Third box]
Description
\end{tcolorbox}

\end{document}

Thank you for your help,

Best Answer

To wrap text around a tcolorbox you can simply put it inside a wrapfigure, using the wrapfig package. To make the tcolorbox smaller than the textwidth, simply use the option width=. Here is an example with a box similar to the yellow in the provided picture:

\documentclass{article}
\usepackage{tikz, adjustbox}
\usepackage[most]{tcolorbox}
% new packages:
\usepackage{xcolor}
\usepackage{wrapfig}
\usepackage{lipsum}
\definecolor{BgYellow}{HTML}{FFF59C}
\definecolor{FrameYellow}{HTML}{F7A600}

\newtcolorbox{mybox2}[1][]{enhanced,
before skip=2mm,after skip=2mm,
width=0.5\textwidth,
halign=center,
colback=BgYellow,colframe=FrameYellow,boxrule=0.2mm,
attach boxed title to top left={xshift=0cm,yshift*=0mm-\tcboxedtitleheight},
varwidth boxed title*=-3cm,
boxed title style={frame code={
\path[left color=FrameYellow,right color=FrameYellow,
middle color=FrameYellow]
([xshift=-0mm]frame.north west) -- ([xshift=0mm]frame.north east)
[rounded corners=0mm]-- ([xshift=0mm,yshift=0mm]frame.north east)
-- (frame.south east) -- (frame.south west)
-- ([xshift=0mm,yshift=0mm]frame.north west)
[sharp corners]-- cycle;
},interior engine=empty,
},
  sharp corners,rounded corners=southeast,arc is angular,arc=3mm,
  underlay={%
    \path[fill=BgYellow!80!black] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
    \path[draw=FrameYellow,shorten <=-0.05mm,shorten >=-0.05mm,color=FrameYellow] ([yshift=3mm]interior.south east)--++(-0.4,-0.1)--++(0.1,-0.2);
    },
  drop fuzzy shadow,
fonttitle=\bfseries,
title={#1}}

\begin{document}
Paragraphe de test pour essayer de placer une boîte 


\begin{wrapfigure}{R}{0.6\textwidth}
\begin{mybox2}[Third box]
Description
\end{mybox2}
\end{wrapfigure}
\lipsum[1-1]

\end{document}

This produces the output: enter image description here

You can change the box to the left side using {L} instead of {R} in \begin{wrapfigure}{R}{0.6\textwidth}(NB make sure the wrapfig is wider than the box!). You can also change the width of the box in the third line in the code (width=0.5\textwidth).