Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I tried to get a random color with xcolor and tikz:

\documentclass{article}
\usepackage[xetex, rgb]{xcolor}
\usepackage{tikz}

\begin{document}

\pgfmathsetseed{1}
\begin{tikzpicture}
    \xdefinecolor{MyColor}{hsb}{rand, 1.0, 1.0}
    \node[fill=MyColor]{Hello};
\end{tikzpicture}
\end{document}

But this does not work, it reports the following error

Missing number, treated as zero

Does anybody know how to get random colors? thanks.

share|improve this question

2 Answers

up vote 7 down vote accepted
\begin{tikzpicture}
    \pgfmathparse{rnd}
    \xdefinecolor{MyColor}{hsb}{\pgfmathresult, 1.0, 1.0}
    \node[fill=MyColor]{Hello};
\end{tikzpicture}
share|improve this answer

The egre solution gives you a Pseudorandom color.

If you use pdftex or one of his son, I suggest you this solution, where for every compilation you will obtain a different color.

\documentclass{standalone}

\usepackage{tikz}


\begin{document}
\begin{tikzpicture}
    \pgfmathparse{rnd}
    \xdefinecolor{MyColor}{rgb}{\pgfmathresult, 1.0, 1.0}
    \node[fill=MyColor](A) at (0,0){Hello};

     \edef\R{\pdfuniformdeviate 255}
     \edef\G{\pdfuniformdeviate 255}
     \edef\B{\pdfuniformdeviate 255}
     \xdefinecolor{MyColor2}{RGB}{\R,\G,\B}
     \node[fill=MyColor2](A2) at (0,-.5){Hello};
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.