2

I'm trying to decorate a path with vectors. I've created two different styles: one which creates tangent vectors, and one which creates random ones.

I want in each selected point of the path two vectors: one (dl) that is tangent and one (Rvec) that is randomly rotated.

Unfortunately I cannot apply both styles simultaneously (third picture). There seems to be something wrong with the Rvec style: in the second image it will not show at all and in the third it somehow prohibits the dl decoration. The dl style seems to work just fine.

Also at some point Rvec was somehow applied twice (in each point there were 2 random vectors) but I can't seem to reproduce that behavior again.

Do you have any idea how this could be solved?

Multiple decorations

This is my minimal example

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.markings}

\tikzset{
% Tangent vector
    dl/.style={ 
        decoration={
            markings,
            mark=between positions 0 and 1 step #1 with {
                \draw[{Circle[length=4pt]}-Latex, purple, very thick] (0,0) -- (28pt, 0) node[above]{$\vec{dl}$};
            }
        },
        postaction={decorate}
    },
% Random vector
    Rvec/.style={ 
        decoration={
            markings,
            mark=between positions 0 and 1 step #1 with {
                \draw[{Circle[length=4pt]}-Latex, black, very thick] (0,0) -- (rand*360:28pt) node[above]{$\vec{H}$};
            }
        }
    },
    postaction={decorate}
}

\begin{document}
\begin{tikzpicture}[scale=1.3]
    \draw[thick, red, dl=0.2] (0,1)  to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.3]
    \draw[thick, red, Rvec=0.2] (0,1)  to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.3]
    \draw[thick, red, Rvec=0.2, dl=0.2] (0,1)  to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}
\end{document}
  • Decorations have to be be put in ascending order, this is most likely the origin of the problem. The simplest way out may just to do the path again with the second decoration. – user121799 Oct 17 '18 at 23:54
2

There are two issues:

  1. postaction=decorate was outside the Rvec style, which is why the random vectors did not show in the second picture.
  2. Decorations have to be be put in ascending order. So you might want to draw the path twice. This can be made more convenient with the use path trick.

Suggested code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.markings}
\makeatletter % https://tex.stackexchange.com/a/38995/121799
\tikzset{
  use path/.code={\pgfsyssoftpath@setcurrentpath{#1}}
}
\makeatother

\tikzset{
% Tangent vector
    dl/.style={ 
        decoration={
            markings,
            mark=between positions 0 and 1 step #1 with {
                \draw[{Circle[length=4pt]}-Latex, purple, very thick] (0,0) -- (28pt, 0) node[above]{$\vec{dl}$};
            }
        },
        postaction={decorate}
    },
% Random vector
    Rvec/.style={ 
        decoration={
            markings,
            mark=between positions 0 and 1 step #1 with {
                \draw[{Circle[length=4pt]}-Latex, black, very thick] 
                (0,0) -- (rand*360:28pt) node[above]{$\vec{H}$};
            }
        },
    postaction={decorate}
    }
}

\begin{document}
\begin{tikzpicture}[scale=1.3]
    \draw[thick, red, dl=0.2] (0,1)  to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.3]
    \draw[thick, red, Rvec=0.2] (0,1)  to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
\end{tikzpicture}

\begin{tikzpicture}[scale=1.3]
    \draw[thick, red, Rvec=0.2,save path=\tmpath] (0,1)  to[out=30,in=210] node[midway,below] {$\Gamma$} (6, 1);
    \path[dl=0.2,use path=\tmpath];
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much! What do you mean by decorations being in "ascending order"? Ascending as in alphabetical order? – Iam Oct 18 '18 at 10:51
  • @Iam Ascending means in positions. You place the first set decorations at 0, 0.2 ... and then the second set starts again at 0, which is not OK according to what the pgfmanual says on p. 585: "It is possible to give the mark option several times, which causes several markings to be applied. In this case, however, it is necessary that the positions on the path are in increasing order. That is, it is not allowed (and will result in chaos) to have a marking that lies earlier on the path to follow a marking that is later on the path." – user121799 Oct 18 '18 at 11:56

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.