# What is the \rule equivalent to \hrule?

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?

-
prepend a \noindent. – Yiannis Lazarides May 1 '11 at 22:14

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}

-
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
@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

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}

\begin{document}

\noindent\hrulefill

\noindent\varhrulefill

\noindent\varhrulefill[0.4mm]

\noindent\varhrulefill[1mm]

\end{document}


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

-

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]{{%
}}
\newcommand{\xfilll}[2][1ex]{%
}


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

\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]{{%
}}
\newcommand{\xfilll}[2][1ex]{%
}

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

-