答案1
含钛钾Z 这相当简单。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[0/.style={draw,ultra thin},1/.style={0,fill=black}]
\matrix[matrix of nodes,cells={minimum size=1.5em,anchor=center}]
{|[0]| & |[1]| & |[0]| \\
|[1]| & |[0]| & |[1]|\\
|[0]| & |[1]| & |[0]|\\
};
\end{tikzpicture}
\end{document}
如果你有一个简单的模式,你也可以这样做
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[my cell/.style={/utils/exec={%
\pgfmathtruncatemacro{\itest}{mod(\the\pgfmatrixcurrentrow+\the\pgfmatrixcurrentcolumn,2)}
\ifnum\itest=1
\pgfkeysalso{/tikz/fill=black}
\fi}}]
\matrix[matrix of nodes,nodes in empty cells,
nodes={minimum size=1.5em,anchor=center,draw,ultra thin,my cell}]
{ & & \\
& & \\
& & \\
};
\end{tikzpicture}
\end{document}
附录,只是为了好玩。它与@egreg 的回答很好事实上\sparsezero
,\sparseone
环境的名称就是从那里偷来的。不同之处在于,它没有使用 0 和 1 个活动字符,这也是一种黑客行为,但可以说不那么暴力。它定义了一种只使用宏的新列类型。但是,将条目扩展到更大的值将和向 中collcell
添加几个 s 一样容易,所以我觉得这可能比 egreg 的概念上构建的优秀解决方案更容易定制。\or
\ifcase
\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{collcell}
\newlength{\sparsesize}
\setlength{\sparsesize}{12pt}
\newcommand{\sparsezero}{%
\begingroup
\setlength{\fboxsep}{-0.2pt}%
\setlength{\fboxrule}{0.2pt}%
\fbox{\hspace{\sparsesize}\rule{0pt}{\sparsesize}}%
\endgroup
}
\newcommand{\sparseone}{\rule{\sparsesize}{\sparsesize}}
\newcommand{\sparseentry}[1]{\ifcase#1
\sparsezero
\or
\sparseone
\fi}
\newcolumntype{F}{>{\collectcell\sparseentry}c<{\endcollectcell}}
\newenvironment{sparsematrix}
{%
\renewcommand{\arraycolsep}{0pt}%
\renewcommand{\arraystretch}{0}%
\begin{array}{*{20}{F}}%
}
{\end{array}}
\begin{document}
\[
\begin{sparsematrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0 \\
\end{sparsematrix}=\begin{pmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0 \\
\end{pmatrix}
\]
\end{document}
答案2
语法相当自然:
\documentclass{article}
\usepackage{amsmath}
\newlength{\sparsesize}
\setlength{\sparsesize}{12pt}
\newcommand{\sparsezero}{%
\begingroup
\setlength{\fboxsep}{-0.2pt}%
\setlength{\fboxrule}{0.2pt}%
\fbox{\hspace{\sparsesize}\rule{0pt}{\sparsesize}}%
\endgroup
}
\newcommand{\sparseone}{\rule{\sparsesize}{\sparsesize}}
\newcommand{\activate}[2]{%
\begingroup\lccode`~=`#1\lowercase{\endgroup\let~}#2%
\mathcode`#1="8000
}
\newenvironment{sparsematrix}
{%
\renewcommand{\arraystretch}{0}%
\setlength{\arraycolsep}{0pt}%
\activate{0}{\sparsezero}\activate{1}{\sparseone}%
\begin{matrix}%
}
{\end{matrix}}
\begin{document}
\[
\begin{pmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{pmatrix}
=
\begin{sparsematrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{sparsematrix}
\]
\end{document}
对于具有整数系数的一般矩阵,这有点困难。
\documentclass{article}
\usepackage{amsmath,xparse}
\newlength{\sparsesize}
\setlength{\sparsesize}{12pt}
\newcommand{\sparsezero}{%
\begingroup
\setlength{\fboxsep}{-0.2pt}%
\setlength{\fboxrule}{0.2pt}%
\fbox{\hspace{\sparsesize}\rule{0pt}{\sparsesize}}%
\endgroup
}
\newcommand{\sparseone}{\rule{\sparsesize}{\sparsesize}}
\ExplSyntaxOn
\NewDocumentEnvironment{sparsematrix}{b}
{
\renewcommand{\arraystretch}{0}%
\setlength{\arraycolsep}{0pt}%
{% make a subformula
\begin{matrix}
\eagleone_sparsematrix:n { #1 }
\end{matrix}
}
}{}
\seq_new:N \l__eagleone_sparsematrix_rows_seq
\seq_new:N \l__eagleone_sparsematrix_row_in_seq
\seq_new:N \l__eagleone_sparsematrix_row_out_seq
\cs_new_protected:Nn \eagleone_sparsematrix:n
{
\seq_set_split:Nnn \l__eagleone_sparsematrix_rows_seq { \\ } { #1 }
\seq_map_function:NN \l__eagleone_sparsematrix_rows_seq \__eagleone_sparsematrix_row:n
}
\cs_new_protected:Nn \__eagleone_sparsematrix_row:n
{
\seq_set_split:Nnn \l__eagleone_sparsematrix_row_in_seq { & } { #1 }
\seq_map_inline:Nn \l__eagleone_sparsematrix_row_in_seq
{
\int_compare:nTF { ##1 = 0 }
{
\seq_put_right:Nn \l__eagleone_sparsematrix_row_out_seq { \sparsezero }
}
{
\seq_put_right:Nn \l__eagleone_sparsematrix_row_out_seq { \sparseone }
}
}
\seq_use:Nn \l__eagleone_sparsematrix_row_out_seq { & } \\
}
\ExplSyntaxOff
\begin{document}
\[
\begin{pmatrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{pmatrix}
=
\begin{sparsematrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{sparsematrix}
\]
\[
\begin{sparsematrix}
0 & 1 & 0 \\
1 & 0 & 1 \\
0 & 1 & 0
\end{sparsematrix}^2
=
\begin{sparsematrix}
1 & 0 & 1 \\
0 & 2 & 0 \\
1 & 0 & 1
\end{sparsematrix}
\]
\end{document}