jonschz / tikz-nfold

Triple, quadruple, and n-fold paths and arrows with TikZ
LaTeX Project Public License v1.3c
11 stars 1 forks source link

Individual arrow style #16

Open tobiasBora opened 9 months ago

tobiasBora commented 9 months ago

Thanks for this great package! But by any chance, do you think it would be possible to add a way to easily customize the style of individual lines? For instance:

A \ar[nfold=3, nfold wires={-latex}, nfold wire={2}{latex-}] & B\\

would produce 3 arrows, where the first and last arrows points right while the second arrow points left. I see that you provide in the doc a solution to customize the path using save path, but it is not really user friendly and I don't know if we can use custom arrow tips.

jonschz commented 9 months ago

Happy to hear you like the package!

When enabling nfold, the part of the rendering pipeline that draws the "body" (i.e. the part between the arrow tips) is modified. The rendering of the arrow tips is not modified. Therefore, the feature you describe will certainly not be easy to implement. I would also argue that it goes against the common TikZ/pgf/tikz-cd logic, where every call to \ar or \path has results in at most one tip on either end.

That being said, there could be a more elegant solution here. Are your arrows supposed to be straight or curved? In the straight case, you could try something like \foreach[index=\i] and us shift left={some \i-dependent value}. For curved paths you could indeed try to use the example code from the documentation - I will test it with arrow tips when I have the time.

A good point you made is that the interface to this feature could clearly be improved. I am always open to suggestions. The difficulty is that there are several modes with different parameters for different use cases. A modified shift left to account for curvature might be possible (although arrow tips could turn out to be difficult).

Thanks for testing and reporting!

Qrrbrbirlbel commented 9 months ago

Since I have used the offset key as defined below in some answers on TeX.sx, let me start with an example.

The key nfolder = {<list>} will distribute the <list> of styles (could also be empty just to denote the number) across the width of /tikz/nfolder distance. I don't know how to access PGF's inner line width (the one that enables the doubling) in the best way without actually activating the double thing at all so you have to specify it yourself.

Again, this describes the distance between the outer most lines, not the space between neighbor lines.

TikZ-CD keys are allowed (like rightarrow) and using nfolder <number> to set a style is also possible (then you can always use an empty list, and yes, we could also define a key nfolder'={3} that just draws three parallel lines with already present nfolder 0, nfolder 1 and nfolder 2 styles.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{cd, nfold, bending}
\makeatletter
\tikzcdset{
  nfolder distance/.initial=1mm,
  nfolder/.code={%
    \tikzset{path only, arrows=-}%
    \setcounter{pgf@counta}{-1}%
    \pgfutil@for\tikz@temp:=#1\do{%
      \stepcounter{pgf@counta}%
      \tikzcdset{nfolder \the\value{pgf@counta}/.append style/.expand once=\tikz@temp}}%
    \edef\nfolder@total{\the\value{pgf@counta}}%
    \tikzcdset{@nfolder/.list={0, ..., \nfolder@total}}},
  @nfolder/.style={
    /tikz/postaction/.expanded={
      draw,
      offset={(#1*2/\nfolder@total-1)*(\pgfkeysvalueof{/tikz/commutative diagrams/nfolder distance})},
      /tikz/commutative diagrams/.cd, nfolder #1/.try}},
  /tikz/offset/.code=
    \tikz@addoption{%
      \pgfgetpath\tikz@temp
      \pgfsetpath\pgfutil@empty
      \pgfoffsetpath\tikz@temp{#1}}}
\makeatother
\begin{document}
\begin{tikzcd}
A & B \ar[d, nfolder distance=1.2mm, nfolder={rightarrow, help lines, Leftrightarrow}] \\
  & C \ar[ul,nfolder distance=1.5mm, nfolder={-Latex, Latex-, {red, thick, Stealth-Latex}}, bend left, ul]
\end{tikzcd}
\end{document}

Output

grafik

jonschz commented 9 months ago

That is pretty impressive! Are you interested in contributing your macro to this package? At first glance it looks like it should work outside of tikzcd as well.

Only minor nitpicking: The result will not be accurate on paths with non-trivial line joins (which are quite rare in tikz-cd, like in custom path). I think you can define a modified version of your offset macro that uses \pgfoffsetpathfraction. This should also render your offset calculation obsolete.

tobiasBora commented 9 months ago

That’s awesome! Would be great to integrate in this package indeed!