Horizontal Line Options in LaTeX

Techwalla may earn compensation through affiliate links in this story.
LaTeX offers a lot of ways to typeset and format a document.
Image Credit: Nattakorn Maneerat/iStock/GettyImages

LaTeX offers a wealth of possibilities when it comes to typesetting and formatting a document. It is especially useful when you're dealing with math, but it also boasts applications far beyond that. The price you pay in comparison to other word processing and typesetting software is for the increased complexity, but if you want your document to look the best it can, LaTeX more than worth it. As long as you're willing to learn a little when you want to do something like draw a horizontal line in LaTeX, you can do pretty much anything you need to with the system.


\rule Command in LaTeX

When you want to create a horizontal line in LaTeX of any length and thickness, the \rule command is the easiest one to use. Simply type \rule{length}{thickness} with your chosen line length and thickness in the place of the two placeholder words in the example. You need to specify the unit you're using when you do this (for example, cm or pt), but if you do that, you can create a horizontal line to suit your purposes using this command.

If you want the line raised above (or lowered below) the neutral location, you can include an optional argument in square brackets right after the "\rule" for the "raise-height." To increase the vertical position, use a positive value; to decrease it, use a negative one. For this and the main length and thickness arguments of the \rule command, it's best to try out a few options and see which is best for your purposes.


\hline in LaTeX

The \hline command is often said to be the simplest way to create a horizontal line, and in a sense, this is certainly true because you enter \hline with no need for arguments or anything else. However, this command specifically relates to tables in LaTeX, so you can only use it as part of a table. The command draws a line that is the same width as the table.

You can also enter two successive \hline commands if you want two lines with a small space separating them.


\line Command in LaTeX

The \line command is more versatile than the previous options in that you can set a slope for the line you draw, and with a specific choice, you can use it to draw horizontal lines too. Type \line(x-slope,y-slope){length} with the x-slope, y-slope and length you want in place of the placeholder words. For a horizontal line, you want "(1,0)" for the first part, so you get a line with a unit slope in the x-direction and no slope whatsoever in the y-direction. Again, make sure you include units for the length so that LaTeX knows what you want.


\hrulefill Command in LaTeX

The LaTeX hrule command that's the closest thing to a horizontal line is \hrulefill. This command produces a line with "rubber length," which means that the length varies depending on what it shares a line with. This is particularly useful for contents pages, where you may want the title for a chapter and the page on the same line but separated by a horizontal line and so the page numbers are right-aligned in the final document. This is ideal for filling the variable lengths caused by chapter titles without having to fine-tune the length using the rule command, for instance.


\dotfill Command in LaTeX

If you want the same functionality as \hrulefill except with dots instead of a straight horizontal line, you can use \dotfill. This works in exactly the same way in that the length is variable. Some people prefer this style for contents pages.