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 created a command for filling a line with a black rectangle. It's called \fullrule, you can find it in the code below.

I have several questions regarding this MWE:

\documentclass[10pt]{article}
\usepackage[a4paper]{geometry}

\newlength{\mylift}
\setlength{\mylift}{-10pt} % MARK2
\addtolength{\mylift}{-1ex}
\addtolength{\mylift}{\baselineskip}

\newcommand{\fullrule}{%
    {\hspace*{0pt}\leaders\hbox{\rule[\mylift]{1pt}{10pt}}\hfill}% % MARK 1
    \hspace*{0pt}}

\begin{document}
aaa

\fullrule

aaa\fullrule{}\\
\fullrule{}\\
\fullrule{}

\fullrule

aaa\fullrule{}aaa

\fullrule{}\\
\fullrule{}aaa\\
\fullrule
\end{document}

This is what it produces:

fullrule

  1. Why is the first line of my document indented? (Okay, this one is the smallest problem.)
  2. I had to add \hspace*{0pt} right before \leaders, and after the "whole \leaders part" to get it work with lines with no leading characters before \fullrule. Why? (I'd like to understand this, because it took a long time to figure out with trying.)
  3. Is there any way to rewrite the \fullrule command to use the actual font size instead of 10pt (see % MARK 1 in the code above), and to calculate the rule's raise using the actual font size (see % MARK 2 in the code above).

Thanks!

share|improve this question
    
Ad 1.: The first line of a paragraph is indented by default, unless the paragraph follows a sectioning command. –  lockstep Dec 3 '11 at 14:09
    
@lockstep Thanks, I've never noticed that. I'm a bit tired after a lot of trying with this MWE. –  szantaii Dec 3 '11 at 14:11
add comment

2 Answers

up vote 6 down vote accepted
\newcommand{\fullrule}{%
  \noindent\null\leaders\hrule height \dimexpr\ht\strutbox-\fboxrule\relax
                          depth  \dimexpr\dp\strutbox-\fboxrule\relax\hfill
  \null}

As lockstep remarks, your \fullrule starts a paragraph, so it's indented.

Let's see what the new definition does:

  1. \noindent starts a paragraph without indentation; if we are already inside a paragraph it does nothing.

  2. \null drops an invisible box that will cause \fullrule not being swallowed at the beginning of a line.

  3. Instead of repeating a box, we use the \leaders\hrule feature of TeX: fill with a rule the space specified at the end (in this case \hfill).

  4. \null will avoid the leaders to be ignored at the end of a paragraph (TeX always does \unskip when it scans the end of a paragraph).

For points 2 and 4 it's important to know that leaders are treated the same as glue, apart from filling the space with something instead of blank space.

The \hrule specification tells TeX that the rule should be as high and deep as a strut (with a small correction that you can adjust yourself); thus the height and depth of the rule will depend on the current baselineskip.

share|improve this answer
add comment

\leaders in vertical mode expect vertical glue. So \leaders with \hfill cannot be used at vertical mode. At the start of a new paragraph you are at vertical mode. So you have to leave vertical mode first. You may do this not only with \leavemode but \hspace* or another invisible element like \null (defined at LaTeX kernel as empty horizontal box: \hbox{}).

\hfill at the end of a line does not fill anything, because TeX removes last horizontal space at the end of paragraphs. You may avoid this using either invisible material again or one more horizontal space (even \hspace without star) or \kern0pt (that's what have been done at the LaTeX kernel in similar cases), e.g. \dotfill or \hrulefill.

To detect the height and depth of a line you may use \ht\strutbox and \dp\strutbox.

Here a suggestion with an adapted version of \hrulefill from LaTeX kernel (only height and depth of the rule has been added):

\newcommand{\fullrule}{%
  \leavevmode
  \leaders\hrule height\ht\strutbox depth\dp\strutbox\hfill\kern0pt}%

This version does not remove the paragraph indent. If you want to remove the paragraph indent, use egreg's solution.

share|improve this answer
1  
Leaders surely can be used also in vertical mode. In this case we want to go into horizontal mode anyway, so \noindent\null is more efficient than doing a conditional. Something is needed after \hfill because of the implicit \unskip issued by \par (this is more TeXnically correct than your statement, but for practical purposes they are the same). –  egreg Dec 3 '11 at 14:39
    
leaders in vertical mode need vertical glue! Using \hfill would result in ! Leaders not followed by proper glue. –  Schweinebacke Dec 3 '11 at 15:41
    
@egreg: Maybe we should remove my answer, because I didn't see your answer before submitting my one. –  Schweinebacke Dec 3 '11 at 16:16
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.