6

I am using a horizontal rule to to separate texts, and I would like the rule to start to the left of the current left margin. I am using the following code, but apparently it is not the correct way to do that, as I get the warning: "Overfull \hbox (2.22221pt too wide) in paragraph" What is the correct way?

thanks.

\documentclass{article}
\usepackage{xcolor}
\usepackage[english]{babel}
\usepackage{blindtext}
 \newcommand{\blockline}{\noindent\hspace{-0.05\textwidth}
    \textcolor{orange}{\rule{1.05\textwidth}{5pt}}}
\begin{document}
    \noindent\blindtext

    \blockline

    \noindent\blindtext
\end{document}
dustin
  • 18,617
  • 23
  • 99
  • 204
Andro
  • 1,587

3 Answers3

8

There's a spurious blank space at the end of the first line (which you should comment out). Also, I would use \par at the beginning of the definition so \blockline always ends a paragraph; to prevent the line to be left as a widow, I would use something like\par\nobreak at the end of the definition:

\newcommand{\blockline}{\par\noindent\hspace{-0.05\textwidth}%
    \textcolor{orange}{\rule{1.05\textwidth}{5pt}}\par\nobreak}
Gonzalo Medina
  • 505,128
5

I propose another definition of \blockline that is more complicated, disallows completely a break before the rule and makes easier to control the spacing before and after it:

\newcommand{\blockline}{\par\nobreak % don't break a page here
  \moveleft0.05\textwidth\vbox{% we want the rule to protrude on the left
    \hsize=1.05\textwidth % correct the length
    \kern\the\prevdepth % don't take into account the depth of the preceding line
    \kern3pt % space before the rule
    \color{orange}\hrule height 5pt width\hsize % the rule
    \kern3pt % space after the rule
  }\nointerlineskip % no additional space after the rule
}

In this way we are sure that the baselines of the preceding and following lines are exactly 3+5+3 points apart.

egreg
  • 1,121,712
3

The result here is actually not due to the \hspace being incorrect as you may have thought, but in your code that inserts a spurious space:

\documentclass{article}
\usepackage{xcolor}
\usepackage[english]{babel}
\usepackage{blindtext}
\newcommand{\blockline}{\noindent\hspace{-0.05\textwidth}% <--- note the %
  \textcolor{orange}{\rule{1.05\textwidth}{5pt}}}
\begin{document}
\noindent\blindtext

\blockline

\noindent\blindtext
\end{document}
Werner
  • 603,163
  • That's why it is good practice (and you will probably see it often - not for decorative reasons) in LaTeX to put a % at the end of a line. – Count Zero Sep 26 '11 at 17:21
  • Thanks for replying. What do you mean? This is also I point I don't understand. What is the role of "%" that appear at the end of command lines? and adding that doesn't remove the warning. – Andro Sep 26 '11 at 17:22
  • @Andro: % causes TeX to ignore everything to its right. In your case, it makes TeX ignore the blank spurious space. – Gonzalo Medina Sep 26 '11 at 17:26
  • @Gonzalo: So if I enter a new line in my code in the middle of a command, it would be interpreted as a "space character" before moving to the 2nd line, and I need % to suppress it? – Andro Sep 26 '11 at 17:29
  • @Andro: This is correct. As a precaution to this possibility, many people include % by default as opposed to leaving the end-of-line blank. – Werner Sep 26 '11 at 17:31
  • @Andro: yes. In text mode, n consecutive spaces (for all positive integers n\geq 1) and even a line break will be considered by TeX as one space. – Gonzalo Medina Sep 26 '11 at 17:32
  • @werner: I see. thanks! and why does the "\par\nobreak" help me here? sorry for the novice questions – Andro Sep 26 '11 at 17:33
  • The \par allows you to write \blockline and \blindtext without the visible blank line in your code, since it initiates a new paragraph. \nobreak requests TeX to not break a line of page, since color changes may cause this. – Werner Sep 26 '11 at 17:37