使用轴环境中的坐标将文本置于行的中间

使用轴环境中的坐标将文本置于行的中间

我想在轴环境中表示一行中间的文本。这是一个最小工作示例:

\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}

所以最后两个\draw命令应该是这样的

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

,但该命令未被接受。我该如何解决这个问题?

次要问题:

1) 有没有更优雅的方法将表中的数据保存在 LaTeX 变量中,以便稍后在坐标中使用它们?这一切看起来都很麻烦。

2)0.2,-0.2坐标中使用的 asoffset指的是哪里?我不认为它是axis cs图形宽度和高度的百分比?

答案1

正在进行的解决方案

确实有很多方法。你可以这样做

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

获取“从shiftedTriangleAcoordC到路径的一半shiftedTriangleAcoordA”,或者直接在路径绘制中使用节点(减少重复),例如:

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

或者使用评论中的解决方案。

进入施工区域

但您首先也询问了定义坐标的更好方法。该语法也pos=<fraction>可以在命令中使用。\addplot

待续...

答案2

要获取行中间的文本,请使用命令

\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};

感谢 MaartenDhondt。

相关内容