如何在 tikz 中比较数组的两个元素

如何在 tikz 中比较数组的两个元素

** 我需要比较数组的两个元素(char),但我不能:

请问你能帮帮我吗!!

如何将元素保存在新数组中?例如 {B&A&D&C&} **

  \documentclass{article}


    \usepackage{arrayjob}

    \usepackage{calc}
    \usepackage{ifthen}
    \usepackage{tikz}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}

\usetikzlibrary{automata,positioning}

\begin{document}
\begin{tikzpicture}
\def \length {7} 
\newarray\newarray
\readarray{newarray}{%
B&B&%
A&B&%
A&A&%
B&D&%
C&C&%
C&A&%
D&D&
}

\dataheight=2
\foreach \x in {1,...,\length}
{

\draw (\x,-6) node {\x};
\draw (\x,-5) node {\newarray(\x,1)};
\draw (\x,-4) node {\newarray(\x,2)};

    \expandafter\ifx\expandafter{\newarray(\x,1)} \expandafter{\newarray(\x,2)}
    \draw (\x,0) node {\newarray(\x,1)};
    \else 
    \draw (\x,2) node {\newarray(\x,2)};
    \fi

}

\end{tikzpicture}
\end{document}

答案1

我基本上使用的是手册第 11 页中的示例。如果使用而不是 ,arrayjobx代码也会起作用。arrayjobarrayjobx

\documentclass{article}
\usepackage{arrayjobx} % <-changed but also works with arrayjob
\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usetikzlibrary{automata,positioning}
\begin{document}
\begin{tikzpicture}
\def \length {7} 
\newarray\myarray %\newarray\newarray is dangerous and prevents you from using \newarray at later occasions
\readarray{myarray}{%
B&B&%
A&B&%
A&A&%
B&D&%
C&C&%
C&A&%
D&D&
}

\dataheight=2
\foreach \x in {1,...,\length}
{

\draw (\x,-6) node {\x};
\draw (\x,-5) node {\myarray(\x,1)};
\draw (\x,-4) node {\myarray(\x,2)};

\checkmyarray(\x,1)% \cachedata is now the (\x,1) element
\edef\myx{\cachedata}% save the element in \myx
\checkmyarray(\x,2)% \cachedata is now the (\x,2) element

    \ifx\myx\cachedata\relax
    \draw (\x,0) node {\myarray(\x,1)};
    \else 
    \draw (\x,2) node {\myarray(\x,2)};
    \fi

}

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我认为你不应该使用arrayjobx(绝对不应该arrayjob)。

以下是一个实现xparse

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\definearray}{O{1}mm}
 {% #1 = height of array, #2 = name, #3 = data
  \seq_set_from_clist:Nn \l__ajx_array_temp_seq { #3 }
  \int_new:c { l_ajx_array_#2_int }
  \int_step_inline:nnnn { 1 } { 1 } { #1 }
   {
    \seq_new:c { l_ajx_array_#2_##1_seq }
    \seq_set_split:cnx { l_ajx_array_#2_##1_seq } { , } { \seq_item:Nn \l__ajx_array_temp_seq { ##1 } }
    \int_set:cn { l_ajx_array_#2_int } { \seq_count:c { l_ajx_array_#2_##1_seq } }
   }
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { cnx }
\seq_new:N \l__ajx_array_temp_seq

\NewExpandableDocumentCommand{\getarrayitem}{mmm}
 {% #1 = array name, #2 = first index, #3 = second index
  \seq_item:cn { l_ajx_array_#1_#3_seq } { #2 }
 }

\NewExpandableDocumentCommand{\getarraylength}{m}
 {
  \int_use:c { l_ajx_array_#1_int }
 }

\NewExpandableDocumentCommand{\equalitemsTF}{mmmm}
 {% #1 = first item, #2 = second item, #3 = true text, #4 = false text
  \str_if_eq_x:nnTF { #1 } { #2 } { #3 } { #4 }
 }

\ExplSyntaxOff

\definearray[2]{letters}{
  {B,A,A,B,C,C,D},
  {B,B,A,D,C,A,D}
}

\begin{document}

\begin{tikzpicture}
\foreach \x in {1,...,\getarraylength{letters}}{
  \draw (\x,-6) node {\x};
  \draw (\x,-5) node {\getarrayitem{letters}{\x}{1}};
  \draw (\x,-4) node {\getarrayitem{letters}{\x}{2}};

  \equalitemsTF{\getarrayitem{letters}{\x}{1}}{\getarrayitem{letters}{\x}{2}}
   {\draw (\x,0) node {\getarrayitem{letters}{\x}{1}};}
   {\draw (\x,2) node {\getarrayitem{letters}{\x}{2}};}
}
\end{tikzpicture}

\end{document}

索引是“列-行”的;如果您喜欢“行-列”样式,可以进行更改。

在此处输入图片描述

技术细节。如果数组声明为n行,我定义n序列并将\getarrayitem挑选出好的一个。请注意,\getarrayitem是完全可扩展的,与 定义的宏相反arrayjobx

相关内容