4

How to fix the following to have math inside the text key with dynamic variables?

\documentclass{article}
\usepackage{pgfplots}

\usetikzlibrary{decorations.text, math, intersections}

\begin{document}
    \newcommand{\adhesion}{5}
    \begin{tikzpicture}
        \begin{axis}[
            cycle list name = color list,
            ]
            %
            \pgfplotsinvokeforeach{1, 2}{
                \addplot+ [
                    variable = \Fbr,
                    postaction = {
                        decoration = {
                            text along path,
                            text = {$\mu = #1 + \adhesion$},
                            }, decorate,
                        },
                    ]
                {#1 - \Fbr};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}
4
  • @campa I am not. However, it is my guess since just $\mu$ gets me the same error.
    – Diaa
    Dec 1, 2023 at 15:18
  • 1
    @campa I forgot to load the relevant libraries. So, I will delete or edit the question to post it correctly.
    – Diaa
    Dec 1, 2023 at 15:34
  • text along path={hello} makes a box out of each letter. This is not possible with a math formula.
    – egreg
    Dec 1, 2023 at 15:50
  • @Diaa If your math does not get any more complicate, you could set your label as normal text, e.g. i.stack.imgur.com/9i56g.png (the spacing is off, but at least you would have a label) Dec 1, 2023 at 16:17

2 Answers 2

2

You cannot have an entire math formula along a path. In this case you can get an approximation by doing

text = {{$\mu$} = \adhesion{} + #1},

With just two cycles, in order to reduce the ugliness:

enter image description here

Of course your paths will be different and the positioning so there's no overlap, I guess.

3
  • Why does the simple case text = {{$\mu$}}, fail? Should be there any normal text as in text = {{$\mu$}{}},? Edit: I just found out that an entire math can be employed by just having {} at the end like text = {{$\mu = \sqrt{\theta}$}{}},.
    – Diaa
    Dec 1, 2023 at 17:39
  • @Diaa text along path is difficult. Are you sure that the math formula works along a curved path?
    – egreg
    Dec 1, 2023 at 17:53
  • For now, I haven't needed to do so or try it.
    – Diaa
    Dec 1, 2023 at 17:58
2

I don't know why, but enclosing the math in {$ $} and adding empty {} after it did the trick and made me able to parse a complete math input.

enter image description here

\documentclass{article}
\usepackage{pgfplots}

\usetikzlibrary{decorations.text, math, intersections}

\begin{document}
    \newcommand{\adhesion}{5}
    \begin{tikzpicture}
        \begin{axis}[
            cycle list name = color list,
            ]
            %
            \pgfplotsinvokeforeach{1, 2}{
                \addplot+ [
                    variable = \Fbr,
                    postaction = {
                        decoration = {
                            text along path,
                            text = {$\mu = #1 + \adhesion \sqrt{\theta}$}{},
                            }, decorate,
                        },
                    ]
                {#1 - \Fbr};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .