# How to colour \hrule

I am trying to display a coloured \hrule.

This discussion implies wrapping the \hrule in a \textcolor should do, but that adds an ugly extra vertical space above the \hrule.

Other questions on TeX.SE, such as this, or this, deal with coloring \rule rather than \hrule, but \rule already produces that extra vertical space even without \textcolor.

MWE with the original \hrule and two unsuccessful attempts to colour it:

\documentclass{article}

\setlength{\parindent}{0cm}

\usepackage{xcolor}

\begin{document}

abc%
\vskip 1mm%
\hrule

abc%
\vskip 1mm%
\textcolor{red}{\hrule}

abc%
\vskip 1mm%
\rule{\textwidth}{.1mm}

\end{document}


This produces:

I know I could probably use the hack of inserting a negative \vskip to remove a \baselineskip offset, but I am looking for a clean way of colouring a \hrule that does not involve such a workaround.

Try {\color{red}\hrule} -- you need the {...} around \color etc. to prevent the color leaking into other parts of the document.

\documentclass{article}

\setlength{\parindent}{0cm}

\usepackage{xcolor}

\begin{document}

abc%
\vskip 1mm%
\hrule

abc%
\vskip 1mm%
{\color{red}\hrule}
%\textcolor{red}{\hrule}

abc%
\vskip 1mm%
\rule{\textwidth}{.1mm}

\end{document}


Edit Here's some improvement

\documentclass{article}

\setlength{\parindent}{0cm}

\usepackage{pgffor}
\usepackage{xparse}
\usepackage[x11names]{xcolor}

\NewDocumentCommand{\colorrule}{O{.4pt}m}{%
{\color{#2}\hrule height#1}%
}

\begin{document}

abc%
\vskip 1mm%
\hrule

% Now a blue rule
\colorrule{blue}
abc%
\vskip 1mm%
\foreach \x in {Red4,Red3,Red2,Red1,orange,yellow,LightBlue1,LightBlue2,LightBlue3,LightBlue4,blue,green,brown} {%
\colorrule[2pt]{\x}
}

abc%
\vskip 1mm%
\rule{\textwidth}{.1mm}

\end{document}