1

Is there a way in TikZ to add a transformation such as xshift=1mm to every coordinate of a path?

The following don't work because they do either more or less than "every coordinate of a path":

  • \path [xshift=1mm] ... doesn't apply to coordinates defined with nodes, e.g. \draw [xshift=1mm] (A) -- (B); doesn't shift anything.
  • transform canvas={xshift=1mm} transforms everything (not just path coordinates), and causes troubles with the bounding box and the definition of new coordinates.
  • decorations don't change the underlying path (I think?) so \draw [fancy decoration] (A) -- (B) node [below] {text}; will have the node at the "wrong" position (corresponding to the unshifted path).

What does work is to manually add options to each coordinate, for example this works even with nodes: \draw ([opts]0,0) -- ([opts]A);. Well at least it works if A is defined through coordinate, and for other nodes it works when specifying an anchor as in A.center.

Is there something like a every coordinate/.style={opts} that would do the same thing without having to write opts for each coordinate?

As motivation, here's an example where it would help, while I think neither of the above three alternatives work:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (1,0);
    \coordinate (B) at (3,1);
    \draw [very thick,->] (A) -- (B) node [above left] {$\vec v$};
    %
    % Define style for shifting by 1cm perpendicular to A-B
    \tikzset{perpshift/.style={shift={($(A)!1cm!270:(B)-(A)$)}}}
    %
    \draw [very thick,->] ([perpshift]A) coordinate (A2) --
                          ([perpshift]$(A)!0.5!(B)$) coordinate (B2)
                          node (N) [below right] {$\vec w$};
    \draw [dashed] (A) -- (A2)
                   ($(A)!(B2)!(B)$) -- (B2);
\end{tikzpicture}
\end{document}

enter image description here

jeremie
  • 744
  • 3
    \begin{scope}[...] ... \end{scope}? – cfr Nov 03 '23 at 15:29
  • I think that can be used to share the \path [opts] ...; options among several paths, but it doesn't help for the problem that opts will have no effect on node coordinates like (A.center).... – jeremie Nov 03 '23 at 15:55
  • Short version: No, a coordinate/node is a fixed position in your diagram, irregardless of any transformation system. Long version: various fixes/patches/implementation exist around it here on this site. Also for a node A a specification like (A) is supposed to specify a point on the border (for many of the path operation that exist). That's the whole point of nodes, in a way. – Qrrbrbirlbel Nov 03 '23 at 16:07
  • That's by design, so you are looking at hacking/workarounds, I think. – cfr Nov 03 '23 at 16:08
  • 2
    Reading material: Q98924 (→ maybe Wibrow's answer still works, I also like Živanović' which is a little bit less hacky but not that automatic but at least .r easier to input than […]), my opinion, Q95030 (alternative approach but still very manual). – Qrrbrbirlbel Nov 03 '23 at 16:13
  • @Qrrbrbirlbel I'm not trying to modify an existing coordinate. What I want is the effect of opts in \path ([opts]A.center) or \coordinate (A) at (1,2); \path ([opts]A), but for the whole path instead of putting it there manually for each coordinate. – jeremie Nov 03 '23 at 16:15
  • @cfr since TikZ provides syntax to do that on a single coordinate (with \path ([opts]...) I don't think it would be a hack to have a style or something to apply the same to all coordinates on the path... – jeremie Nov 03 '23 at 16:17
  • 1
    Yes, I understood you. I still think Q98924 is directly the same. You want to apply the current transformation also to references to coordinates/nodes. This is exactly what Wibrow's answer does. If you only want to shift a straight line orthogonally, other solutions exist that can make your life easier. TikZ-CD even has a shift left and shift right key we could also use. – Qrrbrbirlbel Nov 03 '23 at 16:25
  • @jeremie 'Hack' is not a pejorative in the code context. – cfr Nov 03 '23 at 16:26
  • @Qrrbrbirlbel you're right, thanks for the links, these answers directly address my problem. The workaround I give above is basically Andrew Stacey's solution, which I still like best for simple cases with few coordinates. I guess this question can be closed as duplicate. – jeremie Nov 03 '23 at 16:30
  • @cfr I meant to say I don't think what I ask is against the design, since the feature is there already for single coordinates.... But as shown in the linked answers, it's arguably against the implementation :) – jeremie Nov 03 '23 at 16:33
  • @jeremie I can't find the reference now, but I had understood it was deliberate. But I might just be wrong about this. – cfr Nov 03 '23 at 16:48
  • 1
    @cfr It's badly explained in the manual (see also “my opinion” link above). @jeremie It has its advantages. If you want to draw -| path between to nodes but rotated, you simply do \draw[rotate=45] (A) -| (B);. The -| will be rotated, the points on the border of the node are correct and it looks good. Consider coordinates fixed points on the page and not shortcuts to pair of x and y values. – Qrrbrbirlbel Nov 03 '23 at 18:58
  • 1
    @Qrrbrbirlbel That's the bit I was trying to find in the manual, yes. Thanks. – cfr Nov 03 '23 at 20:26

0 Answers0