如何创建像这样的自比较矩阵?

如何创建像这样的自比较矩阵?

在此处输入图片描述

我想要创建一个这样的矩阵。有什么方法可以实现这一点?

答案1

我假设您更愿意以编程方式生成此自比较矩阵,而不是使用蛮力方式。如果这个假设是正确的,那么以下解决方案(需要 LuaLaTeX)可能会让您感兴趣。

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{newtxtext,newtxmath} % optional (Times Roman text and math font)

\usepackage{luacode} % for '\luaexec' macro
%% Create a Lua function called 'self_comparing_matrix'
%% The function takes 1 input argument: vector (coded as a Lua table)
\luaexec{
function self_comparing_matrix ( nn )
    %-- Print the header row
    tex.sprint ( "\\mathrm{COM}" )
    for _,v in ipairs (nn) do tex.sprint ( "&"..v ) end
    tex.sprint ("\\\\ \\hline") %-- line break and \hline 
    %-- Print the data rows
    for _,u in ipairs (nn) do
        tex.sprint ( u )
        for _,v in ipairs (nn) do
            if     v==u then tex.sprint ( "& 0" )
            elseif v>u  then tex.sprint ( "& +" )
            else             tex.sprint ( "& -" )
            end
        end
        tex.sprint ("\\\\") %-- line break
    end
end
}
\begin{document}
\renewcommand\arraystretch{1.333} % optional
\[
\begin{array}{@{} c | *{6}{c} }
   \directlua{ self_comparing_matrix ( { 1, 4, 3, 2, 5, 0 } ) }
\end{array}
\]
\end{document}

答案2

您只能使用 TeX 基元来完成此操作:

\def\com#1{\vbox{\offinterlineskip 
   \hbox{\hbox to3em{\hss COM\hss}\vrule height10pt depth6pt \ \comH#1\end}\hrule
   \def\comlist{#1}\comA#1\end}}
\def\comA#1{\ifx\end#1\else
   \hbox{\hbox to3em{\hss#1\hss}\vrule height13pt depth4pt \ 
          \def\comC{#1}%
          \expandafter\comD\comlist\end}
   \expandafter\comA\fi
}
\def\comD#1{\ifx\end#1\else \hbox to1.3em{\hss
   $\ifnum\comC<#1+\fi \ifnum\comC=#1 0\fi \ifnum\comC>#1-\fi$\hss}%
   \expandafter\comD\fi
}
\def\comH#1{\ifx#1\end\else \hbox to1.3em{\hss#1\hss}\expandafter\comH\fi}

\com{143250}

\end

答案3

循环内套循环:

\documentclass{article}
%\usepackage{xparse} % uncomment if using a LaTeX release prior to 2020-10-01

\ExplSyntaxOn

\NewDocumentCommand{\comparisonmatrix}{O{COM}m}
 {
  % #1 is the label in the upper left corner, #2 a comma separated list of values
  \egreg_comparisonmatrix:nn { #1 } { #2 }
 }

\tl_new:N \l__egreg_comparisonmatrix_body_tl
\seq_new:N \l__egreg_comparisonmatrix_data_tl

\cs_new_protected:Nn \egreg_comparisonmatrix:nn
 {
  \seq_set_from_clist:Nn \l__egreg_comparisonmatrix_data_tl { #2 }
  % first row
  \tl_set:Nx \l__egreg_comparisonmatrix_body_tl
   {
    \exp_not:n { #1 }
    &
    \seq_use:Nn \l__egreg_comparisonmatrix_data_tl { & }
    \exp_not:n { \\ \hline }
   }
  \seq_map_inline:Nn \l__egreg_comparisonmatrix_data_tl
   {
    % row index
    \tl_put_right:Nn \l__egreg_comparisonmatrix_body_tl { ##1 \vphantom{$\Big|$} }
    % add the other items
    \seq_map_inline:Nn \l__egreg_comparisonmatrix_data_tl
     {
      \int_compare:nTF { ####1 > ##1 }
       {% column index > row indes: add a +
        \tl_put_right:Nn \l__egreg_comparisonmatrix_body_tl { & $+$ }
       }
       {
        \int_compare:nTF { ####1 < ##1 }
         {% column index < row index: add a -
          \tl_put_right:Nn \l__egreg_comparisonmatrix_body_tl { & $-$ }
         }
         {% column index = row indes: add a 0
          \tl_put_right:Nn \l__egreg_comparisonmatrix_body_tl { & 0 }
         }
       }
     }
    % end the row
    \tl_put_right:Nn \l__egreg_comparisonmatrix_body_tl { \\ }
   }
  % print the table
  \begin{tabular}{c | *{ \seq_count:N \l__egreg_comparisonmatrix_data_tl } { c } }
  \l__egreg_comparisonmatrix_body_tl
  \end{tabular}
 }

\ExplSyntaxOff

\begin{document}

\[
\comparisonmatrix{1,4,3,2,5,0}
\quad
\comparisonmatrix[$x$]{0,1,2,3,4}
\]

\end{document}

在此处输入图片描述

答案4

适用于所有引擎的解决方案。

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \ComparisonMatrix { m }
  {
    \seq_set_from_clist:Nn \l_tmpa_seq { #1 }
    \int_set:Nn \l_tmpa_int { \seq_count:N \l_tmpa_seq }
    \AutoNiceMatrix
      [ 
        code-after = 
         \tikz \draw (2-|1) -- (2-| \int_eval:n { \l_tmpa_int + 2 } )  
                     (1-|2) -- ( \int_eval:n { \l_tmpa_int + 2 } -|2) ;
      ]
      { \int_eval:n { \l_tmpa_int + 1 } - \int_eval:n { \l_tmpa_int + 1 } }
      { 
        \scan_stop:
        \int_compare:nNnTF \c@iRow = 1
          {
            \int_compare:nNnTF \c@jCol = 1
              { \hbox:n { COM } }
              { \seq_item:Nn \l_tmpa_seq { \c@jCol -1 } }
          }
          {
            \int_compare:nNnTF \c@jCol = 1
              { \seq_item:Nn \l_tmpa_seq { \c@iRow - 1 } }
              {
                \int_compare:nNnTF 
                  { \seq_item:Nn \l_tmpa_seq { \c@iRow - 1 } } 
                  = 
                  { \seq_item:Nn \l_tmpa_seq { \c@jCol - 1 } } 
                  0
                  { 
                    \int_compare:nNnTF 
                      { \seq_item:Nn \l_tmpa_seq { \c@iRow - 1 } } 
                      >
                      { \seq_item:Nn \l_tmpa_seq { \c@jCol - 1 } } 
                      - +  
                  }   
              }
          }
      }
  }
\ExplSyntaxOff
\makeatother

$\ComparisonMatrix{ 1 , 4 , 3 , 2 , 5 , 0 }$

\end{document}

上述代码的输出

相关内容