6

I am using tcolorboxes and sometimes want them to be part of a larger paragraph, that is I want the text after the tcolorbox not to be indented. However, both \noindent and \@afterheading show some deficiencies (see comments in MWE).

How can I prevent the indentation following a tcolorbox without these deficiencies? I also don't want to lose the nice spacing provided by default (i.e. by /tcb/beforeafter skip balanced).

\documentclass{article}

\usepackage{tcolorbox}

\makeatletter

% This leads to extra spacing before a heading.
%\AddToHook{env/tcolorbox/after}{\noindent\ignorespaces}

% This leads to reduced spacing before a heading as well as
% not allowing new paragraphs after the environment.
% It also prevents page breaks, which I do not want.
%\AddToHook{env/tcolorbox/after}{\@afterheading}

\begin{document}

\section{Indentation after Boxes}
Here's a box followed by a heading.
\begin{tcolorbox}
  This box is followed by a heading.
\end{tcolorbox}

\subsection{A Heading}
Here's a box that is part of a paragraph
\begin{tcolorbox}
  This box should not lead to indentation of the following line.
\end{tcolorbox}
and should not cause indentation in this line.

And here's a box at the end of its paragraph.
\begin{tcolorbox}
  This box is followed by a new, indented paragraph.
\end{tcolorbox}

This is a new paragraph.

\end{document}

MEW output

  • 2
    Why not just add \noindent manually before the continued line, as in \end{tcolorbox} \noindent and should not cause indentation in this line.? This would be the normal way to address this. – Steven B. Segletes Oct 29 at 12:24
  • @StevenB.Segletes I guess I'm lazy. I'm writing quite a lengthy document with a lot of these boxes.. – schtandard Oct 29 at 18:33
  • Lazy? I resemble that remark! – Steven B. Segletes Oct 29 at 19:23
6

Appending \@endparenv, which is used by every list environment, to /tcb/after seems to work.

Update: For breakable boxes, some experimental attempt is added.

\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{hooks, breakable}

\makeatletter
\tcbset{
  after app={%
    \ifx\tcb@drawcolorbox\tcb@drawcolorbox@breakable
    \else
      % add only when not breakabel
      \@endparenv
    \fi
  }
}

% for breakable
\appto\tcb@use@after@lastbox{\@endparenv\@doendpe}
\makeatother

\begin{document}
\section{Indentation after Boxes}
Here's a box followed by a heading.
\begin{tcolorbox}
  This box is followed by a heading.
\end{tcolorbox}

\subsection{A Heading}
Here's a box that is part of a paragraph
\begin{tcolorbox}[title=Unbreakable]
  This box should not lead to indentation of the following line.
\end{tcolorbox}
and should not cause indentation in this line.

Here's a box that is part of a paragraph
\begin{tcolorbox}[title=Breakable but not broken, breakable]
  This box should not lead to indentation of the following line.
\end{tcolorbox}
and should not cause indentation in this line.

Here's a box that is part of a paragraph
\begin{tcolorbox}[title=Breakable and broken, breakable]
  \lipsum[1-2]
\end{tcolorbox}
and should not cause indentation in this line.

And here's a box at the end of its paragraph.
\begin{tcolorbox}
  This box is followed by a new, indented paragraph.
\end{tcolorbox}

This is a new paragraph.

\end{document}

enter image description here enter image description here

| improve this answer | |
  • Nice! Any idea how to extend this to breakable boxes? – schtandard Oct 29 at 18:17
  • 1
    @schtandard Answer updated. I don't have much confident on the support for breakable boxes, hence use with care and feedback is welcome. – muzimuzhi Z Oct 29 at 18:58
  • 1
    Great! Two notes after some more experimenting: (1) Appending \@endpetrue (instead of \endparenv) seems to be enough and avoids additional breakpoints. (2) Appending to the \end macro also seems to work well, both for breakable and unbreakable boxes. That is, after defining boxed using \newtcolorbox, saying \long\appto\endboxed{\@endpetrue} does the trick. – schtandard Oct 29 at 20:21

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.