我正在尝试放置一个延伸整个页面宽度且其高度取决于所包含环境的高度的背景。例如,以下代码提供了我正在寻找的内容:
\documentclass[letterpaper]{article}
\usepackage{xcolor}
\usepackage{amsmath,amssymb,calc}
\usepackage{framed}
\setlength{\parindent}{0pt}
\newenvironment{test}[1]{%
\def\FrameCommand{\fboxsep=\FrameSep\colorbox{green!15}}%
\MakeFramed {\advance\hsize -\width \FrameRestore}}%
{\endMakeFramed}
\begin{document}
\begin{test}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu-
lum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris.
Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et ne-
tus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus
rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu
tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
\end{test}
\end{document}
但当然,这个框架只延伸到文本宽度。我想覆盖两侧的整个边距,同时保持 内的任何环境\textwidth
。我正在考虑使用TikZ
或其附属包。我真的很感激在包、包或包background
范围内的解决方案。framed
mdframed
TikZ
background
答案1
使用mdframed
并给予足够宽的边距,这样即使在双面打印时也能起作用:
\documentclass[letterpaper]{article}
\usepackage{xcolor}
\usepackage{mdframed}
\setlength{\parindent}{0pt}
\usepackage{lipsum}
\newmdenv[skipabove=\topsep,skipbelow=\topsep,
leftmargin=-10cm,rightmargin=-10cm,
innerleftmargin=10cm,innerrightmargin=10cm,
innertopmargin=\fboxsep,innerbottommargin=\fboxsep,
backgroundcolor=green!15,
hidealllines=true,
]{test}
\begin{document}
\lipsum[2]
\begin{test}
\lipsum[1]
\end{test}
\lipsum[3]
\end{document}
答案2
经咨询包的作者后framed
,以下代码生成了所需的结果:
\documentclass[letterpaper]{article}
\usepackage{xcolor}
\usepackage{framed}
\setlength{\parindent}{0pt}
\usepackage{lipsum}
\newenvironment{test}[1][green!15]{%
\def\FrameCommand##1{%
\setlength\leftmargin{1in+
\ifodd\value{page}\oddsidemargin\else\evensidemargin\fi}%
\setlength\rightmargin{\paperwidth-\textwidth-\leftmargin}%
%
\hspace{-\leftmargin}%
\colorbox{#1}{%
\hspace{-\fboxsep}%
\hspace{\leftmargin}%
##1%
\hspace{\rightmargin}%
\hspace{-\fboxsep}%
}%
\hspace{-\rightmargin}%
}
\MakeFramed {\advance\hsize -\width \FrameRestore}}%
{\endMakeFramed}
\begin{document}
\begin{test}
\lipsum[1-2]
\end{test}
\end{document}