4

I have an environment which left and right skips are set to same value. Inside it I have a text and a rule below that text. I want rule to have exactly same width as text above it. (Similarly to what would happen in HTML when horizontal rule is inside div of fixed width; rule should expand to the width of that div and hence be same width as text.)

The closest I have come to solution:

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

which produces this:

rule

As far as I can see, rule is wider than block of text on the left side and a bit narrower on the right side. How can I make this rule take 100% of the environment width (that is: taking into account skips), so that it is exactly same width as text above it? Ideally, solution would not require setting value manually, but use something like \textwidth or \linewidth (which do not produce desired result right now).

  • 1
    Use \noindent\hrulefill. – Steven B. Segletes Oct 8 '15 at 10:34
  • 1
    You really should not set \leftskip and \rightskip directly in latex (as otehrwise any nested environments such as lists or center etc will do the wrong thing) the environment should be defined as a list (as for example quote is) then latex sets up all the dependent lengths an \linewidth is always the width of the current text block inside the margins. Also \noindent before \begin{myenv} is rather weird (and just produces an anomalous empty paragraph) as the environment starts with \par – David Carlisle Oct 8 '15 at 11:21
  • @DavidCarlisle In my case I do not have any environment nested inside myenv. In that case, is there any side effect of using \leftskip and \rightskip in myenv definition? In other words: can it affect any text outside myenv environment? – Rafal Oct 14 '15 at 9:34
3

You shouldn't use \leftskip and \rightskip, but a list environment:

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

enter image description here

You can easily adjust the spacing around the rule. If you want to move it closer to the text, use a negative \vspace (say, \vspace{-1ex}) immediately after \nopagebreak.

  • @Does your solution have any advantage over \noindent\hrulefill that Steven's suggested? I do not see any side effects of just replacing \centerline{\rule{4.1in}{0.2pt}} with \noindent\hrulefill. – Rafal Oct 14 '15 at 9:57
  • 1
    @Rafal Surely \centerline is the last thing to consider. With \rule you can control the thickness, for instance, which you can't with \hrulefill – egreg Oct 14 '15 at 10:31
1

May be you could use 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}

enter image description here

1

Here is a code that produces what you want:

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.