3

I want to write a two-sided document with different left an right margins. One of them is larger to accommodate for margin notes.

I also have defined a tcolorbox environment to emphasize some content. The resulting box is supposed to "ignore" the larger margin (as margin notes are not supposed to happen inside) and nearly span the whole page width. For that purpose, I use the spread sidewards key of tcolorbox.

It works fine, except when I put a tcolorbox inside a list item, after loading the enumitem package. Whenever the item runs over more than one page, it seems that the even/odd calculation is wrong, and the box appears as if it should have been on a page with a different parity. I suspect enumitem is doing something with the labels that tcolorbox is using for page parity check.

Here is a minimal example demonstrating the problem. Observe that as long as the tcolorbox is on the same page as the beginning of the itemize, it works as expected.

\documentclass{article}
\usepackage[a4paper,hmargin={6cm,2cm},twoside]{geometry}
\usepackage{enumitem}
\usepackage{tcolorbox}
\usepackage{lipsum}

\begin{document}
  \begin{itemize}
    \item
    \lipsum[1]
    \begin{tcolorbox}[spread sidewards=-1cm]
      This box should have both its sides at 1cm from the paper sides. It works fine.
    \end{tcolorbox}
    \lipsum[2-6]
    \begin{tcolorbox}[spread sidewards=-1cm]
      This box should also have both its sides at 1cm from the paper sides... but it's wrong!
    \end{tcolorbox}
  \end{itemize}
  \begin{tcolorbox}[spread sidewards=-1cm]
    This box should have both its sides at 1cm from the paper sides. It works fine.
  \end{tcolorbox}
\end{document}

Working box Faulty box

I have tried playing around with toggle left and right=forced and toggle enlargement=forced for the faulty box, but it did not help.

Any idea to fix this?

6
  • looks okay for me. Is your system up-to-date? Jul 31, 2019 at 7:17
  • actually it doesn't look good. The second box is fine, but the first is wrong for me. Jul 31, 2019 at 7:21
  • I have run a few more tests. Actually, it seems to fail on any even page (again, while in a list), and to work on odd ones. I am quite surprised by this, not to mention the fact you are experiencing the opposite! Jul 31, 2019 at 8:47
  • Is your tcolorbox.sty up-to-date? I know why it fails for me, but you seem to have other internal settings. Jul 31, 2019 at 8:52
  • The code didn't use \lipsum[1] in the first item, but \lipsum1. The screenshots were made with \lipsum[1], apparently. It was a markup (Markdown!) problem; I've just fixed this.
    – frougon
    Jul 31, 2019 at 8:59

3 Answers 3

3

You could correct the key imho like this: (\Gm@lmargin, \Gm@rmargin is from geometry, but could be replaced by core calculation):

\documentclass{article}
\usepackage[a4paper,hmargin={6cm,2cm},twoside]{geometry}
\usepackage{tcolorbox}
\usepackage{lipsum}

\makeatletter
\tcbset{%
 spread sidewards/.code=
 {\tcbset{check odd page}%
  \tcbifoddpage
   {\tcbset{grow to left by=\@totalleftmargin+\Gm@lmargin+(#1), grow to right by=\Gm@rmargin+(#1)}}
   {\tcbset{grow to left by=\Gm@rmargin+(#1)+\@totalleftmargin, grow to right by=\Gm@lmargin+(#1)}}
 },
}   
\begin{document}
  \begin{itemize}
    \item
    \lipsum[1]
    \begin{tcolorbox}[spread sidewards=-1cm]
      This box should have both its sides at 1cm from the paper sides. It works fine.
    \end{tcolorbox}
    \lipsum[2-6]
    \begin{tcolorbox}[spread sidewards=-1cm]
      This box should also have both its sides at 1cm from the paper sides... but it's wrong!
    \end{tcolorbox}
  \end{itemize}
  \begin{tcolorbox}[spread sidewards=-1cm]
    This box should have both its sides at 1cm from the paper sides. It works fine.
  \end{tcolorbox}
\end{document}
3
  • It is simpler that way, thank you. Actually, I would suggest separately modifying spread inwards/outwards styles (which are used internally by spread sidewards), in case one want asymetric left/right adjust. Jul 31, 2019 at 14:15
  • Imho the best would be to report this to the tcolorbox maintainer. Simply switching left and right values doesn't work. Jul 31, 2019 at 14:29
  • You are right. For the sake of circular references: github.com/T-F-S/tcolorbox/issues/76 Jul 31, 2019 at 20:12
2

After reading the tcolorbox package code, I think I found a possible solution:

\documentclass{article}

\usepackage[a4paper,hmargin={6cm,2cm},twoside]{geometry}
\usepackage{enumitem}
\usepackage{tcolorbox}
\usepackage{lipsum}

\makeatletter
\newtcolorbox{fullwidth}[1][]{
  check odd page,
  if odd page={%
    grow to left by={\hoffset+1in+\oddsidemargin+\leftskip+\@totalleftmargin-1cm},
    grow to right by={\paperwidth-\hoffset-1in-\oddsidemargin-\leftskip-\textwidth-1cm}
  }{%
    grow to left by={\hoffset+1in+\evensidemargin+\leftskip+\@totalleftmargin-1cm},
    grow to right by={\paperwidth-\hoffset-1in-\evensidemargin-\leftskip-\textwidth-1cm}
  },
  #1
}
\makeatother

\begin{document}
  \begin{fullwidth}
    This box should have both its sides at 1cm from the paper sides. It works fine.
  \end{fullwidth}
  \begin{itemize}
    \item\lipsum[1]
    \begin{fullwidth}
      This box should have both its sides at 1cm from the paper sides. It works fine.
    \end{fullwidth}
    \lipsum[2-6]
    \begin{fullwidth}
      This box should also have both its sides at 1cm from the paper sides... but it's wrong!
    \end{fullwidth}
  \end{itemize}
  \begin{fullwidth}
    This box should have both its sides at 1cm from the paper sides. It works fine.
  \end{fullwidth}
\end{document}
2
  • Seems you forgot \makeatletter and \makeatother. :-)
    – frougon
    Jul 31, 2019 at 12:04
  • Good call! I added it. Jul 31, 2019 at 14:02
2

I can confirm the issue. It will be corrected for the next version.

The following patch should work for the meantime:

\makeatletter
\tcbset{%
  spread inwards/.style={%
    if odd page={grow to left by=1in+\hoffset+\oddsidemargin+\@totalleftmargin+(#1)}%
      {grow to right by=\paperwidth-\textwidth-1in-\hoffset-\evensidemargin+(#1)}%
  },
  spread outwards/.style={%
    if odd page={grow to right by=\paperwidth-\textwidth-1in-\hoffset-\oddsidemargin+(#1)}%
      {grow to left by=1in+\hoffset+\evensidemargin+\@totalleftmargin+(#1)}%
  },
}
\makeatother

You must log in to answer this question.

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