1

Is there a single option to set both the draw and the fill color of a TikZ node?

Something like:

\node[rectangle,drawandfill=red]{test};

The actual problem is the following: I have many adiacent rectangles node with different filling colors. Each rectangle has a different filling color.

Now I need to remove the tiny white border around each rectangle and I want that each rectangle has a drawing color equal to its filling color.

Since it would be a pain to rewrite draw=<color name> for every rectangle, I was hoping for a command like fillanddrawn=<color name> so that I could use some search and replace feature to add all the drawing colors at once.

5
  • If they are to be the same color, why not just fill? The only difference will be a 0.4pt (with default line widths) difference in each dimension of the rectangle. Jul 29, 2014 at 14:39
  • yes, that 0.4pt matters, it needs to be the same color of the filling. Anyway I think my question was too generic, I'll rephrase it.
    – JennyC
    Jul 29, 2014 at 14:40
  • \tikzset{draw and fill/.style args={#1 and #2}{draw=#1,fill=#2}} to be used as draw and fill=blue and red. Jul 29, 2014 at 14:45
  • If your editor is a decent one, you can use it to do the search and replace in any case. For example, I'd search on (fill=)([a-zA-Z0-9]*) and replace with \1\2, draw=\2 in Kile. The syntax will vary slightly depending on how your editor implements 'regular expressions' (complex search and replace patterns as opposed to plain words). If your editor can't do this, you are using the wrong editor ;).
    – cfr
    Jul 29, 2014 at 15:04
  • Could you not just replace fill= with fill, draw, throughout if there is not text in the nodes, as you say later?
    – cfr
    Jul 29, 2014 at 15:10

3 Answers 3

9

You can define your own parameterized style to do this, with

\tikzset{drawandfill/.style={draw=#1,fill=#1}}

Complete example:

\documentclass[tikz]{standalone}
\begin{document}
\tikzset{drawandfill/.style={draw=#1,fill=#1}}
\begin{tikzpicture}
  \node[rectangle,drawandfill=red] at (0,0) {test};
  \node[rectangle,fill=red,draw=red] at (1,0) {test};
\end{tikzpicture}
\end{document}

The two nodes are identical:

enter image description here

0
3

I doubt that you want the fill and draw colour to be the same as you won't see anything!

Here's an example (the drawing colour is just give as the node colour and the fill colour is specified with fill=...):

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \node[blue,fill=red, rectangle]at (0,0){test};
  \node[blue,fill=blue, rectangle]at (1,0){test};
\end{tikzpicture}

\end{document}

enter image description here

12
  • sorry, my example is misleading. I have a jpeg into the node and no text.
    – JennyC
    Jul 29, 2014 at 14:38
  • 1
    Also, try draw=red,fill=red and you'll see that the text remains black. Jul 29, 2014 at 14:39
  • 1
    @JennyC Then you probably don't need to specify the drawing colour at all?
    – user30471
    Jul 29, 2014 at 14:39
  • 1
    I was forgetting: with colors, the order in which you specify the options matters. That's why draw,blue,fill=red makes blue cancel the previous draw=black and fill=red cancel the previous implicit setting fill=blue. Hope now it will be more clear. Jul 29, 2014 at 15:06
  • 1
    @cfr: at the cost of clarity ;) No offense, but to me "saving keystrokes" is a completely non-sense metric. Codes should be clear as much as possible, for ourselves and for others, now and in 10 years. I prefer every time to add some comments or a much longer expression if this helps. Jul 30, 2014 at 8:34
3

Use find/replace with Regular expressions (feature of editor, not of TeX) to replace any occurrence of ''fill=colorx,'' with ''fill=colorx,draw=colorx,''. In Winedt I just confirmed that searching for

fill=\(*\),

and replacing with

fill=\0,draw=\0,

will turn

\node[fill=red,rectangle]at (0,0){test};%

into

\node[fill=red,draw=red,rectangle]at (0,0){test};%
2
  • Thanks, I need to learn the regular expressions stuff asap :)
    – JennyC
    Jul 29, 2014 at 15:02
  • @JennyC Note that the syntax is editor dependent. (See my comment above re. Kile.)
    – cfr
    Jul 29, 2014 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .