5

How can I replace the matrix element by colored squared box ? Suppose I have a 10x10 matrix with entries{+1,-1}. I want to put a black square box for +1 and red square box got -1 which will give me a design. Anyone can provide me clear idea ? Thanks in advance.

Erfan
  • 59

1 Answers1

11

Below I present two possibilities.

  1. You don't even need the 1s; it's enough to have an array of + and - characters:

    \documentclass{article}
    \usepackage{fancyvrb}
    \usepackage{xcolor}
    
    \makeatletter
    \newenvironment{MyVerb}
      {
        \def\verbatim@nolig@list{\do\`\do\<\do\>\do\,\do\'}
        \catcode`+=\active
        \catcode`-=\active
        \setlength\fboxsep{6pt}
        \offinterlineskip
        \VerbatimEnvironment
        \begin{Verbatim}
      }
      {\end{Verbatim}}
    \makeatother
    
    \begin{document}
    
    \begin{MyVerb}[defineactive=%
        \def+{\colorbox{black}{\phantom{a}}}
        \def-{\colorbox{red!70!black}{\phantom{a}}}]
    +-+-+-+-+-
    -+-+-+-+-+
    +-+-+-+-+-
    -+-+-+-+-+
    +-+-+-+-+-
    -+-+-+-+-+
    +-+-+-+-+-
    -+-+-+-+-+
    +-+-+-+-+-
    -+-+-+-+-+
    \end{MyVerb}
    
    \begin{MyVerb}[defineactive=%
        \def+{\colorbox{black}{\phantom{a}}}
        \def-{\colorbox{red!70!black}{\phantom{a}}}]
    +---+---+-
    --+---+---
    -+++-+++-+
    --+---+---
    +---+---+-
    ++-+++-+++
    +---+---+-
    --+---+---
    -+++-+++-+
    --+---+---
    \end{MyVerb}
    
    \begin{MyVerb}[defineactive=%
        \def+{\colorbox{black}{\phantom{a}}}
        \def-{\colorbox{red!70!black}{\phantom{a}}}]
    ++++++++++
    +---++---+
    +-+-++-+-+
    +++-++-+++
    ----++----
    ----++----
    +++-++-+++
    +-+-++-+-+
    +---++---+
    ++++++++++
    \end{MyVerb}
    
    \end{document}
    

    enter image description here

    This answer is a variation of my answer to Is there a way to make a certain character in a Verbatim have a certain formatting?.

  2. Here's a different approach using this time the collcell package: \documentclass{article} \usepackage[table]{xcolor} \usepackage{collcell}

    \def\Side{8pt}
    
    \newcommand\ColCell[1]{%
      \ifnum#1=-1\relax
        \gdef\mycolor{red!70!black}
      \else
      \ifnum#1=1\relax
        \gdef\mycolor{black}
      \fi\fi 
      \expandafter\expandafter\cellcolor{\mycolor}%
        \parbox[c][\dimexpr\Side+2\tabcolsep\relax][c]{\Side}{}%
    }
    
    \newcolumntype{E}{>{\centering\arraybackslash\collectcell\ColCell}p{\Side}<{\endcollectcell}}
    
    \begin{document}
    
    \noindent\begin{tabular}{*{10}{E}}
    +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 \\
    -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 \\
    +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 \\
    -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 \\
    +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 \\
    -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 \\
    +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 \\
    -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 \\
    +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 \\
    -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 & -1 & +1 \\
    \end{tabular}\qquad        
    \noindent\begin{tabular}{*{10}{E}}
    +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 \\
    -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 \\
    -1 & +1 & +1 & +1 & -1 & +1 & +1 & +1 & -1 & +1 \\
    -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 \\
    +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 \\
    +1 & +1 & -1 & +1 & +1 & +1 & -1 & +1 & +1 & +1 \\
    +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 \\
    -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 \\
    -1 & +1 & +1 & +1 & -1 & +1 & +1 & +1 & -1 & +1 \\
    -1 & -1 & +1 & -1 & -1 & -1 & +1 & -1 & -1 & -1 \\
    \end{tabular}\bigskip
    
    \noindent\begin{tabular}{*{10}{E}}
    +1 & +1 & +1 & +1 & +1 & +1 & +1 & +1 & +1 & +1 \\
    +1 & -1 & -1 & -1 & +1 & +1 & -1 & -1 & -1 & +1 \\
    +1 & -1 & +1 & -1 & +1 & +1 & -1 & +1 & -1 & +1 \\
    +1 & +1 & +1 & -1 & +1 & +1 & -1 & +1 & +1 & +1 \\
    -1 & -1 & -1 & -1 & +1 & +1 & -1 & -1 & -1 & -1 \\
    -1 & -1 & -1 & -1 & +1 & +1 & -1 & -1 & -1 & -1 \\
    +1 & +1 & +1 & -1 & +1 & +1 & -1 & +1 & +1 & +1 \\
    +1 & -1 & +1 & -1 & +1 & +1 & -1 & +1 & -1 & +1 \\
    +1 & -1 & -1 & -1 & +1 & +1 & -1 & -1 & -1 & +1 \\
    +1 & +1 & +1 & +1 & +1 & +1 & +1 & +1 & +1 & +1 \\
    \end{tabular}
    
    \end{document}
    

    enter image description here

Gonzalo Medina
  • 505,128