5

Consider this code:

\documentclass{article}

\usepackage{tikz}

\begin{document}

Sloped label with scaling:

\begin{tikzpicture}[yscale=-1]
  \draw (0,0) -- (2,2) node[midway,sloped] {Label};
\end{tikzpicture}

Sloped label without scaling:

\begin{tikzpicture}
  \draw (0,0) -- (2,2) node[midway,sloped] {Label};
\end{tikzpicture}

\end{document}

This is the output:

Output

So it seems that the sloped option somehow ignores the global coordinate transform matrix. There's nothing particular about yscale=-1; you can try with something else as well. It seems like the slope is computed using the literal node coordinates instead of the actual positions.

So is this a bug or am I doing something wrong? How do I get a properly sloped label?

I'm using the tikz package from MacLive 2017 (how do I check the exact version?).

3 Answers3

5

Update

By default transformations do not apply to nodes (see the documentation) and sloped is used as a node option. So your issue seems to be no bug.

You could use

sloped/.append style={transform shape,yscale=-1}

for the tikzpicture. This affects all sloped nodes in the picture.

Example:

\documentclass{article}
\usepackage{tikz}

\begin{document}

Sloped label with scaling:

\begin{tikzpicture}[yscale=-1,sloped/.append style={transform shape,yscale=-1}]
  \draw (0,0)node{x} -- (2,2) node[midway,sloped]{Label};
\end{tikzpicture}

\end{document}

Result:

enter image description here


Original answer

Here is another suggestion (affects all nodes in the picture):

\documentclass{article}
\usepackage{tikz}

\begin{document}

Sloped label with scaling:

\begin{tikzpicture}[yscale=-1,transform shape,nodes={yscale=-1}]
  \draw (0,0)node{x} -- (2,2) node[midway,sloped] {Label};
\end{tikzpicture}

\end{document}

Or (affects a single node):

\documentclass{article}
\usepackage{tikz}

\begin{document}

Sloped label with scaling:

\begin{tikzpicture}[yscale=-1]
  \draw (0,0)node{x} -- (2,2) node[midway,sloped,transform shape,yscale=-1] {Label};
\end{tikzpicture}

\end{document}
esdd
  • 85,675
  • That obtains the desired output quite cleanly, thanks. But a main point of the question was also to know if the behavior is expected or if that's a bug. What do you think about it? – Nicola Gigante Nov 02 '18 at 15:13
  • I think the behavior is expected. See my updated answer. – esdd Nov 02 '18 at 17:06
4

One option is to use decorations.markings for that. (I do not get the desired result with transform shape, which has been suggested by esdd and is a great idea in general.)

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\begin{document}

Sloped label with scaling and \verb|decorations.markings|:

\begin{tikzpicture}[yscale=-1]
  \draw[postaction=decorate,decoration={markings,
  mark=at position 0.5 with {\path (0,0) coordinate (aux0) (1,0) coordinate
  (aux1);
  \pgftransformreset
  \path let \p1=($(aux1)-(aux0)$),\n1={atan2(\y1,\x1)} in 
  (aux0) node[rotate=\n1]{Label};} }] (0,0) -- (2,2);
\end{tikzpicture}

Sloped label with scaling and \verb|transform shape|:

\begin{tikzpicture}[yscale=-1,transform shape]
  \draw (0,0) -- (2,2) node[midway,sloped] {Label};
\end{tikzpicture}


Sloped label without scaling:

\begin{tikzpicture}
  \draw (0,0) -- (2,2) node[midway,sloped] {Label};
\end{tikzpicture}

\end{document}

enter image description here

  • That is really more complex that a midway node, though.. – Nicola Gigante Nov 02 '18 at 15:05
  • @gigabytes Yes, it is. To be fair, one should say that transform shape would work fine if you were to add another yscale=-1, i.e. \begin{tikzpicture}[yscale=-1,transform shape] \draw (0,0) -- (2,2) node[midway,sloped,yscale=-1] {Label}; \end{tikzpicture}. And thanks for your legal advice! –  Nov 02 '18 at 15:23
  • Beware I got interested into copyright/patents issues because of a software engineering background but I'm not a lawyer! Back to the question though, do you think this behavior of the sloped option is expected? – Nicola Gigante Nov 02 '18 at 15:35
  • @gigabytes I do not know what "expected" really means here, and I can see how one may think of this as "unexpected". Yet esdd's answer and, to some extent also my answer, show how to get the desired result. I –  Nov 02 '18 at 15:41
  • Yes, indeed I’ll accept that answer if nothing more explanatory comes out. Btw, by “expected” I mean “described by the manual”, or “implied by things described by the manual”. The explanation of the sloped option does not mention the fact that the transform matrix is ignored. But maybe that’s simply a consequence of something basic that I do not know. Someone knowing tikz internals might illuminate us. – Nicola Gigante Nov 02 '18 at 16:06
2

Using transform shape works, but maybe the result is unexpected:

\documentclass{article}
\usepackage{tikz}
\begin{document}

Sloped label with scaling:

\begin{tikzpicture}[yscale=-1]
  \draw (0,0) -- (2,2) node[midway,sloped, transform shape] {Label};
\end{tikzpicture}

Sloped label without scaling:

\begin{tikzpicture}
  \draw (0,0) -- (2,2) node[midway,sloped] {Label};
\end{tikzpicture}

\end{document}

enter image description here

...you are mirroring vertically everything...

Rmano
  • 40,848
  • 3
  • 64
  • 125