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}
  • @touhami I tried to change \textwidth, \linewidth, and \hsize, but it has no effect on \hrule. – Alexander Gelbukh Sep 28 '15 at 6:58
  • 3
    You can't: \hrule (as explained by Mico) has no default width; either you explicitly specify a width, or the one of the enclosing vertical box will be used. – egreg Sep 28 '15 at 8:29
  • @egreg In fact yours is the best answer: "\hrule has no default width". – Alexander Gelbukh Sep 28 '15 at 9:40
4

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 7:00
  • @AlexanderGelbukh - See my answer for a method that sets the width directly. – Mico Sep 28 '15 at 7:03
  • 1
    @AlexanderGelbukh: Instead of \hrule use \rule{<width>}{<height>} or define the width of the \hrule. \rule works in horizontal mode like \hrulefill. – user2478 Sep 28 '15 at 7:04
12

\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
  • 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 7: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 8:17

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.