Horizonal Rules in LaTeX

by Michael Szul on

No ads, no tracking, and no data collection. Enjoy this article? Buy us a ☕.

As mentioned in the previous post, while formatting the chatbot book that I've been writing, there were a handful of quirks and configurations that I've had to wrestle with. We talked about custom block quotes in that post, so in this post, we're going to talk about horizontal rules.

In many technology books, especially programming ones, code is separated from regular text via a line. This line is a horizontal rule that you need to insert while formatting the text. In LaTeX, you accomplish this with the \rule command. The \rule command has two options: length and thickness. In our case, we want the line as long as the text, so we set this to \textwidth:

\rule{\textwidth}
      

For line thickness, we'll add in a small amount:

\rule{\textwidth}{0.4pt}
      

This will give us a thin, basic thickness that looks good in books.

Lastly, we need to pay attention to indentations. If a LaTeX document is using indents, \rule will follow the basic rules of that indention, and you may find that it moves your line farther into the text.

You can eliminate this by using \noindent. Our final command looks like the following:

\noindent\rule{\textwidth}{0.4pt}
      

This will add in a horizontal rule with no indent that is the same width as the text width, and will look pretty good in a print book.