¿Cómo utilizar la biblioteca "calc" para sumar coordenadas?

¿Cómo utilizar la biblioteca "calc" para sumar coordenadas?

¿Por qué el siguiente código dibuja una línea inclinada? Pensé que la suma ($(1,6) + (2,0)$) devolvería un punto (3,6).

\documentclass[dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{pgfplots,tikz}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}
\usepgflibrary{arrows}                                                  
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$}
              ]


    \addplot[very thick, domain=0:11] {5 + x};
    \draw[thick, dashed] (1,6) -- ($(1,6) + (2,0)$);
  \end{axis}
\end{tikzpicture}

\end{document}

El resultado es:

ingrese la descripción de la imagen aquí

¡Gracias de antemano!

Respuesta1

De la documentación de pgfplots:

Para expresar posiciones relativas (o longitudes), es necesario utilizar la dirección del eje cs.

Entonces tienes que usar:

\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);

ingrese la descripción de la imagen aquí

Ejemplo:

\documentclass[dvipsnames]{article}
%\usepackage[utf8]{inputenc}% need for outdated TeX distributions
\usepackage{pgfplots}% loads tikz and xcolor
\pgfplotsset{compat=newest}

\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$}
              ]
    \addplot[very thick, domain=0:11] {5 + x};
    \draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
  \end{axis}
\end{tikzpicture}
\end{document}

Explicación

Dentro del eje las coordenadas usan axis cspor defecto.

De la documentación:

El efecto axis cses aplicar cualquier transformación personalizada (incluida symbolic x coords), logaritmos, transformaciones de escala de datos o cualquier cosa que pgfplots normalmente haga y proporcione como resultado una coordenada pgf de bajo nivel.

La coordenada pgf de bajo nivel se refiere a la coordenada (rel axis cs:0,0). Esta es la esquina inferior izquierda del área del eje (y no el origen del eje). Por tanto axis cslas coordenadas son posiciones absolutas en el eje. Si los agregas obtienes el resultado inesperado:

ingrese la descripción de la imagen aquí

Código:

\documentclass[dvipsnames]{article}
%\usepackage[utf8]{inputenc}% need for outdated TeX distributions
\usepackage{pgfplots}% loads tikz and xcolor
\pgfplotsset{compat=newest}

\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\tikzset{point/.style={circle,fill=black,inner sep=1pt},>=latex'}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$},
              ]
    \addplot[very thick, domain=0:11] {5 + x};
    \path
      (1,6) coordinate(P) node[point,label=above:P]{}
      (2,0) coordinate(Q) node[point,label=above left:Q]{}
      (rel axis cs:0,0) node{x}
    ;
    \draw[blue!50!black] (1,6) -- ($(1,6) + (2,0)$);
  \end{axis}
  \path (0,0) coordinate(O) node[point,label=below:O]{};% origin of the rel axis cs
  \begin{scope}[->,red]
    \draw (O)--(P);
    \draw (O)--(Q);
    \draw (Q)--+(P)node[point]{};
  \end{scope}
\end{tikzpicture}
\end{document}

Esto se puede solucionar usando cualquiera de los dos

\draw[thick, dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);

o

\draw[thick,dashed] (1,6) -- ($(1,6) + (2,0)-(0,0)$);

ingrese la descripción de la imagen aquí

Código:

\documentclass[dvipsnames]{article}
%\usepackage[utf8]{inputenc}% need for outdated TeX distributions
\usepackage{pgfplots}% loads tikz and xcolor
\pgfplotsset{compat=newest}

\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\tikzset{point/.style={circle,fill=black,inner sep=1pt},>=latex'}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$},
                clip=false
              ]
    \addplot[very thick, domain=0:11] {5 + x};
    \draw[thick,blue!50!black] (1,6) -- ($(1,6) + (2,0)$);
    \draw[thick,green!50!black] (1,6) -- ($(1,6) + (2,0)-(0,0)$);
    \draw[thick,dashed] (1,6) -- ($(1,6) + (axis direction cs:2,0)$);
    %
    \path
      (rel axis cs:0,0) coordinate(O) node[point,label=below:O]{}
      (0,0) coordinate(A) node[point,label=above left:A]{}
      (1,6) coordinate(P) node[point,label=above:P]{}
      (2,0) coordinate(Q) node[point,label=above left:Q]{}
    ;
    \begin{scope}[->,red]
      \draw[thick,dotted,cyan] (A)--(O);
      \draw (O)--(P);
      \draw (O)--(Q);
      \draw (Q)--+(P)node[point]{};
      \draw[thick,cyan] (Q) ++(P)-- +($(O)-(A)$)node[point]{};
    \end{scope}
  \end{axis}
\end{tikzpicture}
\end{document}

Respuesta2

Para tener una mejor idea de lo que está pasando, la coordenada (eje cs: 2,0) es la distancia desde (rel eje cs: 0,0). no (eje cs: 0,0). El 5 adicional en la dirección y proviene de [ymin=-5].

\documentclass[dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{pgfplots,tikz}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}
\usepgflibrary{arrows}                                                  
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[ axis lines = middle,
                xmin = -1,
                ymin = -5,
                xmax = 10,
                ymax = 14,
                domain = -1:10,
                xtick = {1,2,...,9},
                ytick = \empty,
                xlabel style={below right},
                ylabel style={above left},
                x tick label style={below},
                samples = 100,
                axis on top=true,
                xlabel = {$x$}, 
                ylabel = {$f$}
              ]


    \addplot[very thick, domain=0:11] {5 + x};
    \coordinate (A) at (1,6);% axis cs: is the default
    \coordinate (B) at (2,0);
    \coordinate (origin) at (0,0);
  \end{axis}
  \draw[thick, dashed] (A) -- ($(A) + (B) - (origin)$);
\end{tikzpicture}

\end{document}

información relacionada