我有一个环境,其中左跳过和右跳过设置为相同的值。在其中我有一个文本和该文本下方的规则。我希望规则具有确切地与其上方的文本宽度相同。(与 HTML 中水平规则位于固定宽度的 div 内时的情况类似;规则应扩展到该 div 的宽度,因此与文本宽度相同。)
我最接近的解决方案是:
\documentclass[final,11pt]{book}
\newenvironment{myenv}{%
\par%
\addtolength{\leftskip}{3em}%
\addtolength{\rightskip}{3em}%
\vspace{1em}%
}{%
\par%
\addtolength{\leftskip}{-3em}%
\addtolength{\rightskip}{-3em}%
\vspace{1.5em}%
}
\begin{document}
\noindent\begin{myenv}
\noindent Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo beatae, cumque omnis pariatur. Nam, unde sunt exercitationem odit vitae suscipit dolor libero autem cum aliquam, dolores ipsam omnis nulla distinctio, voluptas quae tenetur temporibus repellat praesentium! Obcaecati veritatis, harum, voluptate sequi ducimus fugit mollitia, autem dolor facere enim, sit deleniti!
\centerline{\rule{4.1in}{0.2pt}}
\end{myenv}
\end{document}
结果是:
据我所知,规则比左侧的文本块更宽,而右侧的文本块略窄。我怎样才能让此规则占据环境宽度的 100%(即:考虑跳过),以便其宽度与上方的文本完全相同?理想情况下,解决方案不需要手动设置值,而是使用类似\textwidth
或\linewidth
(目前无法产生预期结果)的方法。
答案1
您不应该使用\leftskip
和\rightskip
,而应该使用list
环境:
\documentclass[final,11pt]{book}
\usepackage{lipsum} % for context
\newenvironment{myenv}
{\par\setlength{\topskip}{1em}%
\begin{list}{}{%
\setlength{\leftmargin}{3em}%
\setlength{\rightmargin}{3em}%
}\item\relax}
{\par\nopagebreak\rule{\linewidth}{0.4pt}\end{list}}
\begin{document}
\lipsum[2]
\begin{myenv}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo beatae, cumque omnis pariatur. Nam,
unde sunt exercitationem odit vitae suscipit dolor libero autem cum aliquam, dolores ipsam omnis nulla
distinctio, voluptas quae tenetur temporibus repellat praesentium! Obcaecati veritatis, harum, voluptate
sequi ducimus fugit mollitia, autem dolor facere enim, sit deleniti!
\end{myenv}
\lipsum[3]
\end{document}
您可以轻松调整规则周围的间距。如果您想将其移近文本,请在 后立即使用负数\vspace
(例如\vspace{-1ex}
)\nopagebreak
。
答案2
也许你可以使用tcolorbox
:
\documentclass[final,11pt]{book}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\newenvironment{myenv}{%
\par%
\addtolength{\leftskip}{3em}%
\addtolength{\rightskip}{3em}%
\vspace{1em}%
}{%
\par%
\addtolength{\leftskip}{-3em}%
\addtolength{\rightskip}{-3em}%
\vspace{1.5em}%
}
\newtcolorbox{mybox}{empty, leftright skip=3em, boxsep=0pt, left = 0pt, right=0pt, leftrule=0pt, rightrule=0pt, boxrule=0pt, overlay unbroken={\draw (frame.south west)--(frame.south east);}}
\begin{document}
\noindent\lipsum[1]
\noindent\begin{myenv}
\noindent Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo beatae, cumque omnis pariatur. Nam, unde sunt exercitationem odit vitae suscipit dolor libero autem cum aliquam, dolores ipsam omnis nulla distinctio, voluptas quae tenetur temporibus repellat praesentium! Obcaecati veritatis, harum, voluptate sequi ducimus fugit mollitia, autem dolor facere enim, sit deleniti!
\centerline{\rule{4.1in}{0.2pt}}
\end{myenv}
\begin{mybox}
\noindent Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo beatae, cumque omnis pariatur. Nam, unde sunt exercitationem odit vitae suscipit dolor libero autem cum aliquam, dolores ipsam omnis nulla distinctio, voluptas quae tenetur temporibus repellat praesentium! Obcaecati veritatis, harum, voluptate sequi ducimus fugit mollitia, autem dolor facere enim, sit deleniti!
\end{mybox}
\end{document}
答案3
下面是可以生成您想要的结果的代码:
\documentclass[final,11pt]{book}
\newenvironment{myenv}{%
\par%
\addtolength{\leftskip}{3em}%
\addtolength{\rightskip}{3em}%
\vspace{1em}%
}{%
\par%
\addtolength{\leftskip}{-3em}%
\addtolength{\rightskip}{-3em}%
\vspace{1.5em}%
}
\newcommand\myenvrule{\noindent\rule{\dimexpr\linewidth-\leftskip-\rightskip\relax}{0.2pt}}
\begin{document}
\noindent\begin{myenv}
\noindent Lorem ipsum dolor sit amet, consectetur adipisicing elit. Explicabo beatae, cumque omnis pariatur. Nam, unde sunt exercitationem odit vitae suscipit dolor libero autem cum aliquam, dolores ipsam omnis nulla distinctio, voluptas quae tenetur temporibus repellat praesentium! Obcaecati veritatis, harum, voluptate sequi ducimus fugit mollitia, autem dolor facere enim, sit deleniti!
\myenvrule
\end{myenv}
\end{document}