# Writing mathematical text along a curve in TikZ

I am trying to write some mathematical text along a plotted curve in TikZ. I have read two related questions in this site:

and have tried to follow the method in the answers, with only partial success. Here is a minimal working example of what I am trying to do.

\documentclass[11pt,a4paper]{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{arrows.meta, decorations.text}

\begin{document}

\begin{center}
\begin{tikzpicture}[x=1.0cm,y=1.0cm,scale=1.0]
\draw [very thick,gray,opacity=0.4] (-2.5,-2.5) -- (2.5,2.5);
\draw [very thick,gray,opacity=0.4] (-2.5,2.5) -- (2.5,-2.5);
\draw[line width=3pt,domain=-1:1,smooth,variable=\x,black] plot ({1.7*sinh(\x)},{1.7*cosh(\x)});
\end{tikzpicture}
\end{center}
\end{document}


which produces this:

I would like somewhere along the thick black hyperbolic segment to write some mathematical text (e.g., r=0) aligned with the curve but not necessarily centred.

I tried the following:

\documentclass[11pt,a4paper]{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{arrows.meta, decorations.text}

\begin{document}

\begin{center}
\begin{tikzpicture}[x=1.0cm,y=1.0cm,scale=1.0]
\draw [very thick,gray,opacity=0.4] (-2.5,-2.5) -- (2.5,2.5);
\draw [very thick,gray,opacity=0.4] (-2.5,2.5) -- (2.5,-2.5);
\draw[line width=3pt,domain=-1:1,smooth,variable=\x,black,%
postaction={decorate,%
decoration={text along path, text={$r=0$}, raise=5pt}}%
] plot ({1.7*sinh(\x)},{1.7*cosh(\x)});
\end{tikzpicture}
\end{center}
\end{document}


It produces something reasonable (although I cannot find how to position the text along the curve):

but I get compilation errors which I don't understand:

./mwe.tex:14: Dimension too large.
\relax
l.14     ] plot ({1.7*sinh(\x)},{1.7*cosh(\x)});

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

./mwe.tex:14: Dimension too large.

l.14     ] plot ({1.7*sinh(\x)},{1.7*cosh(\x)});

I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.


Any help is appreciated!

• \documentclass[11pt,a4paper]{article} \usepackage{tikz,pgfplots} \usetikzlibrary{arrows.meta, decorations.text} \begin{document} \begin{center} \begin{tikzpicture}[x=1.0cm,y=1.0cm] \draw [very thick,gray,opacity=0.4] (-2.5,-2.5) -- (2.5,2.5); \draw [very thick,gray,opacity=0.4] (-2.5,2.5) -- (2.5,-2.5); \draw[line width=3pt,domain=-1:1,smooth,variable=\x,black,% postaction={decorate,decoration={text along path, text={$r=0$}, raise=5pt}}] plot ({1.7cm*sinh(\x)},{1.7*cosh(\x)}); \end{tikzpicture} \end{center} \end{document} 1.7cm*sin(\x) solved the problem – Niranjan Mar 5 '20 at 14:00
• Just reduce the samples to 11 or 15, say. The dimension too large errors come from TikZ wanting to compute the tangent of some very short Bezier curve segments. – user194703 Mar 5 '20 at 14:56

The Dimension too large error is in general due to the fact that by default, PGF computations rely on TeX \dimen registers, whose maximum value is \maxdimen, i.e., 16383.99999pt, which is approximately 226.7 inches, and thus approx. 18.9 feet, hence the 19 feet mentioned in the error message you reported.

In your case, the problem appears to be triggered by the text along path, due to a particular number of samples when drawing the curve (thanks to @Schrödinger'scat for pointing this out). If you draw the curve with samples=15, it works fine.

As I understand it: a large number of samples means very short “segments” used to draw the curve (presumably, “little” Bézier curves :-). The text along path option probably tries to determine the slope of the segment below each box of the text to typeset, and in order to do this divides the “delta y” of a segment by its (very close to zero in this case) “delta x,” which yields a number that is too large to fit in a TeX \dimen register.

\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{decorations.text}

\begin{document}

\begin{tikzpicture}[x=1cm, y=1cm]
\path (0,2.9);                 % extend the upper side of the bounding box

\begin{scope}[very thick, gray, opacity=0.4]
\draw (-2.5,-2.5) -- (2.5,2.5);
\draw (-2.5,2.5) -- (2.5,-2.5);
\end{scope}

\draw[line width=3pt, domain=-1:1, smooth, variable=\x, black,
samples=15,   % <-------- this
postaction={decorate,
decoration={text along path,
text={$r=0$}, raise=5pt,
% text align={left indent=1cm},
},
},
] plot ({1.7*sinh(\x)},{1.7*cosh(\x)});
\end{tikzpicture}

\end{document}


Options for positioning the text along your curve are described in the TikZ & PGF manual section Text Decorations (around page 656). They include align, left, right, center, left indent, right indent, fit to path and fit to path stretching spaces. For instance, if you uncomment the text align={left indent=1cm} in my example, you'll get the r = 0 formula starting at one centimeter from the leftmost point of the curve: