How to draw a horizontal rule with a colour gradient?
You can control the distance between the gradients and text changing the values 1em
for example. Also, the height for rectangle could be changed 1.5em
.
\documentclass{article}
\usepackage{lipsum}%dummy text only
\usepackage{tikz}
\usetikzlibrary{fadings}
\newcommand{\gradient}[1]{\noindent%
\begin{tikzpicture}
\fill[cyan,path fading=east] (0,1em) rectangle (\linewidth,1.5em);
\node at (.5\linewidth,0) {\bfseries #1};
\fill[cyan,path fading=west] (0,-1em) rectangle (\linewidth,-1.5em);
\end{tikzpicture}%
}
\begin{document}
\lipsum[1]
\gradient{Thermodynamics}
\lipsum[2]
\end{document}
Adapting Sigur's answer to just give a command for the rule itself without the rest of the header, you might try
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newcommand{\tikzrule}[3][]{\tikz{\fill[#1] (0,0) rectangle (#2,#3);}}
\begin{document}
{\centering
\tikzrule[cyan, path fading=east]{\textwidth}{1em}\\[.5em]
\textbf{Thermodynamics}\\[.5em]
\tikzrule[cyan, path fading=west]{\textwidth}{1em}
}
\noindent
\tikzrule{.9\textwidth}{3pt}\\
\rule{.9\textwidth}{3pt}
\end{document}
The command \tikzrule
behaves like the built in \rule
except that it takes an optional argument which is passed as a tikz style. It is, however, a bit less robust than \rule
in terms of what sorts of expressions can be used as its dimensions (e.g., \dimexpr
doesn't work in \tikzrule
).
Note that \tikzrule
itself still works without the fadings
tikzlibrary, although of course you need the library in order to produce the fading.