比较数组中的元素不起作用

比较数组中的元素不起作用

在这个例子中,如果第 2 列的元素相等,我希望在两个矩阵之间画一条线。但是 ifthenelse 语句听起来总是假,所以两个矩阵之间没有画线!

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\usepackage{etoolbox}
\usepackage{ifthen}
\usepackage{eqparbox}
\def\JLST{%
    {0,low,red},
    {1,high,blue},
    {7,qvga,green}%
}
\def\MLST{%
    {23,qvga,320,240},
    {45,high,1920,1080},
    {38,low,320,240}%
}
\newbox\matrixcellbox
\tikzset{center align per column/.style={nodes={execute at begin
            node={\setbox\matrixcellbox=\hbox\bgroup},
            execute at end
            node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][c]{\copy\matrixcellbox}}}},
}
\newcommand{\TBL}[3][M] {%[#1]{name}{list}
    \let\desc\empty
    \foreach \col in #3 {%
        \foreach \row [count=\nc] in \col {
            \ifnum\nc > 1%
            \expandafter\gappto\expandafter\desc\expandafter{\&}
            \fi%
            \expandafter\gappto\expandafter\desc\expandafter{\row}%
        }%
        \xappto\desc{\\}%  
    }%
    \matrix [#1,
    nodes={draw,thin,anchor=center,inner sep=2pt,
        text depth={depth("g")},text height={height("H")}},
    center align per column,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes in empty cells,
    matrix of nodes,ampersand replacement=\&] (#2) {
        \desc
    };
}
\begin{document}
    \tiny\begin{tikzpicture}
    \def\dx{1cm}
    \TBL[]{MJ}{\JLST};
    \TBL[right=\dx of MJ]{MM}{\MLST};
    \foreach \colm [count=\idm] in \MLST {
        \foreach \colj [count=\idj] in \JLST {
            \ifthenelse{\equal{\colm[1]}{\colj[1]}}{
                    \draw (MJ-\idj-3.east) -- (MM-\idm-1.west);
            }{}
        }
    }
    \end{tikzpicture}
\end{document} 

输出:

在此处输入图片描述

答案1

您似乎希望从列表中提取索引元素1(根据 pgf 计数,这是第二个元素),然后进行比较。这个问题已在这个答案。 你不需要ifthen

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\usepackage{etoolbox}
\usepackage{eqparbox}
% from https://tex.stackexchange.com/a/225192
\def\addquotes#1,#2\relax{"#1",\if\relax#2\relax\else\addquotes#2\relax\fi}
\def\JLST{%
    {0,low,red},
    {1,high,blue},
    {7,qvga,green}%
}
\def\MLST{%
    {23,qvga,320,240},
    {45,high,1920,1080},
    {38,low,320,240}%
}
\newbox\matrixcellbox
\tikzset{center align per column/.style={nodes={execute at begin
            node={\setbox\matrixcellbox=\hbox\bgroup},
            execute at end
            node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][c]{\copy\matrixcellbox}}}},
}
\newcommand{\TBL}[3][M] {%[#1]{name}{list}
    \let\desc\empty
    \foreach \col in #3 {%
        \foreach \row [count=\nc] in \col {
            \ifnum\nc > 1%
            \expandafter\gappto\expandafter\desc\expandafter{\&}
            \fi%
            \expandafter\gappto\expandafter\desc\expandafter{\row}%
        }%
        \xappto\desc{\\}%  
    }%
    \matrix [#1,
    nodes={draw,thin,anchor=center,inner sep=2pt,
        text depth={depth("g")},text height={height("H")}},
    center align per column,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes in empty cells,
    matrix of nodes,ampersand replacement=\&] (#2) {
        \desc
    };
}
\begin{document}
    \tiny\begin{tikzpicture}
    \def\dx{1cm}
    \TBL[]{MJ}{\JLST};
    \TBL[right=\dx of MJ]{MM}{\MLST};
    \foreach \colm [count=\idm] in \MLST {
        \foreach \colj [count=\idj] in \JLST {          
            \pgfmathsetmacro\tempa{{\expandafter\addquotes\colm,\relax}[1]}%
            \pgfmathsetmacro\tempb{{\expandafter\addquotes\colj,\relax}[1]}%
            \ifx\tempa\tempb\relax
                %\typeout{\tempa=\tempb}
                \draw (MJ-\idj-3.east) -- (MM-\idm-1.west);
            \else
                %\typeout{\tempa!=\tempb}
            \fi     
        }
    }
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容