Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've tried to put a rule long as the text in this way:

\rule{\textwidth}{0.4mm}

but I obtain a rule slightly smaller that the text width. How to obtain the same effect as \hrule?

share|improve this question
5  
prepend a \noindent. –  Yiannis Lazarides May 1 '11 at 22:14
add comment

3 Answers

up vote 22 down vote accepted

According to p. 221 of the TeXbook, the height of \hrule is 0.4pt.

\documentclass{article}

\begin{document}

\hrule

\bigskip

\noindent\rule{\textwidth}{0.4mm}

\noindent\rule{\textwidth}{0.4pt}

\end{document}
share|improve this answer
    
the \noindent let me obtain the same effect as \hrule, why I have to \noindent? There are particular reasons? –  pygabriel May 1 '11 at 22:21
12  
@pygabriel: \rule starts with \leavevmode which will start a new paragraph and insert the indent if TeX is between paragraphs. Also \hrule and \noindent\rule{\textwidth}{0.4pt} are not equivalent, as the former won't add interline glue before and after it (and \hrule always ends a paragraph). –  egreg May 1 '11 at 22:39
    
@egreg awesome answer! Thank you! –  pygabriel May 2 '11 at 6:03
add comment

Herbert's xhfill package "is a package for extended hfillrules." It provides

  • \xhrulefill: Modified dimensions for \hrulefill
  • \xrfill: Coloured horizontal rules
  • \xdotfill: Dotted \hrulefill
  • \xhrectanglefill: Rectangular fills

and in the documentation also defines:

\newcommand{\xfill}[2][1ex]{{%
  \dimen0=#2\advance\dimen0 by #1
  \leaders\hrule height \dimen0 depth -#1\hfill%
}}
\newcommand{\xfilll}[2][1ex]{%
  \dimen0=#2\advance\dimen0 by #1%
  \leaders\hrule height \dimen0 depth -#1\hfill%
}

The following, taken partially from the documentation, is an example of the rules generated:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
%\usepackage{xcolor}% http://ctan.org/pkg/xcolor | Loaded by listings
\usepackage{xhfill}% http://ctan.org/pkg/xhfill

\setlength{\parindent}{0pt}% Just for this example
\newcommand{\xfill}[2][1ex]{{%
  \dimen0=#2\advance\dimen0 by #1
  \leaders\hrule height \dimen0 depth -#1\hfill%
}}
\newcommand{\xfilll}[2][1ex]{%
  \dimen0=#2\advance\dimen0 by #1%
  \leaders\hrule height \dimen0 depth -#1\hfill%
}

\begin{document}
blah\xfilll{1pt}blub
\begin{lstlisting}
blah\xfilll{1pt}blub
\end{lstlisting}
blah\xfilll{1pt}blub
\begin{lstlisting}
blah\xfilll{1pt}blub
\end{lstlisting}

blah\xfilll[0pt]{4pt}blub
\begin{lstlisting}
blah\xfilll[0pt]{4pt}blub
\end{lstlisting}

blah\xfilll[-12pt]{12pt}blub
\begin{lstlisting}
blah\xfilll[-12pt]{12pt}blub
\end{lstlisting}

blah\xrfill{1pt}[blue]blub blah\xrfill{2pt}[cyan]blub
\begin{lstlisting}
blah\xrfill{1pt}[blue]blub blah\xrfill{2pt}[cyan]blub
\end{lstlisting}

laber\xrfill[0pt]{4pt}[green]blub blub
\begin{lstlisting}
laber\xrfill[0pt]{4pt}[green]blub blub
\end{lstlisting}

blah\xrfill[-1ex]{1pt}[red]blub
\begin{lstlisting}
blah\xrfill[-1ex]{1pt}[red]blub
\end{lstlisting}

blah \xhrulefill{cyan}{1cm} blub
\begin{lstlisting}
blah \xhrulefill{cyan}{1cm} blub
\end{lstlisting}

blah \xhrectanglefill{0.5cm}{1pt} blubber
\begin{lstlisting}
blah \xhrectanglefill{0.5cm}{1pt} blubber
\end{lstlisting}

blah\xdotfill{1pt}[blue]blah\xdotfill{2pt}[red]blub
\begin{lstlisting}
blah\xdotfill{1pt}[blue]blah\xdotfill{2pt}[red]blub
\end{lstlisting}
\end{document}

Although these rules are all constructed as leaders, and therefore span the entire width in a "fill" manner, they can also be made to be of a specific length.

share|improve this answer
add comment

You can use \hrulefill instead. If you want to have an equivalent to \hrulefill but with a variable height, you can use something like the following:

\documentclass{article}

\newcommand*\varhrulefill[1][0.4pt]{\leavevmode\leaders\hrule height#1\hfill\kern0pt}

\begin{document}

\noindent\hrulefill

\noindent\varhrulefill

\noindent\varhrulefill[0.4mm]

\noindent\varhrulefill[1mm]

\end{document}

variable hrule fill example

EDIT: I modified the definition of \varhrulefill to avoid the use of @.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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