Texto a mitad de una línea usando coordenadas en un entorno de eje

Texto a mitad de una línea usando coordenadas en un entorno de eje

Quiero indicar texto a mitad de línea en un entorno de eje. Aquí hay un ejemplo de trabajo mínimo:

\documentclass{article}
\usepackage{currfile}
\usepackage{filecontents}
\usepackage{pgfplots}

\usetikzlibrary{calc}

\begin{filecontents*}{batch_output_5.txt}
    NDOF Nelx Nely hnx hny condKBC errabsu errreleu errrelau beta beta_h
    640     9     9 1.111111111111111e-01 1.111111111111111e-01 1.234567890000000e+00 3.360958503340160e-03 6.801367190507313e-04 6.801883693667268e-04 1.234567890000000e+00 1.234567890000000e+00
    2864    19    19 5.263157894736842e-02 5.263157894736842e-02 1.234567890000000e+00 3.290048281511312e-04 6.939215705384151e-05 6.939186823409962e-05 1.234567890000000e+00 1.234567890000000e+00
    6089    29    29 3.448275862068965e-02 3.448275862068965e-02 1.234567890000000e+00 1.950755726148022e-04 4.494377155875972e-05 4.494363189976460e-05 1.234567890000000e+00 1.234567890000000e+00
    10450    39    39 2.564102564102564e-02 2.564102564102564e-02 1.234567890000000e+00 1.818287989540975e-04 3.111970271342298e-05 3.111966516370642e-05 1.234567890000000e+00 1.234567890000000e+00
   \end{filecontents*}

\begin{document}
    \begin{tikzpicture}
        \begin{loglogaxis}
        [
            unit vector ratio=1 1 1,
            %unit vector ratio*=1 1 1,
            ymin=10^-5,
            ymax=10^-2,
        ]
            \addplot table[x index=3,y index=7] {\currfiledir batch_output_5.txt};

            % Triangle coordinates.         
            \pgfplotstablegetelem{0}{[index]3}\of{\currfiledir batch_output_5.txt}
            \edef\triangleAcoordAx{\pgfplotsretval}
            \pgfplotstablegetelem{0}{[index]7}\of{\currfiledir batch_output_5.txt}
            \edef\triangleAcoordAy{\pgfplotsretval}
            \pgfplotstablegetelem{1}{[index]3}\of{\currfiledir batch_output_5.txt}
            \edef\triangleAcoordBx{\pgfplotsretval}
            \pgfplotstablegetelem{1}{[index]7}\of{\currfiledir batch_output_5.txt}
            \edef\triangleAcoordBy{\pgfplotsretval}
            \pgfplotstablegetelem{0}{[index]3}\of{\currfiledir batch_output_5.txt}
            \edef\triangleAcoordCx{\pgfplotsretval}
            \pgfplotstablegetelem{1}{[index]7}\of{\currfiledir batch_output_5.txt}
            \edef\triangleAcoordCy{\pgfplotsretval} 

            \coordinate (offset) at (0.2,-0.2);
            \coordinate (triangleAcoordA) at (axis cs:\triangleAcoordAx,\triangleAcoordAy);
            \coordinate (triangleAcoordB) at (axis cs:\triangleAcoordBx,\triangleAcoordBy);
            \coordinate (triangleAcoordC) at (axis cs:\triangleAcoordCx,\triangleAcoordCy);

            \coordinate (shiftedTriangleAcoordA) at ($(triangleAcoordA)+(offset)$);
            \coordinate (shiftedTriangleAcoordB) at ($(triangleAcoordB)+(offset)$);
            \coordinate (shiftedTriangleAcoordC) at ($(triangleAcoordC)+(offset)$);

            % Draw triangle.            
            \draw[black]    (shiftedTriangleAcoordA)--
                            (shiftedTriangleAcoordB)--
                            (shiftedTriangleAcoordC)--
                            cycle;
            \draw ($(shiftedTriangleAcoordB)+(shiftedTriangleAcoordC)-(shiftedTriangleAcoordB)$) node[anchor=north]{1};
            \draw ($(shiftedTriangleAcoordC)+(shiftedTriangleAcoordA)-(shiftedTriangleAcoordC)$) node[anchor=west]{2};
        \end{loglogaxis}    
    \end{tikzpicture}
\end{document}

Entonces los dos últimos \drawcomandos deberían ser algo como

\draw ($(shiftedTriangleAcoordB)+((shiftedTriangleAcoordC)-(shiftedTriangleAcoordB))/2$) node[anchor=north]{1};

, pero ese comando no se acepta. ¿Cómo puedo solucionar esto?

Unas preguntas secundarias:

1) ¿Existe una forma más elegante de guardar los datos de la tabla en variables LaTeX y usarlos en coordenadas más adelante? Todo parece tan engorroso.

2) El 0.2,-0.2como se usa en la coordenada offset, ¿a dónde se refiere? ¿No creo que sean axis csporcentajes o porcentajes del ancho y alto del gráfico?

Respuesta1

Solución de trabajo en progreso

Hay muchas maneras, de verdad. Podrías hacerlo

\draw ($(shiftedTriangleAcoordC)!0.5!(shiftedTriangleAcoordA)$) node[anchor=west]{2};

para llegar "a la mitad del camino desde shiftedTriangleAcoordCa shiftedTriangleAcoordA", o use nodos directamente en el dibujo del camino (menos repeticiones), como:

% Draw triangle.            
\draw[black] (shiftedTriangleAcoordA)-- node[left,pos=0.5] {halfway}
             (shiftedTriangleAcoordB)--
             (shiftedTriangleAcoordC)-- node[right,pos=0.5] {halfway also}
             cycle;

O utilice la solución de los comentarios.

Entrando a la zona en construcción

Pero, en primer lugar, también preguntaste sobre una mejor manera de definir las coordenadas. La sintaxis también pos=<fraction>se puede utilizar en comandos.\addplot

Continuará...

Respuesta2

Para obtener el texto a mitad de línea, use los comandos

\coordinate (midBC) at ($(shiftedTriangleAcoordB)+0.5*(shiftedTriangleAcoordC)-0.5*(shiftedTriangleAcoordB)$);
\coordinate (midAC) at ($(shiftedTriangleAcoordC)+0.5*(shiftedTriangleAcoordA)-0.5*(shiftedTriangleAcoordC)$);

\draw (midBC) node[anchor=north]{1};
\draw (midAC) node[anchor=west]{2};

Gracias a MaartenDhondt.

información relacionada