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

sloped should consider the current transformation #1058

Open
thinbold opened this issue Oct 11, 2021 · 3 comments · May be fixed by #1371
Open

sloped should consider the current transformation #1058

thinbold opened this issue Oct 11, 2021 · 3 comments · May be fixed by #1371

Comments

@thinbold
Copy link
Contributor

Brief outline of the proposed feature

Inspired by this SX question and this older one,
I would like to suggest that we should consider the current transformation
when computing the slope of the sloped nodes.

Usage example

The following code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[xscale=2, yscale=3, xshift=40, yshift=50]
	\draw (0, 0) -- node[sloped, above] {sloped correctly?} (1, 1);
\end{tikzpicture}
\end{document}

produces a line and a node that do not align.
This is because in the definition of \pgftransformlineattime,
the slope is calculated using the start and end coordinates
without applying the current transformation.
(For instance, in the above example, the diff vector will be (1cm, 1cm), not (2cm, 3cm).)
If we take that into account, a proper slope can be computed.

See my answer for one possible fix.

@muzimuzhi
Copy link
Member

muzimuzhi commented Oct 12, 2021

The current PGF code ensures that the "external" transformation is only applied when transform shape is used. But the linked TeX-SX question shows a situation that with sloped users may expect the "external" rotation auto applied, but not the translation nor scaling, which may make the role of transform shape vague.

A little improvement to your answer on TeX-SX (the added transformation calculations are now surrounded by \ifpgfresetnontranslationattime and \fi), to make sloped work with transform shape:

\documentclass{article}
\usepackage{tikz}

\makeatletter

\def\pgftransformlineattime#1#2#3{%
  \pgfgettransformentries\pgf@tempaa\pgf@tempab\pgf@tempba\pgf@tempbb\pgf@notimportantx\pgf@notimportanty%%% ⬅️⬅️⬅️ new
  \pgf@process{#2}%
  \pgf@xb=\pgf@x% xb/yb = start point
  \pgf@yb=\pgf@y%
  \pgf@process{#3}%
  \pgf@xc=\pgf@x% xc/yc = end point
  \pgf@yc=\pgf@y%
  \pgftransformshift{\pgfpointlineattime{#1}{\pgfqpoint{\pgf@xb}{\pgf@yb}}{\pgfqpoint{\pgf@xc}{\pgf@yc}}}%
  \ifpgfresetnontranslationattime%
    \pgftransformresetnontranslations%
  \fi%
  \ifpgfslopedattime%
    \advance\pgf@xc by-\pgf@xb%
    \advance\pgf@yc by-\pgf@yb%
    % Now that we get the tangent vector without transformation
    % It suffices to apply the non-shift part of the transformation
    {%%% ⬇️⬇️⬇️ new
      \ifpgfresetnontranslationattime
        \pgfsettransformentries\pgf@tempaa\pgf@tempab\pgf@tempba\pgf@tempbb{0pt}{0pt}%
        \pgf@pos@transform{\pgf@xc}{\pgf@yc}%
        \global\pgf@xc\pgf@xc
        \global\pgf@yc\pgf@yc
      \fi
    }%%% ⬆️⬆️⬆️ new
    % OK, now xc and yc are correctly tangent to the line
    % Continue the usual routine
    \ifpgfallowupsidedownattime%
    \else%
      \ifdim\pgf@xc<0pt%
        \pgf@xc=-\pgf@xc%
        \pgf@yc=-\pgf@yc%
      \fi%
    \fi%
    \pgf@x=\pgf@xc%
    \pgf@y=\pgf@yc%
    \pgfpointnormalised{}% x/y = normalised vector
    \pgf@ya=-\pgf@y%
    \pgftransformcm%
    {\pgf@sys@tonumber{\pgf@x}}{\pgf@sys@tonumber{\pgf@y}}%
    {\pgf@sys@tonumber{\pgf@ya}}{\pgf@sys@tonumber{\pgf@x}}{\pgfpointorigin}%
  \fi%
}
\makeatother

\begin{document}
\begin{tikzpicture}[xscale=2, yscale=3, xshift=40, yshift=50, nodes={above}]
    \draw (0, 0) -- node[sloped] 
      {\texttt{sloped} only} +(2, 2);
    \draw (2, 0) -- node[transform shape] 
      {\texttt{transform shape} only} +(2, 2);
    \draw (4, 0) -- node[sloped, transform shape] 
      {\texttt{sloped} and \texttt{transform shape}} +(2, 2);
\end{tikzpicture}%
\end{document}

image

@thinbold
Copy link
Contributor Author

thinbold commented Oct 12, 2021

I agree that sloped and transform shape are hard to make sense together.
While all external rotation will be overridden by sloped,
Should the external shearing apply at all? Before or after the rotation?
I don't have a clear picture of the usage of these two options
so any opinions are welcomed.

(E.g., your code above seems to assume that the node is attached, tangentially,
to the line, and then the scaling applies, which makes perfect sense for now.)

@hmenke
Copy link
Member

hmenke commented Nov 2, 2021

@muzimuzhi PR please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants