8

When used without parameters, what varfiable controls the length of \hrule?

I.e., how can I change the default length of \hrule, other than adding parameters to each instance -- say, to affect existing commands defined in a package?

The following example shows that it does not seem to depend on margins or \textwidth:

enter image description here

\documentclass{article}
\usepackage{changepage}
\begin{document}
\hrule
a\dotfill\verb|\textwith|=\the\textwidth
\begin{adjustwidth}{0in}{2in}
\addtolength\textwidth{-5cm}
a\dotfill\verb|\textwith|=\the\textwidth
\hrule
a\dotfill\verb|\textwith|=\the\textwidth
\dotfill
\end{adjustwidth}
a\dotfill\verb|\textwith|=\the\textwidth
\hrule
\end{document}

2 Answers2

13

\hrule is a TeX primitive. On p. 221 of the TeXbook, Knuth says that the width of the rule that is created by \hrule

depends on the context; the rule will extend to the boundary of the smallest box or alignment that encloses it.

Quite frequently, this means that the width of the rule will equal \textwidth, since the box that encases the rule encases the entire textblock.

If you need to set the width explicitly (and want to stick with the \hrule syntax), you should write, say,

\hrule width0.5\textwidth

or

\hrule width5cm
Mico
  • 506,678
  • Yes, I know I can set it on each instance. The question is how can I change the default for some block. It seems that minipage is an option, but is there any simple variable to tweak, like {\somelength=1cm lots of code with many \hrule}? – Alexander Gelbukh Sep 28 '15 at 07:03
  • 1
    @AlexanderGelbukh you can try something like \newlength{\rulewidth} \newcommand\mtrule{\hrule width \rulewidth} \newcommand\somelength[1]{\setlength{\rulewidth}{#1}} {\somelength{1cm} lots of code with many \mtrule} – touhami Sep 28 '15 at 08:17
5

It is a rule in vertical mode. You need a vbox if you want to change the width:

\documentclass{article}
\begin{document}

a\dotfill\verb|\textwith|=\the\textwidth
\hrule

\vspace{5mm}
\begin{minipage}{5cm}
foo\hrule
\end{minipage}

\end{document}

enter image description here

  • So there is no simple variable to treak, other than enclose the whole thing in minipage? – Alexander Gelbukh Sep 28 '15 at 07:00
  • @AlexanderGelbukh - See my answer for a method that sets the width directly. – Mico Sep 28 '15 at 07:03
  • 1
    @AlexanderGelbukh: Instead of \hrule use \rule{<width>}{<height>} or define the width of the \hrule. \rule works in horizontal mode like \hrulefill. –  Sep 28 '15 at 07:04