7

I want to have the text go on the right side of my tcolorbox (like this), instead of it just being white space on the right side of my tcolorbox.

This code illustrates a tcolorbox with all white on the right side:

\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\begin{document}

\chapter{First}
\section{Furst}
\subsection{Ok}

%Defining a newtcolorbox to be a standard box for all examples in the text:
\newtcolorbox[auto counter,number within=section]{phbox}[1][]{skin=bicolor,title=Figure~\thetcbcounter,#1,size=title,colback=white,colbacklower=black!10!white}


Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
\begin{phbox}[label={fig:label},halign=center,width=\linewidth/2]
\tcbincludepdf[scale=1,size=tight,opacityframe=0]{example-image.pdf}
\tcblower
Here is a bunch of text to explain a figure.
\end{phbox}
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere 


\end{document}

I've tried using nobeforeafter and after=, in the tcolorbox options, but this does not produce the desired effect. My best guess is a solution using before and after, I've tried looking in the manual for clues, but I've come up empty.

Do I have to use the wrapfig package to achieve this? I would prefer a way to do it with tcolorbox.

Thanks!

Edit: To clarify: Before using a new package, I wanted to make sure that there wasn't an obvious easy way to do it using tcolorbox. The thing I'm asking for seems like it should be easy to do, maybe it was too obvious for the manual.

I'm already using a lot of tcolorbox in my document, and it's a lot more work having to put stuff in a wrapfigure if I could just put after=\somecommand in the tcolorbox options instead.

Also it's not obvious (to me) that combining wrapfig and tcolorbox would be unproblematic.

  • Isn't the example on page 74, tcolorbox 3.61. documentation something you wan't to achieve? – user31729 Jul 12 '15 at 21:43
  • Given that you found nothing in the manual to suggest the package can do this, why do you nonetheless think the package can do it? Sorry, but this sounds very much like 'I've been through the instructions for the food processor and I don't see any evidence that it can cook food, but do I really need to use a cooker as well? I'd prefer to do everything with the food processor. So, how can I cook food with the food processor?' – cfr Jul 12 '15 at 21:49
  • @ChristianHupfer I take it that the OP doesn't want the text to be within the box. The desiderata is to wrap body text around the tcolorbox. As if with wrapfig. But not only does the OP not want to use wrapfig (for unspecified reasons). The OP wants to use tcolorbox to do this. If that's right, then this is a feature request to the tcolorbox maintainer, IMHO, and so off-topic. – cfr Jul 12 '15 at 21:52
  • @cfr: That example is in the box, true, but I assume this could be modified, with savelowerto or something similar – user31729 Jul 12 '15 at 21:53
  • @ChristianHupfer If it is really just text on the right of the box which is needed, that's true. (Don't even need tcolorbox's facilities for that - a couple of minipages would do it.) But the image the OP linked to shows text flowing around the box and that is different. (Unless you break the text manually into pieces, of course.) – cfr Jul 12 '15 at 21:57
7

I know you asked for a non-wrapfig answer, though it is unclear why. They can work together. More importantly, I think, is that tcolorbox doesn't care much about how things outside the "colorboxes" look or behave, while wrapfig only cares about wrapping things around other things....

\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum, wrapfig}
% change "top" vertical spacing in wrapfig
\setlength{\intextsep}{0pt}
\begin{document}

\chapter{First}
\section{Furst}
\subsection{Ok}

%Defining a newtcolorbox to be a standard box for all examples in the text:
\newtcolorbox[auto counter,number
within=section]{phbox}[1][]{skin=bicolor,
  title=Figure~\thetcbcounter,#1,
  size=title,colback=white,colbacklower=black!10!white}


Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
\begin{wrapfigure}[14]{r}[0pt]{5cm}
\begin{phbox}[label={fig:label},width=\linewidth]
\tcbincludepdf[scale=1,size=tight,opacityframe=0]{example-image.pdf}
\tcblower
Here is a bunch of text to explain a figure.
\end{phbox}
\end{wrapfigure}
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere

\end{document}

wrapfig+tcolorbox

| improve this answer | |
  • 1
    That's the answer I would have given myself :-) – Thomas F. Sturm Jul 13 '15 at 5:59
  • 1
    @ThomasF.Sturm -- Ah, high praise indeed! – jon Jul 14 '15 at 1:05
5

Here is a solution without using the wrapfig package. The idea is to define a macro \newcommand{\MyWrapFigure} with two arguments; the first is the figure and the second is the wrapping text. The macro calculates the dimensions of the figure and indents the text horizontally and vertically, accordingly.

Caution should be taken, however, when using such macros with, for example, \hangindent. It will not work for all situations out of the box. Messing with \hangindent in LaTeX is dangerous: list environments use \parshape that overrides it. You can use \leftskip instead.

The macro used here is by no means complete and can be much improved up on, but, this is at least a first step in getting rid of wrapfig. I hope it helps.

\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum,calc}

\newcommand{\MyWrapFigure}[2]{%
\newlength\picwidth
\newlength\picheight
\settowidth\picwidth{#1}
\settoheight\picheight{#1}
\newcount\countA
\countA=\picheight
\divide\countA by \baselineskip
\advance\countA by 2%
\newlength\heightT
\settoheight\heightT{\mbox{T}}%

% set the picture box
\noindent\raisebox{-\picheight+\heightT-\baselineskip}[0pt][0pt]{#1}%

\setlength{\hangindent}{\picwidth+1em}
\hangafter-\countA
\let\OrigPar=\par% save the original \par command
\edef\par{\hfil\break\indent}
% now place the text
\noindent
#2%
\relax\let\par=\OrigPar\par
}

\begin{document}

\chapter{First}
\section{Furst}
\subsection{Ok}

%Defining a newtcolorbox to be a standard box for all examples in the text:
\newtcolorbox[auto counter,number within=section]{phbox}[1][]{skin=bicolor,title=Figure~\thetcbcounter,#1,size=title,colback=white,colbacklower=black!10!white}

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
\MyWrapFigure{%
\begin{phbox}[label={fig:label},halign=center,width=\linewidth/2]
\tcbincludepdf[scale=1,size=tight,opacityframe=0]{example-image.pdf}
\tcblower
Here is a bunch of text to explain a figure.
\end{phbox}
}
{%
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere 
}

\end{document}

enter image description here

| improve this answer | |
  • Thanks for taking the time to type this out. I should have clarified that I was asking if there is an obvious way to do it with tcolorbox. But since it seems there isn't, I'll just have to use wrapfig. – john.abraham Jul 13 '15 at 9:37

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.