1

I have a tcolorbox environment that I use on its own, and sometimes inside a wrapfigure. On its own, it works perfectly - paragraphs are spaced according to my document's parskip and parindent. However, inside wrapfigure those settings seem to be ignored.

Here is a MWE:

% !TEX program = xelatex

\documentclass{scrbook}

\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{tcolorbox}

\setlength{\parskip}{3mm}
\setlength{\parindent}{0pt}

\newtcolorbox[auto counter]{sidebar}[2][]{
    parbox=false, % normal paragraph spacing (unless wrapped!)
    sharp corners,
    colframe=black,
    colback=black!15!white,
    title=#2,
    #1
}

\begin{document}

\begin{sidebar}{Test without wrapping}
    Paragraph 1.

    Paragraph 2.
\end{sidebar}

\begin{wrapfigure}{r}{0.5\linewidth}
    \begin{sidebar}{Test with wrapping and no explicit parksip}
        Paragraph 1.

        Paragraph 2.
    \end{sidebar}
\end{wrapfigure}

\begin{wrapfigure}{r}{0.5\linewidth}
    \begin{sidebar}{Test with wrapping and explicit parskip}
        \setlength{\parskip}{3mm}\setlength{\parindent}{0pt}Paragraph 1.

        Paragraph 2.
    \end{sidebar}
\end{wrapfigure}

\end{document}

Here is the output:

enter image description here

Is there any way I can avoid the need to explicitly specify parskip (and parindent) inside my wrapped tcolorbox?

  • Have you tried the parskip class option? Or you can always add it to the default code executed at the beginning of the boxes using tcolorbox's configuration, can't you? Possibly annoying, but you don't have to do it for every box - only once or once per type of box, depending on your needs, I guess. You could also just box the box, I suspect. Certainly if you boxed first, it should work. But even inside might do. – cfr Feb 24 '18 at 4:36
2

Note that you should use the parskip class option with KOMA classes, rather than parskip or, worse, setting the dimensions manually. If the default parskip isn't appropriate, see KOMA's documentation for the various values you can set it to.

You can restore the required skip using the hooks library. For example,

\documentclass[parskip]{scrbook}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{tcolorbox}
\tcbuselibrary{hooks}
\usepackage{kantlipsum}
\newlength\myskip
\AtBeginDocument{%
  \setlength\myskip{\parskip}%
}
\newtcolorbox[auto counter]{sidebar}[2][]{
  parbox=false, % normal paragraph spacing (unless wrapped!)
  sharp corners,
  colframe=black,
  colback=black!15!white,
  before upper app={\par\parskip=\myskip},
  title=#2,
  #1
}

\begin{document}

\begin{sidebar}{Test without wrapping}
  Paragraph 1.

  Paragraph 2.
\end{sidebar}

\begin{wrapfigure}{r}{0.5\linewidth}
  \begin{sidebar}{Test with wrapping and no explicit parksip}
    Paragraph 1.

    Paragraph 2.
  \end{sidebar}
\end{wrapfigure}
\kant[1]

\begin{wrapfigure}{r}{0.5\linewidth}
  \begin{sidebar}{Test with wrapping and explicit parskip}
    \setlength{\parskip}{3mm}\setlength{\parindent}{0pt}Paragraph 1.

    Paragraph 2.
  \end{sidebar}
\end{wrapfigure}
\kant[2]

\end{document}

restored skip

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.