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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

To change the shaded color of the \rule{...}{...} command, one solution is

\textcolor‎{‎blue‎}{\rule{2cm}{2cm}}‎

but I want to know how can I directly change the color of this command globally?

share|improve this question
up vote 17 down vote accepted

You could do

\makeatletter
\let\old@rule\@rule
\def\@rule[#1]#2#3{\textcolor{blue}{\old@rule[#1]{#2}{#3}}}
\makeatother

But it may be better to instead define a new \colorrule command and use that instead, as you may find \rule is used in unexpected places so if you redefine it you may be changing more than you want to change.


If you need to override this sometimes it's probably better to use a colour such as rulecolor rather than blue that you can (re)define when needed:

enter image description here

\documentclass{article}

\usepackage[dvipsnames]{color}

\makeatletter
\let\old@rule\@rule
\def\@rule[#1]#2#3{\textcolor{rulecolor}{\old@rule[#1]{#2}{#3}}}
\makeatother

\definecolor{rulecolor}{named}{Blue}

\usepackage{color}

\begin{document}

\rule{1cm}{1cm}

\bigskip

\rule{.1cm}{1cm}

\bigskip

{\definecolor{rulecolor}{named}{Red}\rule{1cm}{1cm}}

\bigskip

\rule{1cm}{1cm}


\end{document}
share|improve this answer
    
Could you please tell me where I can find the original definition of the rule command? I'm just curious about it. – Sisabe Dec 29 '12 at 16:45
    
I got it from latex.ltx \def\rule{\@ifnextchar[\@rule{\@rule[\z@]}} \def\@rule[#1]#2#3{% \leavevmode \hbox{% \setlength\@tempdima{#1}% \setlength\@tempdimb{#2}% \setlength\@tempdimc{#3}% \advance\@tempdimc\@tempdima \vrule\@width\@tempdimb\@height\@tempdimc\@depth-\@tempdima}} – David Carlisle Dec 29 '12 at 17:09
    
So where is the definition of the black color? Why doesn't this definition have any definition for the black color? – Sisabe Dec 29 '12 at 21:16
    
TeX itself has no knowledge of colour, rules and font cgyphs are just black and white, colour is implemented by telling the ps (or pdf or other) driver separate;y to switch the current colour) A TeX rule is not really "black" it is just a rule. – David Carlisle Dec 29 '12 at 22:15
1  
LaTeX is so fiddly sometimes, makes me want to use notepad and hand a report to someone in plain text. – Nicholas Hamilton Dec 30 '12 at 13:39

The easiest alternative is:

    \textcolor{blue}{\rule{2cm}{2cm}}

If you want to define your own command, you can put this in the preamble or latter to change the color as you go.

    \newcommand{\myRule}[3][black]{\textcolor{#1}{\rule{#2}{#3}}}

and use e.g.:

    \myRule{1cm}{1cm}

    \myRule[red]{1cm}{1cm}

    \myRule[blue]{1cm}{1cm}

And if you want to fix the color throughout your document, try:

    \newcommand{\myRule}[3]{
    \textcolor{red}{\rule{#2}{#3}} %change red to blue, green black whatever.
}
share|improve this answer
    
It is better to use \textcolor as in the MWE than \color using \color can badly affect the position of the rule. – David Carlisle Dec 29 '12 at 22:16
    
Yeah I spose. Cheers ill change it. – Nicholas Hamilton Dec 30 '12 at 4:05

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.