3

I want to label a family of curves representing mathematical functions by writing at some place of the curve the formula directly on the trace. More specifically, my family of curves is the very simple x -> x²+c where c will take various values between -4 and 4. As this family of functions will be difficult to label the usual way (with formula next to the curve), I want to superpose the label directly on the curve (thus interrupting the trace - with a little white space to help the reading).

I tried with TikZ, and failed. The following code is OK with decorations like "bumps", but not with "text along path".

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

\begin{document}

    \begin{center}
        \begin{tikzpicture}[>=Stealth ]
        \def\xmin{-4}
        \def\xmax{4}
        \def\ymin{-6}
        \def\ymax{6}
        \draw[thin, opacity=0.2] (\xmin, \ymin) grid (\xmax, \ymax);
        \draw[very thick, ->] (\xmin,0) -- (\xmax,0) node[below left] {$x$} ;
        \draw[very thick, ->] (0,\ymin) -- (0,\ymax) node[below left] {$y$} ;

        \clip (\xmin, \ymin) rectangle (\xmax, \ymax) ;
        \draw[very thick, dashed, domain=-3:3,samples=200] plot (\x, {(\x)^2} ) ;
        \draw[very thick, domain=\xmin:\xmax,samples=200, red, decorate, 
                decoration={bumps} % OK
                %decoration={text along path, text={\small test}} % Not OK
                ]
            plot (\x, {(\x)^2+3} ) ;
        \draw[decorate, decoration={text along path, text={Some long text along a ridiculously long curve that could yet be longer}} ] (-4,3) to[in=180,out=0] (0,0) to[in=180,out=0] (4,3) ; % OK too
        \end{tikzpicture}
    \end{center}

\end{document}

This code does not compile, in an odd way : the compilation never ends ! I use LuaLaTeX with the latest TexLive (on Ubuntu 18.04, but this is pointless).

Additionally, I found something near to my needs here : Pgfplots: plot text along function. But this approach uses PGFplots syntax which I do not master - and don't have enough time to begin mastering now.

Does someone have any idea ? Thanks in advance !

Update

Thanks to Schrödinger's Cat, who seems to be quite alive, I'm close to what I have in mind. But I have major difficulties to surround the label on each curve to mask the curve, in order to improve its readbility. Here a code showing where I am :

\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{arrows, arrows.meta, decorations.text, decorations.pathmorphing} % decoration.markings

\begin{document}
        \begin{tikzpicture}[scale=.8,>=Stealth]
            \def \xmin {-4}
            \def \xmax {4}
            \def \ymin {-6}
            \def \ymax {6}
            \draw[thin, opacity=0.2] (\xmin, \ymin) grid (\xmax, \ymax);

            \draw[very thick, ->] (\xmin,0) -- (\xmax,0) node[below left] {$x$} ;
            \draw[very thick, ->] (0,\ymin) -- (0,\ymax) node[below left] {$y$} ;

            \clip (\xmin, \ymin) rectangle (\xmax, \ymax) ;
            \draw[very thick, dashed, domain=-3:3,samples=200] plot (\x, {(\x)^2} ) ;
            \foreach \c [evaluate=\c as \pc using {100-(\c+6)*9}, evaluate=\c as \ic using {abs(\xmax-\c)+.5}] in {-6,-4,-2,2,4} {
                \draw[very thick, domain=\xmin:\xmax,samples=200, blue!\pc!red,
                %                  text effects={path from text, characters={text along path, fill=gray!50}},
                postaction={decorate,
                    decoration={text along path,
                        raise=-.5ex,
%                        fill=green     % Let (Lua)LaTeX hang
                        text={|\scriptsize| $x {\mapsto} {x^2}+(\c)$},
                        text color=blue!\pc!red,
                        text align={align=right, right indent=\ic cm}
                    },
                    text effects={path from text, characters={text along path, fill=gray!50}},
                } ]
                plot (\x, {(\x)^2+\c} )  ;
            } ;

            \draw (0,0) node[shift={(-.3cm,-.3cm)}, fill=white, circle, inner sep=-.5pt] {$O$} ;
            \coordinate (I) at (1,0);  \draw (I) node {\small $|$} node[yshift=-.3cm, fill=white, inner sep=.5pt] {$1$} ;
            \coordinate (J) at (0,1);  \draw (J) node {$-$} node[xshift=-.2cm, fill=white, inner sep=.5pt] {$1$} ;
        \end{tikzpicture}
