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.
Asked
Active
Viewed 298 times
1 Answers
11
Below I present two possibilities.
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}
This answer is a variation of
my answerto Is there a way to make a certain character in a Verbatim have a certain formatting?.Here's a different approach using this time the
collcellpackage: \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}
Gonzalo Medina
- 505,128
-
@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?. – Gonzalo Medina Jul 11 '14 at 17:46
\documentclass{...}and ending with\end{document}. – cfr Jul 11 '14 at 02:03