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.

1
  • 1
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – cfr
    Jul 11, 2014 at 2:03

1 Answer 1

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

1
  • @Erfan You're welcome. Don't forget that, after a sensible wait, you can accept an answer that you consider solved your problem by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. Jul 11, 2014 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .