pgf-tikz / pgfplots

pgfplots - A TeX package to draw normal and/or logarithmic plots directly in TeX in two and three dimensions with a user-friendly interface and pgfplotstable - a TeX package to round and format numerical tables. Examples in manuals and/or on web site.
http://pgfplots.sourceforge.net/
198 stars 33 forks source link

Wrong value from the \pgfplotsunit[xyz]length macro #74

Open pgf-tikz-bot opened 9 years ago

pgf-tikz-bot commented 9 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/ Author: liverpool-luigi Timestamp: 2014-12-04 09:13:23.004000

According to this question, I found out that the \pgfplotsunit[xyz]length macros don't actually yield the unit vectors length as stated in the manual of the package. See the following example:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\draw (axis cs:0,0.5) -- ++(0:\pgfplotsunitxlength pt)
                      -- +(90:\pgfplotsunitylength pt);
\draw [red, yshift=2cm] (0,0) -- (1,0);
\end{axis}
\end{tikzpicture}
\end{document}

where the blue lines are as long as the actual unit vectors, the black lines are as long as \pgfplotsunitxlenght pt and \pgfplotsunitylength pt, and the red line is as long as TikZ x unit. You can see that \pgfplotsunitxlenght is not equal to pgfplots unit x length, but rather to TikZ unit x length (I would say that axis scaling is missing).

I think this depends on the fact that in the pgfplots.scaling.code.tex source code, on line 1826 and following, the \pgfplots@x@veclength macro is defined as the length of vector (\pgf@xx,\pgf@xy), which is TikZ x unit vector and not pgfplots one.

If you want the actual x unit vector length (in points), you have to do something like:

\pgfplotstransformdirectionx{1} \\ or \pgfplotstransformcoodinatex{1}
\pgfmathsetlengthmacro{\pgfplotsxunit}{\pgfmathresult*\pgfplotsunitxlength}

as in the following example:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis} [ymax=11]
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\pgfplotsextra{
  \pgfplotstransformdirectionx{1}
  \pgfmathsetlengthmacro{\pgfplotsxunit}{\pgfmathresult*\pgfplotsunitxlength}
  \pgfplotstransformdirectiony{1}
  \pgfmathsetlengthmacro{\pgfplotsyunit}{\pgfmathresult*\pgfplotsunitylength}
}
\draw [<->, dashed] (0,0.5) --  +(0:\pgfplotsxunit);
\draw [<->, dashed] (0.8,0) --  +(90:\pgfplotsyunit);
\end{axis}
\end{tikzpicture}
\end{document}
pgf-tikz-bot commented 9 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/#acbd Author: cfeuersaenger Timestamp: 2015-01-21 19:59:15.085000

Thanks for the detailed report.

However, according to the (admittedly very brief) reference manual, the macro is defined as "The \pgfplotspointunitx is the pgf unit vector in x direction".

This excludes the scaling stuff. Which raises the question why is it exposed in the reference manual at all and what are its uses!?

I will think about a solution. It sounds, however, as if I should prefer a solution which allows to write +(0:1) directly... especially since I improved the interoperability between tikz and pgfplots in the last and current release.

To me, it sounds as if I could improve the manual regarding this basic level macro (perhaps mark it as "internal, handle with care") and fix the high level interface instead. Let me know if this is not what you want.

pgf-tikz-bot commented 9 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/#f1c1 Author: cfeuersaenger Timestamp: 2015-01-21 20:53:20.219000

I have added support for +(0:1) directly - it will be evaluated with axis units.

pgf-tikz-bot commented 9 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/#8cef Author: liverpool-luigi Timestamp: 2015-01-22 09:19:18.303000

Thank you for your feedback. It's true that those macros seem to be unuseful, but I'm telling you what I used them for. Suppose I have a coordinate system with two different unit lengths for the axes. I want to draw an arc between two lines by means of the arc instruction provided by TikZ. So I need to compute the angle between the two lines. I cannot use the two slopes directly, because the two units of measurement are different, so I have to scale them using the two unit vector lenghts. If I use the \pgfplotsunit[xy]macro as it is now, the result is incorrect because of the behaviour I told you in the first post. Finally, I think that in some rare cases those macro are useful and should be improved. For instance, I'd convert them in lenght macros.

pgf-tikz-bot commented 9 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/#08b8 Author: liverpool-luigi Timestamp: 2015-01-22 22:27:27.962000

pgf-tikz-bot commented 9 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/#acbd/c775 Author: liverpool-luigi Timestamp: 2015-01-22 22:28:52.733000

I've just had another look to the reference manual. Actually I missed the phrase "The $x_i$ are the \pgfplotspointunitx variants". So I agree with your questions about the exposition in the manual and uses. I thought that the name pgfplots-unit-x-length meant "the lenght of the pgfplots unit in the x direction", not the pgf one, for which I had used the name "pgf-unit-x-length" instead.

By the way, what about adding three new length macros for the pgfplots unit [xyz] lengths, or redefining the existing ones with the new meaning? I think it would be more useful.

pgf-tikz-bot commented 7 years ago

Migrated from SourceForge https://sourceforge.net/p/pgfplots/bugs/74/#127d Author: cfeuersaenger Timestamp: 2017-03-18 09:35:49.655000