TeX - LaTeX Asked by user11845919 on February 16, 2021
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}
renewcommandarraystretch{1.333} % optional
[
begin{array}{@{} c | *{6}{c} }
directlua{ self_comparing_matrix ( { 1, 4, 3, 2, 5, 0 } ) }
end{array}
]
end{document}
Correct answer by Mico on February 16, 2021
You can do it using TeX primitives only:
defcom#1{vbox{offinterlineskip
hbox{hbox to3em{hss COMhss}vrule height10pt depth6pt comH#1end}hrule
defcomlist{#1}comA#1end}}
defcomA#1{ifxend#1else
hbox{hbox to3em{hss#1hss}vrule height13pt depth4pt
defcomC{#1}%
expandaftercomDcomlistend}
expandaftercomAfi
}
defcomD#1{ifxend#1else hbox to1.3em{hss
$ifnumcomC<#1+fi ifnumcomC=#1 0fi ifnumcomC>#1-fi$hss}%
expandaftercomDfi
}
defcomH#1{ifx#1endelse hbox to1.3em{hss#1hss}expandaftercomHfi}
com{143250}
end
Answered by wipet on February 16, 2021
A solution working with all engines.
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 (row-2-|col-1) -- (row-2-|col- int_eval:n { l_tmpa_int + 2 } )
(row-1-|col-2) -- (row- int_eval:n { l_tmpa_int + 2 } -|col-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}
Answered by F. Pantigny on February 16, 2021
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}
Answered by egreg on February 16, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP