在分页符处将 tcolorbox 放入枚举列表内时出现问题:自动、偶数/奇数相关的大小调整失败

在分页符处将 tcolorbox 放入枚举列表内时出现问题:自动、偶数/奇数相关的大小调整失败

我想写一份双面文档,其左右边距不同。其中一个较大,以容纳边注。

我还定义了一个 tcolorbox 环境来强调一些内容。生成的框应该“忽略”较大的边距(因为边距注释不应该出现在里面)并且几乎跨越整个页面宽度。为此,我使用了spread sidewardstcolorbox 键。

它工作正常,除了当我在加载 enumitem 包后将 tcolorbox 放入列表项中时。每当项目超过一页时,似乎奇偶计算都是错误的,并且框看起来好像应该在具有不同奇偶校验的页面上。我怀疑 enumitem 正在对 tcolorbox 用于页面奇偶校验的标签进行某些操作。

这是一个演示该问题的最小示例。请注意,只要 tcolorbox 与 itemize 的开头位于同一页,它就会按预期工作。

\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}

工作箱 盒子故障

toggle left and right=forced我曾尝试摆弄toggle enlargement=forced这个有故障的盒子,但没有帮助。

有什么办法可以解决这个问题吗?

答案1

在我看来,您可以像这样更正密钥:(\Gm@lmargin、\Gm@rmargin 来自几何,但可以用核心计算代替):

\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}

答案2

阅读完 tcolorbox 包代码后,我想我找到了一个可能的解决方案:

\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}

答案3

我可以确认这个问题。它将在下一版本中得到修复。

目前,下列补丁应该可以起作用:

\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

相关内容