# Centering tikz node text

How node text can be positioned at exact center in the following piece of code?

\draw[dashed] (4.1,5.1) -- (6.1,3.1) node[above=1mm,rotate=-45] {$x$};


I want x to appear exactly above and at the center of the line.

Do you mean something like this:

\documentclass[border=2mm,tikz,preview]{standalone}

\begin{document}
\begin{tikzpicture}
\draw[dashed] (4.1,5.1) -- node[above=1mm,sloped] {$x$} (6.1,3.1) ;
\end{tikzpicture}
\end{document}


To my taste the x is to far from line. I would omit additional distance at above.

The same results you can achieved with:

• \draw[dashed] (4.1,5.1) to node[above,sloped] {$x$} (6.1,3.1);
• \draw[dashed] (4.1,5.1) -- (6.1,3.1) node[pos=0.5,above,sloped] {$x$};
• \draw[dashed] (4.1,5.1) -- (6.1,3.1) node[midway,above,sloped] {$x$};

Option sloped´ automatically rotate text such that it align with line slope.

• You could mention \draw[dashed] (4.1,5.1) -- (6.1,3.1) node[above=1mm,sloped,pos=0.5] {$x$}; as a second option. – Torbjørn T. Jul 8 '15 at 21:08
• Y@TorbjørnT, yes, you have right. The same can be also achieved width midway` option. I will edit my answer. – Zarko Jul 8 '15 at 21:11