在文本和轴环境中使用表格数据

在文本和轴环境中使用表格数据

在一张长表格中,他们告诉我一个粒子实验,第一列是无线电数据,第二列是能量数据。在我的文档中,我需要随机调用表格中的一些条目,将一些数据放在文本和图表中……有人能帮我吗

这是我需要的一个例子

     \documentclass[preview,varwidth ,multi,border=1pt]{standalone}
        \usepackage[dvipsnames,svgnames,x11names,]{xcolor}
        \usepackage{amsmath,mathtools,calculator}
        \usepackage{pgf,tikz,tikz-3dplot,pgfplotstable}
        \usepackage{pgfplots}
        \usepackage[american]{circuitikz}
        \begin{filecontents}{data.txt}
        0.474755603 -2.617688065
        0.480207314 -2.617688065
        0.498670832 -2.613546026
        0.505624541 -2.613546026
        0.531427103 -2.608245746
        0.536149545 -2.608245746
        0.556303011 -2.60379021
        0.567304966 -2.60379021
        0.594408618 -2.540699892
        0.598532384 -2.540699892
        0.631298237 -2.537487595
        0.645293424 -2.537224219
        0.658945477 -2.537224219
        0.67676829  -2.534871304
        0.701739581 -2.534863082
        0.714640691 -2.533026806
        0.738534607 -2.532913819
    ...
        \end{filecontents}

        \begin{document}
        \preview
        The radii of the first element is ¿data(1,1)? and the 165th is ¿data(165,1)?, and the plot shows....

        \begin{tikzpicture}
        \begin{axis}[xlabel={},ylabel={}, axis x line=bottom,
        axis y line=center,
        xmin=-75,
        xmax=60,
        ymin=-3.5,
        ymax=2,     
        ,axis line style={draw=none}, 
        ticks=none
        ]
        \addplot[Silver,mark=none] table[x index=0,y index=1,col sep=tab, restrict x to domain=-60:60 ] {Periodic_Well1.txt}; %this works fine
        \addplot[DeepPink,mark=none, smooth, line width=0.8pt] table[x index=0,y index=2,col sep=tab, restrict x to domain=-60:60 ] {Periodic_Well1.txt}; %this works fine
        \fill[Orange, opacity=0.5] (axis cs:-65,{-pi})-- (axis cs: -65,2)--(axis cs: -70,2)--(axis cs: -70,{-pi})--cycle; %energy scale
----->?       \draw (axis cs:-65,data(2,1)) --(axis cs:-70,data(2,1)) node[right]{$data(2,1)$}; 
----->?        \draw (axis cs:-65,data(2,74)) --(axis cs:-70,data(2,74)) node[right]{$data(2,74)$};
        \end{axis}
        \end{tikzpicture}
        \endpreview
        \end{document}

data(n,m) 表示它是第 n 行第 m 列的条目 谢谢...

答案1

element这是一个获取表元素的函数。语法是element(<row>,<column>),其中和都rowcolumn0 开始。

\documentclass[preview,varwidth ,multi,border=1pt]{standalone}
\usepackage[dvipsnames,svgnames,x11names,]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} 
\begin{filecontents}{data.txt}
0.474755603 -2.617688065
0.480207314 -2.617688065
0.498670832 -2.613546026
0.505624541 -2.613546026
0.531427103 -2.608245746
0.536149545 -2.608245746
0.556303011 -2.60379021
0.567304966 -2.60379021
0.594408618 -2.540699892
0.598532384 -2.540699892
0.631298237 -2.537487595
0.645293424 -2.537224219
0.658945477 -2.537224219
0.67676829  -2.534871304
0.701739581 -2.534863082
0.714640691 -2.533026806
0.738534607 -2.532913819
\end{filecontents}
\pgfmathdeclarefunction{element}{2}{\begingroup
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathtruncatemacro{\myindex}{max(0,#1)}%
\pgfmathtruncatemacro{\mycol}{#2}%
\pgfplotstablegetelem{\myindex}{[index]\mycol}\of\mydata%#1=row, #2=column
\edef\pgfmathresult{\pgfplotsretval}%
\pgfmathsmuggle\pgfmathresult
\endgroup}
\pgfplotstableread{data.txt}\mydata

\begin{document}
\preview
The radii of the first element is $\pgfmathparse{element(1,1)}
\mathtt{data(1,1)}=\pgfmathresult\simeq\pgfmathprintnumber\pgfmathresult$
and the 165th is  \texttt{data(165,1)}, and the plot shows\dots

\begin{tikzpicture}
\begin{axis}[xlabel={},ylabel={}, axis x line=bottom,
axis y line=center,
% xmin=-75,
% xmax=60,
% ymin=-3.5,
% ymax=2,     
,axis line style={draw=none}, 
ticks=none
]
\addplot[Silver,mark=none] 
    table[header=false,x index=0,y index=1,restrict x to domain=-60:60] 
    {data.txt}; %this works fine
% \addplot[DeepPink,mark=none, smooth, line width=0.8pt] 
% table[x index=0,y index=1,col sep=tab, restrict x to domain=-60:60 ] 
% {data.txt}; %this works fine
%\fill[Orange, opacity=0.5] (axis cs:-65,{-pi})-- (axis cs: -65,2)--(axis cs: -70,2)--(axis cs: -70,{-pi})--cycle; %energy scale
% ----->?       
\draw (-1,{element(2,1)}) --(1,{element(2,1)}) coordinate (21); 
\draw (-1,{element(5,1)}) --(1,{element(5,1)}) coordinate (51); 
\end{axis}
\path (21-|current axis.east) node[right]{$\mathtt{data}(2,1)$}
 (51-|current axis.east) node[right]{$\mathtt{data}(5,1)$};
\end{tikzpicture}
\endpreview
\end{document}

在此处输入图片描述

我不得不将其注释掉xmax等等,否则情节几乎看不见。

相关内容