\end{document}
  • You only need to wrap \small in |s: decoration={text along path, text={|\small| test}}. – Schrödinger's cat Oct 8 at 17:21
  • Thanks, I had absolutely no idea about it ! – natsirt Oct 8 at 19:01
3

As is written in section 50.6 Text Decorations of the pgfmanual, you need to wrap text format commands in the corresponding delimiters. The default delimiters are | on both sides. Using this, and adding text align={align=center} to avoid that the text gets clipped away, one gets

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

\begin{document}

    \begin{center}
        \begin{tikzpicture}[>=Stealth ]
        \def\xmin{-4}
        \def\xmax{4}
        \def\ymin{-6}
        \def\ymax{6}
        \draw[thin, opacity=0.2] (\xmin, \ymin) grid (\xmax, \ymax);
        \draw[very thick, ->] (\xmin,0) -- (\xmax,0) node[below left] {$x$} ;
        \draw[very thick, ->] (0,\ymin) -- (0,\ymax) node[below left] {$y$} ;

        \clip (\xmin, \ymin) rectangle (\xmax, \ymax) ;
        \draw[very thick, dashed, domain=-3:3,samples=200] plot (\x, {(\x)^2} ) ;
        \draw[very thick, domain=\xmin:\xmax,samples=200, red, 
        postaction={decorate, 
                decoration={text along path, text={|\small|Some long text along a ridiculously long curve that could yet be longer},
                text align={align=center}}} % Now OK
                ]
            plot (\x, {(\x)^2+3} ) ;
        \draw[decorate, decoration={text along path, text={Some long text along a ridiculously long curve that could yet be longer}} ] (-4,3) to[in=180,out=0] (0,0) to[in=180,out=0] (4,3) ; % OK too
        \end{tikzpicture}
    \end{center}

\end{document}

enter image description here

Notice that I used postaction=decorate to have both the parabola and the text, and that you could also use parabola bend instead of a plot with 200 samples.

  • Thanks a lot, you answer nearly fill all my needs. I must admit, and apologize for it, that I directly read the doc at the relevant paragraph, missing the instruction about how to do font changes or math typesetting). But is there a way to center the text just above the curve, thus recovering the trace ? With a little amount of white space at both sides of the text (in the "node way") to help reading it ? – natsirt Oct 9 at 6:33
  • @natsirt You can move up the text with the raise key, e.g. \draw[very thick, domain=\xmin:\xmax,samples=200, red, postaction={decorate, decoration={text along path, text={|\small|Some long text along a ridiculously long curve that could yet be longer}, text align={align=center},raise=0.7ex}} % Now OK ] plot (\x, {(\x)^2+3} ) ; – Schrödinger's cat Oct 9 at 9:05
  • Thanks one more time ! I must admit that I'm quite loste with the right place to put options... One last thing : I am still unable to create white background (with perhaps a little bit of transparency) in order to let the label of each curve more readable. I tried the fill key, but cannot figure where to put it. At last, I tried with text effects, like in the doc p. 657-658 or 663. No success yet... – natsirt Oct 9 at 10:00
  • @natsirt Could you please ask a new question in which you show the very code that you are trying? – Schrödinger's cat Oct 9 at 10:03
  • Well, I'm still under the description I once wrote, but I will update the question to reflect its evolution. – natsirt Oct 9 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.