Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does rectangle operation change (current subpath start)? #1341

Open
gqqnbig opened this issue Jun 23, 2024 · 2 comments
Open

Why does rectangle operation change (current subpath start)? #1341

gqqnbig opened this issue Jun 23, 2024 · 2 comments

Comments

@gqqnbig
Copy link

gqqnbig commented Jun 23, 2024

Minimal working example (MWE)

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
\draw[gray] (0,0) grid (3,3);

\draw[color=blue] (0,1) rectangle +(1,2);
\draw[color=red] (current subpath start) -- (3,3);
\end{tikzpicture}

\begin{tikzpicture}
\draw[gray] (0,0) grid (3,3);

\draw[color=blue] (0,1) grid +(1,2);
\draw[color=red] (current subpath start) -- (3,3);
\end{tikzpicture}

\begin{tikzpicture}
\draw[gray] (0,0) grid (3,3);

\draw[color=blue] (0,1) -- +(1,2);
\draw[color=red] (current subpath start) -- (3,3);

\end{tikzpicture}
	
\end{document}

In this MWE, I draw three figures using the rectangle, grid, and -- (move-to) operation respectively, and the result is below. I compiled it on Overleaf (texlive 2023).

compilation result

As you can see, the rectangle and the grid operation behave differently than -- (move-to). In Figure 1 and 2, the red line starts at (1,3), but in Figure 3 the red line starts at (0,1).

I deliberately write +(1,2) because pgfmanual says " This [] specifies a point in a relative manner, but it does not “change” the current point used in subsequent relative commands. " (section 11.1)

Since +(1,2) does not change the current point, what should (current subpath start) in the second draw command be? And why do the rectangle, grid, and -- (move-to) operation behave differently?

I couldn't find any hints in the 1300-page manual, so I guess it could be a bug, I am not sure.

@muzimuzhi
Copy link
Member

muzimuzhi commented Jun 23, 2024

Line-to and rectangle are basic operations which are directly translated to backend-specific graphic commands.

Grid is a compound operation which is first translated by tikz to the pgf command \pgfpathgrid, then decomposed to multiple pgf move-to and line-to operations, with a trailing move-to to the second corner point of grid.

\pgf@nlt@moveto{\pgf@x}{\pgf@y}%

What you observed is caused by that undocumented trailing pgf move-to, hence it's independent to tikz's relative coordinate syntax.

In general the current subpath start after each kind of path operations are not well documented. Some of such cases are even not well-defined, see #1136 for the case of circle[at={...}].

@gqqnbig
Copy link
Author

gqqnbig commented Jun 24, 2024

Thanks for your quick reply.

I encountered this problem when I was drawing Fibonacci spiral. I attempted to use (current subpath start) because it seemed to present the last coordinate.

\documentclass[svgnames, tikz]{standalone}

\pgfkeys{
  /directions/.is family, /directions,
  1/.initial = {(1,-1)},
  2/.initial = {(1,1)},
  3/.initial = {(-1,1)},
  0/.initial = {(-1,-1)}
}
\newcommand\getDirection[1]{\pgfkeysvalueof{/directions/#1}}

\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\draw (1,0) rectangle (0,-1);


% count starts from 1
\foreach \fib [count=\n] in {1,2,3,5,8,13}
{
\path (current subpath start) coordinate (s);

\pgfmathparse{int(mod(\n,4))}
\path \getDirection{\pgfmathresult} coordinate (d);

\path ($(s)+\fib*(d)$) coordinate (e);

\draw[->, blue] (s) -- (e); % Line A: draw arrows
\draw (s) rectangle (e); % Line B: draw rectangles

};

\end{tikzpicture}
\end{document}

texstudio_j2WXna56ps

If I put Line A (draw arrows) before Line B (draw rectangles), the spiral is correct.
If I put Line B (draw rectangles) before Line A (draw arrows), the spiral is incorrect.

This observation lead me to investigate the inference of -- and rectangle on (current subpath start).

In general the current subpath start after each kind of path operations are not well documented.

Are you saying that I'm not supposed to use (current subpath start) as the first coordinate of a path? Do you have an alternative way to remember the current coordinate?

Alternatively, is it easy to define the behavior of (current subpath start) that if (current subpath start) is the first coordinate of a path, it is the last point of the previous path command?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants