2
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{%
  parallelone/.style={%
    decoration={markings, mark= at position #1 with
    {\arrow[line width=0.5mm, stealth-]{Latex[length=2.5mm, width=2mm]}}},
    postaction={decorate}
  },
   parallelone/.default=0.5,
}

\begin{tikzpicture}
    \draw[parallelone] (0,0)--(4,4);
\end{tikzpicture}

\end{document}

(I am aware that similar questions have already been asked.)

To deal with more than one argument, I think I need to have pgfkeys, but I am still confused (for example, in the above example, things are somewhat nested). Is there a simple tutorial about it? Or can someone provide step by step explanation for the above case? (That is creating as many keys as I may need to change while using it (such as arrow line width, arrow type, length, width, etc.).)

  • 1
    Maybe you can link similar questions (that you mention in your question), that would make it easier for others to help. – Dr. Manuel Kuehner Mar 3 at 15:44
  • 1
    Which parameters do you want to modify? You can always say /.style args={...}{...} or /.style n args={3}{....}. – user121799 Mar 3 at 15:45
2

Since you are asking about a tutorial, I answer the question in two steps. (IMHO the pgfmanual is an excellent tutorial, in particular when you use it to understand examples that you can find on this site.) You can make an style depend on several parameters. But then you always need to specify all of them if you want to change just one. Therefore, I would like to argue that it is easier to store the parameters in pgfkeys with default value. This leads to

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{%
  parallelone arrow length/.initial=2.5mm,
  parallelone arrow width/.initial=2mm,
  parallelone arrow type/.initial=Latex,
  parallelone/.style={%
    decoration={markings, mark= at position #1 with
    {\arrow{\pgfkeysvalueof{/tikz/parallelone arrow type}[length=\pgfkeysvalueof{/tikz/parallelone arrow length}, 
    width=\pgfkeysvalueof{/tikz/parallelone arrow width}]}}},
    postaction={decorate}
  },
   parallelone/.default=0.5
}

\begin{tikzpicture}
    \draw[parallelone] (0,0)--(4,4);    
    % change the length and poisition
    \draw[parallelone=0.7,parallelone arrow length=5mm] (1,0)--(5,4);
    % change the type. the length is back to its initial or default values
    \draw[parallelone,parallelone arrow type=Stealth] (2,0)--(6,4);
    % change the width. the other paraneters are at their initial or deault values
    \draw[parallelone,parallelone arrow width=5mm] (3,0)--(7,4);    
\end{tikzpicture}
\end{document}

enter image description here

Of course, at a given point you may decide that you do not want to always type parallelone arrow before type, length and so on. This is what key families are for. Then you may only say \draw[parallelone={type=Stealth}] if you want to change the type and leave everything else at their default values.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{%
  parallelone/.style={%
    /utils/exec=\tikzset{parallelone arrow/.cd,#1},
    decoration={markings, mark= at position \pgfkeysvalueof{/tikz/parallelone arrow/pos} with
    {\arrow{\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
    length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
    width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]}}},
    postaction={decorate}
  },
  parallelone/.default={pos=0.5},
  parallelone arrow/.is family,
  parallelone arrow/.cd,
  pos/.initial=0.5,
  length/.initial=2.5mm,
  width/.initial=2mm,
  type/.initial=Latex,
}

\begin{tikzpicture}
    \draw[parallelone] (0,0)--(4,4);    
    % change the length and poisition
    \draw[parallelone={pos=0.7,length=5mm}] (1,0)--(5,4);
    % change the type. the length is back to its initial or default values
    \draw[parallelone={type=Stealth}] (2,0)--(6,4);
    % change the width. the other parameters are at their initial or deault values
    \draw[parallelone={width=5mm}] (3,0)--(7,4);    
\end{tikzpicture}
\end{document}

enter image description here

If you want two arrows with the same functionality, you could try

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}

\tikzset{%
  parallelone/.style={%
    /utils/exec=\tikzset{parallelone arrow/.cd,#1},
    decoration={markings, mark= at position \pgfkeysvalueof{/tikz/parallelone arrow/pos} with
    {\arrow{\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
    length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
    width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]}}},
    postaction={decorate}
  },
  paralleltwo/.style={%
    /utils/exec=\tikzset{parallelone arrow/.cd,#1},
    decoration={markings, mark= at position \pgfkeysvalueof{/tikz/parallelone arrow/pos} with
    {\arrow{\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
    length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
    width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]%
    \pgfkeysvalueof{/tikz/parallelone arrow/type}[%
    length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
    width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]}}},
    postaction={decorate}
  },
  parallelone/.default={pos=0.5},
  parallelone arrow/.is family,
  parallelone arrow/.cd,
  pos/.initial=0.5,
  length/.initial=2.5mm,
  width/.initial=2mm,
  type/.initial=Latex,
}

\begin{tikzpicture}
    \draw[paralleltwo] (0,0)--(4,4);    
    % change the length and poisition
    \draw[parallelone={pos=0.7,length=5mm}] (1,0)--(5,4);
    % change the type. the length is back to its initial or default values
    \draw[parallelone={type=Stealth}] (2,0)--(6,4);
    % change the width. the other parameters are at their initial or deault values
    \draw[parallelone={width=5mm}] (3,0)--(7,4);    
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    I think the OP wants to have something like parallelone=0.3 and 0.7, because as I can see parallelone arrow length is totally not related to parallelone. – user156344 Mar 3 at 16:07
  • @JouleV One can do that, yes, but I wouldn't want to do that because you will always have to add the information on all the arrow details if you just want to move the position. – user121799 Mar 3 at 16:18
  • 1
    @blackened [line width=0.5mm] is not included, but it has no effect, I just forgot to drop that key. The arrow is completely determined by the other parameters (in this example). And a double arrow can be obtained as usual, i.e. by repeating arrow, see e.g. at the bottom of p. 210 of the pgfmanual. – user121799 Mar 3 at 18:47
  • 1
    @blackened The arrow sizes will increase proportionally when you increase the line width. If you do not want to increase the line width, AFAIK you need to adjust both the length and the width separately. – user121799 Mar 18 at 14:15
  • 1
    @blackened Try \draw[parallelone={width=4mm, type={Computer Modern Rightarrow[line cap=round]}}]. – user121799 May 8 at 15:18

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.