3

I want to be able to do something like:

\hspace{2cm} \hrule

and have LaTeX give me a line that goes from 2cm past the left margin, to the right margin. I've looked around and as a LaTeX newbie it seems to be a non-trivial thing to do.

5

You need to use \hrulefill for the purpose. Please see the three examples contained in the following file.

\documentclass{article}

\begin{document}

Fill the whole horizontal width

\hrulefill

\bigskip 

Fill the whole horizontal width after a cetain space 

\hspace{2cm}\hrulefill

\bigskip 

Fill between two texts

Make a horizontal \hrulefill line from the current indented

\end{document}

The following is the output.

enter image description here

In order to know more about \hrulefill, please see here.

  • All your lines start with the indentation box of width \parindent. – user4686 Sep 15 '13 at 15:05
  • @jfbu If any user does not want that, the solution is very simple, either put a \noindent before each one, or use a global \parindent=0.0mm in the preamble. – Masroor Sep 15 '13 at 17:05
  • I'm going with this answer because it's the simplest one. I didn't realise there was a simple command \hrulefill. Thanks! – nebffa Sep 19 '13 at 6:01
  • @nebffa Thanks, glad to be of assistance. See the above comment from jfbu, and my comment as answer. – Masroor Sep 19 '13 at 8:57
2

Define a command for this; \MyRule has an optional argument allowing you to specify the distance from the left margin (default=0pt, meaning that using just \MyRule will gove you a rule spanning the whole text width):

\documentclass{article}
\usepackage{showframe}%just for the example

\newcommand\MyRule[1][0pt]{\par\noindent\hspace*{#1}\hrulefill\par}

\begin{document}

text
\MyRule[0cm]
\MyRule[2cm]
\MyRule[4in]
\MyRule[0.5\textwidth]

\end{document}

enter image description here

The showframe package was only used to have a visual guideline showing the margins.

2

If you want to emulate the behavior of \hrule (which is a primitive command, to be used with care), you can benefit from another primitive, \moveright:

\documentclass{article}
\usepackage{lipsum}
\newcommand{\movedrule}[1]{%
  \par
  \nointerlineskip
  \moveright#1\vbox{
    \hrule width\dimexpr\linewidth-#1\relax
  }
  \nointerlineskip
}

\begin{document}
\lipsum[2]
\hrule
\lipsum[2]
\movedrule{2cm}
\lipsum[2]
\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.