How to create a self-comparing matrix like this?
I'm assuming you would prefer to generate this self-comparing matrix programmatically rather than the brute-force way. If this assumption is correct, the following solution -- which requires LuaLaTeX -- may be of interest to you.
% !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}
You can do it using TeX primitives only:
\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
With a cycle inside a cycle:
\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}