[Tex/LaTex] Place label in middle of line with TikZ

horizontal alignmentlabelstikz-pgf

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.

Best Answer

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