What is the best way to place a label in the middle of a line with TikZ? I mean smack dab in the middle of the line, not above or below it. The following is close

\draw[|->, color=red] (-4cm, 0.5cm) -- (2.5cm, 0.5cm) node[midway, color=black] {$\tau_{r}$};

But the line crosses right through the node and its text, which is bad.

I don't know if there is an option to choose the background color of a node, which I think would solve the problem (e.g. background=white). fill doesn't seem to work.


You can use the fill key to “delete” the background. As the comments show you must care to set no other color after the fill key with color or just using the color name as an option. That will overwrite the color given to fill. As you can see in the example color set the color for filling, drawing and the text.



    \draw (0,0) -- ++(5,2) node [midway,fill=white] {ABC};
    \draw (3,0) -- ++(5,2) node [midway,fill=white,color=blue] {ABC};
    \draw (6,0) -- ++(5,2) node [midway,fill=white,color=blue,text=white] {ABC};

color settings

To change the text or drawing color you can use the draw and text options, which won’t affect the fill color.

\node [draw=blue,fill=gray,text=white] {Colors};

three colors

  • I actually tried fill=white, but I added it before color=black. This resulted in the whole box being black. Does order of commands actually matter in TikZ? Apr 20 '12 at 7:34
  • @xiongtx: for me this MWE works perfectly. Apr 20 '12 at 7:36
  • @xiongtx Yes it does. When you use color=black then that is a meta-command which sets all colours to black: line colour, fill colour, and text colour. If you want to override any of those then you have to reset it afterwards. Apr 20 '12 at 7:36
  • As the manual says, if you use fill “the area enclosed by the path is filled with the current filling color, which is either the last color set using the general color= option or the optional color ⟨color⟩.” (p. 161) So in you case the last set color wins and overwrites the setting of fill.
    – Tobi
    Apr 20 '12 at 7:38
  • 7
    (Sorry, can't resist: the order of doing things is more commonly important than not. That 2+3 = 3+2 ought to be surprising - the fact that it isn't is merely because we're so familiar with it that we don't notice the oddity. But almost everywhere else, order matters. In Norway there is a children's song which translates roughly to: "Open window, look out - hello weather, how are you?". Imagine the difference if you first put your head out of the window and then opened it.) Apr 20 '12 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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