Take the 2-minute tour ×
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.

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

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

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.

share|improve this question
add comment

1 Answer

up vote 19 down vote accepted

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.

\documentclass{article}

\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    \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};
  \end{tikzpicture}
\end{document}

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

share|improve this answer
    
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? –  xiongtx Apr 20 '12 at 7:34
    
@xiongtx: for me this MWE works perfectly. –  Claudio Fiandrino 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. –  Andrew Stacey 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
4  
(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.) –  Andrew Stacey Apr 20 '12 at 7:39
show 1 more comment

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.