0

This is a sequel to this question, which was partially resolved and it was suggested there that I start a new question.

I've created a box with tcolorbox, which should not float and I want to attach a caption which should "stick" in the sense that it should be on the same page as the box.

The solution suggested by Zarko in my previous question does strange things to vertical spacing inside the box, as can be seen in the code below.

I came up with a small workaround in the previous question but the caption doesn't stick. Does anyone have any suggestions for a workaround or fix?

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[skip=1ex]{caption}

\usepackage{tcolorbox}
\tcbuselibrary{most}
\newtcolorbox{abox}[2][]{enhanced, fonttitle=\bfseries, 
                         attach boxed title to top center={yshift=-2mm}, 
                         title={#2},#1}

\usepackage[unicode=true,
            pdfusetitle,
            bookmarks=true,
            bookmarksnumbered=false,
            bookmarksopen=false,
            breaklinks=false,
            pdfborder={0 0 1},
            backref=false,
            colorlinks=false]{hyperref}

\begin{document}

\begin{center}

\begin{abox}{title}
{
{\begin{enumerate}
\item testttttt 
\end{enumerate}
testttttt
\begin{enumerate}\setcounter{enumi}{1} 
    \item testttttt
\end{enumerate}
testttttt
\begin{enumerate}\setcounter{enumi}{2} 
    \item testttttt
\end{enumerate}}
}
\end{abox}
\captionof{figure}{caption}

\end{center}

\end{document}
2

1 Answer 1

2

I am not sure about certain items in your example, so I changed some of it to be able to target the lack of vertical spacing at the first enumerate. This code example shows the correct vertical spacing inside and outside the tcolorbox:

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[skip=1ex]{caption}

\usepackage[most]{tcolorbox}

\newtcolorbox{abox}[1][]{enhanced, fonttitle=\bfseries,
    nofloat,    % added to prevent tcolorbox becoming a float
    attach boxed title to top center={yshift=-2mm}, 
    title=#1}

\usepackage{enumitem} % added to use the resume option

\usepackage{lipsum} % just to insert some random text

\begin{document}
    
    \lipsum[1][1-2]
    
        \begin{abox}[title]
            top text
                \begin{enumerate}[leftmargin=*]
                        \item testttttt 
                \end{enumerate}
            
                    testttttt
            
                \begin{enumerate}[resume*]%\setcounter{enumi}{1} 
                    \item testttttt
                \end{enumerate}
                
                    testttttt
                
                \begin{enumerate}[resume*]%\setcounter{enumi}{2} 
                    \item testttttt
                \end{enumerate}
                    testttttt
        \end{abox}
        \captionof{figure}{caption}
    
    \medskip    
    \lipsum[2][1-2]
    
\end{document}

I made the following changes:

  1. added the enumitem package, so you can use the resume* option in subsequent enumerate occurrences without having to reset the counter
  2. changed the number of variables in your abox to just one for the title. You don't need to define a variable for the content of the box
  3. removed all superfluous brackets as a result of that change
  4. added the option nofloat to your box definition as you clearly stated you don't want the box to be a floating object
  5. removed the center environment as that is no longer required to prevent floating
  6. since the caption is following the box, but not attached to it, you need to add some vertical space to increase the distance between caption and next paragraph. You can't use the option before / after skip balanced from tcolorbox (see pages 86-88 in the manual)

The result of these changes:

vertical spacing correct

As to why the vertical spacing is altered at the first enumerate when embedded in a center environment, I have no clue.

Edited to include options in comments

\documentclass[english]{article}
%\usepackage[T1]{fontenc}
%\usepackage[latin9]{inputenc}
\usepackage{babel}
\usepackage[skip=1ex]{caption}

\usepackage[most]{tcolorbox}

\newtcolorbox{abox}[1][]{enhanced, fonttitle=\bfseries,
    nofloat,    % added to prevent tcolorbox becoming a float
    attach boxed title to top center={yshift=-2mm}, 
    title=#1}

\usepackage{enumitem} % added to use the resume option

\usepackage{lipsum} % just to insert some random text

\begin{document}
    
    \lipsum[1][1-2]
    
        \begin{abox}[title]
            caption inside a parbox
                \begin{enumerate}[leftmargin=*]
                        \item testttttt 
                \end{enumerate}
            
                    testttttt
            
                \begin{enumerate}[resume*]%\setcounter{enumi}{1} 
                    \item testttttt
                \end{enumerate}
                
                    testttttt
                
                \begin{enumerate}[resume*]%\setcounter{enumi}{2} 
                    \item testttttt
                \end{enumerate}
                    testttttt
        \end{abox}
        \noindent \parbox{\linewidth}{\captionof{figure}{caption}}
    
    \medskip    
    \lipsum[2][1-2]
    
    \noindent \begin{minipage}{\linewidth}
    
            \begin{abox}[title]
        boxed inside a minipage
        \begin{enumerate}[leftmargin=*]
            \item testttttt 
        \end{enumerate}
        
        testttttt
        
        \begin{enumerate}[resume*]%\setcounter{enumi}{1} 
            \item testttttt
        \end{enumerate}
        
        testttttt
        
        \begin{enumerate}[resume*]%\setcounter{enumi}{2} 
            \item testttttt
        \end{enumerate}
        testttttt
    \end{abox}
    \captionof{figure}{caption}
    \end{minipage}
    
    \medskip
    \lipsum[4][4-8]
    
\end{document}

This code example shows the correct indention after the use of \captionof:

tcb in minipage

5
  • Thanks for cleaning up my code! The point of the "fix" in the previous question was that center is needed to make sure the text paragraphs following the boxes still indent properly. Removing it reintroduces the problem...
    – user8463
    Commented Mar 30, 2023 at 15:55
  • ... I guess I can wrap the box in a figure environment and turn that into a nofloat version with [H]. Should circumvent the issue, but maybe someone should submit a bug report somewhere...
    – user8463
    Commented Mar 30, 2023 at 16:03
  • Yes I noticed that afterwards. I use captionof a lot too, but never noticed this behaviour. Will start looking whether it occurs in my documents too.
    – alchemist
    Commented Mar 30, 2023 at 16:03
  • Well, I found a workaround for that behaviour. When I use \noindent \parbox{\linewidth}{\captionof{figure}{caption}} the next paragraph is indented correctly.
    – alchemist
    Commented Mar 30, 2023 at 16:33
  • Another option is to include the box plus caption in a minipage environment as the manual of caption suggests on page 16 (refers to another situation, but still). So the code to add would be: \noindent \begin{minipage}{\linewidth} ... box plus caption ... \end{minipage}. This does not ruin vertical spacing inside the box. Added benefit: the caption is now locked to the box.
    – alchemist
    Commented Mar 30, 2023 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .