文本框延伸至边距

文本框延伸至边距

我想重新创建下图所示的文本框。这是书中的劳德博士的迷宫该框从页面开始一直延伸到边缘。

在此处输入图片描述 如何实现?在下面的 MWE 中,我使用了建议的解决方案这里使用mdframed。我已添加并调整了 和 ,leftmarginrightmargin反复试验以获得类似/可行的结果。但我想添加长度,以便解决方案是通用的,使用textwidthmarginpar。这种方法的另一个问题是框上方和下方有空白空间。这个空间可以用来包装框上方/下方的常规文本吗(我尝试过设置,skipabove=0pt但没有效果)。

在此处输入图片描述

最后,这个解决方案也适用于article类,但是如果你将设置documentclass为,book框将位于主文本宽度内,并且不会延伸到边距。对于偶数页,我希望框位于页面的左侧(如旧书的第一页布局所示),而不是右侧(对于奇数页,这是正确的)。

平均能量损失

\documentclass{article}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{wrapfig}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\newenvironment{WrapText}[1][r]
  {\wrapfigure{#1}{0.3\textwidth}\mdframed[backgroundcolor=gray!20, 
hidealllines=true,leftmargin=0, 
rightmargin=-0.25\textwidth,skipabove=0pt,skipbelow=0pt]}
  {\endmdframed\endwrapfigure}

\begin{document}

\lipsum[1]
\marginpar{This is some margin par}


\begin{WrapText}
The quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog.
\end{WrapText}

\lipsum[1]
\clearpage
\newpage


\lipsum[2]
\marginpar{This is some margin par}

%\hspace{4cm} % this doesnt work
\begin{WrapText}
The quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog. the quick brown fox jumps right over the lazy 
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox 
jumps right over the lazy dog.
\end{WrapText}

\lipsum

\end{document}

在此处输入图片描述

答案1

我建议使用彩色盒子而不是mdframed。该软件包正在积极开发并提供了许多选项。

具体来说,您可以使用以下命令将框放大至边缘grow to right by

\begin{tcolorbox}[grow to right by=\marginparsep+\marginparwidth]
  my box content
\end{tcolorbox}

并使用以下命令处理奇数/偶数页toggle enlargement

\begin{tcolorbox}
  [%
    grow to right by=\marginparsep+\marginparwidth,
    toggle enlargement=evenpage
  ]
  my box content
\end{tcolorbox}

完整示例,将选项twoside传递给article类:

\documentclass[twoside]{article}
\usepackage{lipsum}
\usepackage{showframe}
\usepackage{wrapfig}
\usepackage{lipsum}

\usepackage[many]{tcolorbox}

\newenvironment{WrapText}
{\wrapfigure{o}{0.3\textwidth}
  \begin{tcolorbox}
    [%
      enhanced, boxrule=0pt, frame hidden,
      colback=gray!20,
      sharp corners,
      grow to right by=\marginparsep+\marginparwidth,
      toggle enlargement=evenpage,
    ]
  }
  {\end{tcolorbox}\endwrapfigure}

\begin{document}

\lipsum[1]
\marginpar{This is some margin par}


\begin{WrapText}
The quick brown fox jumps right over the lazy dog. the quick brown fox
jumps right over the lazy dog. the quick brown fox jumps right over the lazy
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox
jumps right over the lazy dog. the quick brown fox jumps right over the lazy
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox
jumps right over the lazy dog.
\end{WrapText}

\lipsum[1]
\clearpage
\newpage


\lipsum[2]
\marginpar{This is some margin par}

%\hspace{4cm} % this doesnt work
\begin{WrapText}
The quick brown fox jumps right over the lazy dog. the quick brown fox
jumps right over the lazy dog. the quick brown fox jumps right over the lazy
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox
jumps right over the lazy dog. the quick brown fox jumps right over the lazy
dog. the quick brown fox jumps right over the lazy dog. the quick brown fox
jumps right over the lazy dog.
\end{WrapText}

\lipsum

\end{document}

tcolorbox 解决方案,奇数页

tcolorbox 解决方案,偶数页

编辑:缩小框前/框后的空间

方框下方和之后的空格由 添加wrapfigure。要删除它们,请使用 和在\vspace方框之前和之后添加负数beforeafter

\newenvironment{WrapText}
{\wrapfigure{o}{0.3\textwidth}
  \begin{tcolorbox}
    [%
      enhanced, boxrule=0pt, frame hidden,
      colback=gray!20,
      sharp corners,
      grow to right by=\marginparsep+\marginparwidth,
      toggle enlargement=evenpage,
      before={\vspace*{-1em}},
      after={\vspace*{-1em}},
      ]
    }
  {\end{tcolorbox}\endwrapfigure}

相关内容