如何读取用于节点位置的表格

如何读取用于节点位置的表格

假设 .txt 文件中有一个两列表格:

第一的

如我们所见,该表有一个我们跳过的标题 xy。下一行是节点 A 在 (0, 0) 的位置。类似地,下一行将是节点 B 在 (1,1) 的位置。我正在寻找一个 tikz 文件,在其中我可以直接从 .txt 表中读取 (0,0) 和 (1,1) 并将它们分配给节点 A 和 B 的位置。下面是我无法读取表格的示例代码:

\begin{filecontents}{testOne.txt}
    x   y
    0   0
    1   1
\end{filecontents}


  \documentclass[border=1cm]{standalone}
 \usepackage{tikz}
 \begin{document}
 \begin{tikzpicture}
   \node  at (0,0) (A){}; 
 \filldraw (A) circle (1pt);
 \node [anchor= west] at (A){$A$};

 \node  at (1,1) (B){}; 
 \filldraw (B) circle (1pt);
 \node [anchor= west] at (B){$B$};
 \end{tikzpicture}
 \end{document}

期望的结果是: 第二

您知道如何读取该表并将其用于我的节点的位置吗?

答案1

x对于第一列中的这个特定表头使用包listofitems

\documentclass[border=1cm]{standalone}

\usepackage{tikz}

\begin{filecontents}[overwrite,noheader]{testOne.txt}
    x   y
    0   0
    1   1
\end{filecontents}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listofitems} % split space separated items
% \fileopenr{<file stream>}{<file name>}, opens file for reading
\newcommand\fileopenr[2]{%
  \newread#1%
  \immediate\openin#1=#2%
}
% \readtolist{<file stream>}{\list}
%   reads a line from file stream and splits at ' ' into \list[1], \list[2], ...
\newcommand\readtolist[2]{%
  \begingroup%
    \immediate\read#1 to \inputline%
    \ifeof#1
      \immediate\closein#1%
    \else%
      \setsepchar{ }%
      \greadlist*#2\inputline%
      \def\x{x}\edef\first{#2[1]}%
      \ifx\first\x% skip table header
        \readtolist{#1}{#2}%
      \fi%  
    \fi%
  \endgroup%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\fileopenr{\infile}{testOne.txt}
\begin{tikzpicture}
  \readtolist{\infile}{\table}
  \node  at (\table[1],\table[2]) (A){}; 
  \filldraw (A) circle (1pt);
  \node [anchor= west] at (A){$A$};

  \readtolist{\infile}{\table}
  \node  at (\table[1],\table[2]) (B){}; 
  \filldraw (B) circle (1pt);
  \node [anchor= west] at (B){$B$};
\end{tikzpicture}
\end{document}

答案2

我不是解释专家,我建议

  • 读取第一行(不处理)

  • 一次一行,-用空格分隔序列中的 x 和 y -为第一个点创建一个名为 P1 的节点,P2... (编辑2:我忘了关闭文件)

     \begin{filecontents*}{testOne.txt}
         x   y
         0   0 
         1   1
     \end{filecontents*}
     \documentclass[border=1cm]{standalone}
     %https://tex.stackexchange.com/questions/641621/how-to-read-a-table-to-use-for-node-locations
    
     \usepackage{tikz}
     \tikzset{
         mynode/.style={
         fill=black,
         circle,
         inner sep=0pt,
         outer sep=0pt,
         minimum width=1mm}
         }
     \ExplSyntaxOn
     \ior_new:N \g_yourmodule_ior
     \seq_new:N \l__yourmodule_coordonnees_seq
     \int_new:N \l__yourmodule_numPoint_int% for P1, P2
     \NewDocumentCommand \myPoints { m }
     {       % #1--> name of file
         \ior_open:Nn \g_yourmodule_ior { #1 }
         \ior_get:NN \g_yourmodule_ior \l_tmpa_tl%<-- the first line
         \int_set:Nn \l__yourmodule_numPoint_int { 0 }
         \ior_map_inline:Nn \g_yourmodule_ior
             {%  one line at a time 
             \seq_set_split:Nnn \l__yourmodule_coordonnees_seq { } { ##1 }% separate x and y by the space
             \int_incr:N \l__yourmodule_numPoint_int
             \node[mynode] (P \int_eval:n {\l__yourmodule_numPoint_int}) at (\seq_item:Nn \l__yourmodule_coordonnees_seq { 1 },\seq_item:Nn \l__yourmodule_coordonnees_seq { 2 }){};
             }
         \ior_close:N \g_yourmodule_ior%<-- 
     }
     \ExplSyntaxOff
     \begin{document}
    
     \begin{tikzpicture}
         \myPoints {testOne.txt}
         \node[right] at(P1){$A$};
         \node[right] at(P2){$B$};
     \end{tikzpicture}
     \end{document}
    

答案3

您可以PGFPlots使用其表解析机制来执行此操作:

\begin{filecontents}{testOne.txt}
    x   y
    0   0
    1   1
\end{filecontents}

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\pgfplotstableread{testOne.txt}\coordinatetable
\pgfplotstablegetrowsof{\coordinatetable}
\pgfmathtruncatemacro\coordinatecount{\pgfplotsretval - 1}

\begin{document}
\begin{tikzpicture}

    \pgfplotsinvokeforeach{0,...,\coordinatecount}{
        \pgfplotstablegetelem{#1}{x}\of{\coordinatetable}
        \pgfmathtruncatemacro\tempx{\pgfplotsretval}
        \pgfplotstablegetelem{#1}{y}\of{\coordinatetable}
        \pgfmathtruncatemacro\tempy{\pgfplotsretval}
        \coordinate (n#1) at (\tempx,\tempy); 
        \filldraw (n#1) circle (1pt);
        \node[anchor=west] at (n#1) {\symbol{\numexpr65+#1}};
    }

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容