TikZ-PGF – How to Precisely Set the Position of Text Along a Path

decorationsnodestikz-pgf

Explanation

It seems that nodes can be carefully placed on paths using pos=.25 (i.e. 25% from node BEGIN to node END) when using -- syntax (see Example A).

Problem

I cannot have the same precision as above with paths constructed using the to syntax (see Example B.). Instead, I have resulted to using

postaction={decorate,decoration={text along path,text={|\ttfamily|TCP}}} 

Example A. Note that I can do:

\draw [<->] (data.south east) -- (app.north west) 
node [pos=.5, above, sloped] (TextNode) {TCP};

Example B. Note that I cannot do:

\draw [icoarrow,<->] (data.south east) to[out=90,in=270] (app.north west) 
node [pos=.5, above, sloped] (TextNode) {TCP};

Example

\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.text}


\begin{document}
\begin{tikzpicture}[node distance=2cm]
    \node (data) {Data};
    \node [right=of data] (app) {App};
    % Lines
    \draw [<->] (data) -- (app) node [pos=.5, above, sloped] (TextNode) {TCP}; % works fine
    \draw [<->] (data) to[out=90,in=90] (app) node [pos=.5, above, sloped] (TextNode) {TCP}; % does not work
    \draw [<->,postaction={decorate,decoration={text along path,text align=center,text={TCP}}}]  (data) to[out=90,in=90] (app); % works fine
    %\draw [<->,postaction={decorate,decoration={text along path,pos=.25,text={TCP}}}]  (data) to[out=90,in=90] (app); % does not work (causes error)
\end{tikzpicture}
\end{document}

Best Answer

For A:

Put the node right after to[out=90,in=90] like

\draw [<->] (data) to[out=90,in=90]node [pos=.5, above, sloped] (TextNode) {TCP} (app) ;

For B, do this:

\draw[<->,postaction={decorate,decoration={text along path,raise=1ex,text align=center,text={TCP}}}]   (data) to[out=90,in=90] (app);

Full code:

\documentclass{article}
%\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{positioning,decorations.text}


\begin{document}
\begin{tikzpicture}[node distance=2cm]
    \node (data) {Data};
    \node [right=of data] (app) {App};
    % Lines
    \draw [<->] (data) -- (app) node [pos=.5, above, sloped] (TextNode) {TCP}; % works fine
    \draw [<->] (data) to[out=90,in=90]node [pos=.5, above, sloped] (TextNode) {TCP} (app) ; % does not work
\end{tikzpicture}
\begin{tikzpicture}[node distance=2cm]
    \node (data) {Data};
    \node [right=of data] (app) {App};
    % Lines    
    \draw[<->,postaction={decorate,decoration={text along path,raise=1ex,text align=center,text={TCP}}}]   (data) to[out=90,in=90] (app); % does not work (causes error)
\end{tikzpicture}
\end{document}

enter image description here