0

The breakable boxes made by tcolorbox play nicely with figures that have been defined before the box but have not been typeset yet. Here's an example:

screenshot

The three figures have been issued before the yellow box began, and have no problem appearing on the first page of the breakable box or the page after that.

My problem is that I have some figures that I want to appear later throughout a multi-page breakable box, so I want to issue them somewhere throughout a box. This does not work – any attempt to define figure inside a tcolorbox gives this error:

Not in outer par mode.

I tried to work around that by wrapping the figure into an \afterpage, to make sure it is processed outside of the tcolorbox context, but this didn't help at all.

Is there any way to insert a figure while a breakable tcolorbox is open so they are typeset at a controllable later point throught the box?


To put things into a minimal example: The questions is how to get something like this working:

\documentclass{article}

\usepackage{lipsum} \usepackage[breakable]{tcolorbox}

\begin{document}

\begin{figure} \centering\Huge This is a figure. \caption{A figure that will work} \end{figure}

\begin{tcolorbox}[breakable] \lipsum[1-20]

\begin{figure}
    \centering\Huge
    This is a figure.
    \caption{A figure that will not work}
\end{figure}

\lipsum[21-30]

\end{tcolorbox}

\end{document}

fefrei
  • 1,339
  • Do not use begin{figure}, just \include and\cptionof` for the captions. – yannisl Mar 03 '24 at 17:09
  • That would simply place the contents of the figure and add a caption line, not let the figure float to the best top or bottom position, right? Also note that I don't want the figure to be rendered inside the box – the screenshot in the question shows how I want this to look, just with the ability to also have figures like this later throughout the box. – fefrei Mar 03 '24 at 18:16
  • A figure in a box is like a bird in a cage. He can not fly. – Fran Mar 03 '24 at 18:17
  • Thanks I see. No you cannot achieve it with tcolorbox. It cannot break at a page, then break to insert an image and then restart again. – yannisl Mar 03 '24 at 18:32
  • Well it can… if the figure is already in the list of to-be-typeset floats. I just can't add a new figure to this list… It feels like the hard part already works and the easy part is impossible. – fefrei Mar 03 '24 at 18:39
  • It might be possible to create a figure insert and add it to \@deferlist without using the float environment. Get an insert from \@freelist, put the figure into the box register, the bit flags into the count register and the total height into the length register. OTOH, see https://tex.stackexchange.com/questions/689529/how-to-let-a-figure-appear-exactly-on-page-16 – John Kormylo Mar 03 '24 at 22:59
  • People are more likely to play with this if you provide the code for them to play. – cfr Mar 04 '24 at 05:24
  • If the background color plus frame is a global feature of the document text, then you can re-define the \output routine which will do this background decoration with \box255. The floats (i.e. \inserts from TeX primitive point of view) will work in this case. – wipet Mar 04 '24 at 07:20
  • @cfr I added a MWE above. – fefrei Mar 04 '24 at 10:58
  • @wipet The frame I want to use is more complicated than that, unfortunately. – fefrei Mar 04 '24 at 10:59
  • @fefrei Regardless of the complexity of decoration it is possible to do it at \output routine level, if you want to have floats (i.e. \inserts) inside decorated text. – wipet Mar 04 '24 at 15:21
  • 1
    @wipet I think the OP wants the floats outside the decorated text. So the background and frame aren't supposed to apply to the floats. – cfr Mar 04 '24 at 21:26

1 Answers1

2

As workaround, you can use the shipout hook to insert some material at the top of the next page, and mimic a figure environment (the caption package provides the \captionof command to display a caption outside a float).

\documentclass{article}

\usepackage{lipsum} \usepackage[breakable]{tcolorbox}

\usepackage{caption} % for \captionof command

\begin{document}

\begin{figure} \centering\Huge This is a figure. \caption{A figure that will work} \end{figure}

\begin{tcolorbox}[breakable] \lipsum[1-20]

\AddToHookNext{shipout}{
  \begin{minipage}{1.0\linewidth}
    \centering\Huge
    This is a figure.
    \captionof{figure}{A figure that will not work}
  \end{minipage}
  \bigskip
}

\lipsum[21-30]

\end{tcolorbox}

\end{document}

But it's not a real float, as you required.

jlab
  • 1,834
  • 1
  • 13