10

I have a picture that I would put in a tcolorbox, but the image is too large and comes out.

The code:

\documentclass{report}

\usepackage{geometry}
\usepackage{float}
\usepackage[italian]{babel}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{wrapfig}

\tcbset{enhanced,colback=cyan!5!white,colframe=cyan!75!black,fonttitle=\bfseries}

\begin{document}

\begin{tcolorbox}[every float=\centering, drop shadow, title=Esempio: misura della carica elettrica]
    \begin{wrapfigure}{r}{0.5\textwidth}
        \begin{center}
            \includegraphics[width=0.48\textwidth]{fig1}
        \end{center}
        \label{fig:fig1}
        \caption{Misura della carica elettrica}
    \end{wrapfigure}
    [Some text]
\end{tcolorbox}

\end{document}

The result: enter image description here

I would like the box would fit the figure What could I do? Thanks!

  • 1
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Mar 13 '15 at 11:56
  • 1
    Why wrapfigure here? The code you provided has no text and I believe, that wrapfigure and tcolorbox do not behave well together – user31729 Mar 13 '15 at 11:59
  • The wrapfigure is used to surrounding the image with the text – Andrea Rosa Mar 13 '15 at 12:03
  • Yes, you have right, I have cutted it now for posting the code here. The text I need to insert is the same as showed in the screenshot. – Andrea Rosa Mar 13 '15 at 12:07
  • 1
    I've deleted my answer since it's of no use to the wrongly used wrapfig – user31729 Mar 13 '15 at 13:52
11

Use two minipages and load the capt-of package to write the caption through the command \captionof

\documentclass[demo]{report}

\usepackage{blindtext}
\usepackage{geometry}
\usepackage{float}
\usepackage[italian]{babel}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{capt-of}

\tcbset{enhanced,colback=cyan!5!white,colframe=cyan!75!black,fonttitle=\bfseries}

\begin{document}

\begin{tcolorbox}[every float=\centering, drop shadow, title=Esempio: misura della carica elettrica]
    \begin{minipage}[t]{0.5\linewidth}
    \vspace*{0pt}
    \blindtext
    \end{minipage}\hfill%
    \begin{minipage}[t]{0.4\linewidth}
    \vspace*{0pt}
        \includegraphics[height=0.6\textheight,width=\linewidth]{fig1}
        \captionof{figure}{Misura della carica elettrica}\label{fig:fig1}
    \end{minipage}
\end{tcolorbox}

\end{document} 

enter image description here

  • @AndreaRosa you're welcome. – karlkoeller Mar 13 '15 at 12:35
  • @PaulGessler The OP was misusing wrapfigure but it's clear from the posted image what he/she was trying to obtain. – karlkoeller Mar 13 '15 at 13:03
20

tcolorbox way of doing this is to add sidebyside,sidebyside align=top,lower separated=false as options to tcolorbox and you don't need minipages.

\documentclass[demo]{report}

\usepackage{blindtext}
\usepackage{geometry}
\usepackage{float}
\usepackage[italian]{babel}
\usepackage[most]{tcolorbox}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{capt-of}
\usepackage[export]{adjustbox}

\tcbset{enhanced,colback=cyan!5!white,colframe=cyan!75!black,fonttitle=\bfseries}

\begin{document}

\begin{tcolorbox}[drop shadow, title=Esempio: misura della carica elettrica,sidebyside,sidebyside align=top,lower separated=false]
    \blindtext
    \tcblower
        \includegraphics[height=0.6\textheight,width=\linewidth,valign=t]{fig1}
        \captionof{figure}{Misura della carica elettrica}\label{fig:fig1}
\end{tcolorbox}

\end{document}

enter image description here

You can change the alignment by sidebyside align=top which takes top, center (default) and bottom values.

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.