这是我希望得到的结果和我实际得到的结果的图片。
这是代码:
\documentclass[11pt]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage[dvips]{graphicx}
\usepackage{xcolor}
\usepackage[french]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{latexsym}
\title{graphique}\newcommand{\cellsize}{0.5}
\newcommand{\circlesize}{0.35}
\newcommand{\printwhitecell}[2][]{\node[draw=gray, semithick, fill=white, minimum width=\cellsize cm, minimum height=\cellsize cm] at #2 {#1};}
\newcommand{\printblackcell}[1]{\node[draw=gray, semithick, fill=black, minimum width=\cellsize cm, minimum height=\cellsize cm] at #1 {};}
\newcommand{\printcircle}[1]{\draw[gray, semithick, fill=gray] #1 circle (\circlesize cm/2);}
\newcommand{\printcell}[2]{%
\if#1w%
\printwhitecell{#2}%
\else%
\if#1b%
\printblackcell{#2}%
\else%
\if#1c%
\printwhitecell{#2}%
\printcircle{#2}%
\else%
\if#1C%
\printblackcell{#2}%
\printcircle{#2}%
\else
\printwhitecell[#1]{#2}%
\fi%
\fi%
\fi%
\fi%
}
\newcommand{\printmatrix}[2]{
\begin{tikzpicture}
\foreach \line[count=\i] in #2 {
\foreach \cell[count=\j] in \line {
\printcell{\cell}{(\j*\cellsize,-\i*\cellsize)}
}
\xdef\width{\j}
}
\node at ({0.5*(\width+1)*\cellsize}, {(-\i -1)*\cellsize}) {#1};
\end{tikzpicture}
}
\begin{document}
\begin{center}
\printmatrix{}{{
{b,w,w,w},
{w,b,w,w},
{w,w,b,w},
{w,w,w,b}% %
}} {\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,b,w,w},
{w,w,b,w},
{w,w,w,b},
{w,w,w,w}% %
}}{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,b,w},
{w,w,w,b},
{w,w,w,w},
{w,w,w,w}% %
}}{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,w,b},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,w,w},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
\end{center}
\end{document}
答案1
我们可以利用单元格尺寸已知的事实。箭头比中心线深一点,但它们可能在S_c
上方有,因此忽略矩阵中线的垂直尺寸的影响得到了补偿。最后一个矩阵在新行中,因为\textwidth
太小了。
\documentclass[11pt]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage[dvips]{graphicx}
\usepackage{xcolor}
\usepackage[french]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{latexsym}
\title{graphique}\newcommand{\cellsize}{0.5}
\newcommand{\circlesize}{0.35}
\newcommand{\printwhitecell}[2][]{\node[draw=gray, semithick, fill=white, minimum width=\cellsize cm, minimum height=\cellsize cm] at #2 {#1};}
\newcommand{\printblackcell}[1]{\node[draw=gray, semithick, fill=black, minimum width=\cellsize cm, minimum height=\cellsize cm] at #1 {};}
\newcommand{\printcircle}[1]{\draw[gray, semithick, fill=gray] #1 circle (\circlesize cm/2);}
\newcommand{\printcell}[2]{%
\if#1w%
\printwhitecell{#2}%
\else%
\if#1b%
\printblackcell{#2}%
\else%
\if#1c%
\printwhitecell{#2}%
\printcircle{#2}%
\else%
\if#1C%
\printblackcell{#2}%
\printcircle{#2}%
\else
\printwhitecell[#1]{#2}%
\fi%
\fi%
\fi%
\fi%
}
\newcommand{\printmatrix}[2]{
\begin{tikzpicture}[baseline=-1.5cm] % here -PS
\foreach \line[count=\i] in #2 {
\foreach \cell[count=\j] in \line {
\printcell{\cell}{(\j*\cellsize,-\i*\cellsize)}
}
\xdef\width{\j}
}
\node at ({0.5*(\width+1)*\cellsize}, {(-\i -1)*\cellsize}) {#1};
\end{tikzpicture}
}
\begin{document}
\begin{center}
\printmatrix{}{{
{b,w,w,w},
{w,b,w,w},
{w,w,b,w},
{w,w,w,b}% %
}} {\LARGE$\xrightarrow{S_c}$}%
\printmatrix{}{{
{w,b,w,w},
{w,w,b,w},
{w,w,w,b},
{w,w,w,w}% %
}}{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,b,w},
{w,w,w,b},
{w,w,w,w},
{w,w,w,w}% %
}}{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,w,b},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,w,w},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
\end{center}
\end{document}
答案2
我所做的就是修改你的\printmatrix
宏以将其包装tikzpicture
如下:
\raisebox{-2.5\dimexpr\cellsize cm}{...}
这是 MWE。
\documentclass[11pt]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage[dvips]{graphicx}
\usepackage{xcolor}
\usepackage[french]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{latexsym}
\title{graphique}\newcommand{\cellsize}{0.5}
\newcommand{\circlesize}{0.35}
\newcommand{\printwhitecell}[2][]{\node[draw=gray, semithick, fill=white, minimum width=\cellsize cm, minimum height=\cellsize cm] at #2 {#1};}
\newcommand{\printblackcell}[1]{\node[draw=gray, semithick, fill=black, minimum width=\cellsize cm, minimum height=\cellsize cm] at #1 {};}
\newcommand{\printcircle}[1]{\draw[gray, semithick, fill=gray] #1 circle (\circlesize cm/2);}
\newcommand{\printcell}[2]{%
\if#1w%
\printwhitecell{#2}%
\else%
\if#1b%
\printblackcell{#2}%
\else%
\if#1c%
\printwhitecell{#2}%
\printcircle{#2}%
\else%
\if#1C%
\printblackcell{#2}%
\printcircle{#2}%
\else
\printwhitecell[#1]{#2}%
\fi%
\fi%
\fi%
\fi%
}
\newcommand{\printmatrix}[2]{
\raisebox{-2.5\dimexpr\cellsize cm}{\begin{tikzpicture}
\foreach \line[count=\i] in #2 {
\foreach \cell[count=\j] in \line {
\printcell{\cell}{(\j*\cellsize,-\i*\cellsize)}
}
\xdef\width{\j}
}
\node at ({0.5*(\width+1)*\cellsize}, {(-\i -1)*\cellsize}) {#1};
\end{tikzpicture}}
}
\begin{document}
\begin{center}
\printmatrix{}{{
{b,w,w,w},
{w,b,w,w},
{w,w,b,w},
{w,w,w,b}% %
}} {\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,b,w,w},
{w,w,b,w},
{w,w,w,b},
{w,w,w,w}% %
}}{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,b,w},
{w,w,w,b},
{w,w,w,w},
{w,w,w,w}% %
}}{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,w,b},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
{\LARGE$\xrightarrow{}$}%
\printmatrix{}{{
{w,w,w,w},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
\end{center}
\end{document}
为了将所有内容放在一行中,可以更改\cellsize
为0.4
。
如果右箭头上需要标签,则可以使用当前空白的参数xrightarrow{}
。
答案3
我认为有一种更简单的方法来创建矩阵,这是一个可能的解决方案。你甚至可以通过设置计数器使其进一步自动化,但我认为你可能想更改命令。
输出
代码
\documentclass[11pt]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage[dvips]{graphicx}
\usepackage{xcolor}
\usepackage[french]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage[noend]{algpseudocode}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{geometry}
\usepackage{latexsym}
\title{graphique}
\tikzset{
pics/square/.default={1},
pics/square/.style={%
code={%
\draw[pic actions] (0,0) rectangle (#1,#1);%
}}}
\newcommand\printmatrix[1]{%
\tikz[x=.4cm, y=.4cm, baseline=(a)]{%
\coordinate (a) at (3,3);
\clip (0,0) rectangle (5,5);
\foreach \x [count=\xx starting from #1, evaluate=\xx as \yy using int((4+#1)-\xx)] in {1,...,4}{%
\foreach \y in {1,...,4}{%
\pic at (\x,\y) {square};%
}%
\pic[fill] at (\xx,\yy) {square};%
}%
}%
}%
\newcommand\myrightarrow[1][]{\ensuremath{\raisebox{-2pt}{$\xrightarrow{#1}$}}}%
\begin{document}
\printmatrix{1} \myrightarrow \printmatrix{2} \myrightarrow \printmatrix{3} \myrightarrow \printmatrix{4} \myrightarrow \printmatrix{5}
\end{document}
答案4
正方形的构造可能会简化,但那是另一回事。您可以使用它baseline
来设置相对于基线的位置。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
% the following two are just for the example
\usepackage[margin=1cm]{geometry}
\usepackage{lipsum}
%%%%%
\newcommand{\cellsize}{0.5}
\newcommand{\circlesize}{0.35}
\newcommand{\largeto}{\mbox{\LARGE${}\to{}$}}
\newcommand{\printwhitecell}[2][]{%
\node[draw=gray, semithick, fill=white, minimum width=\cellsize cm,
minimum height=\cellsize cm] at #2 {#1};%
}
\newcommand{\printblackcell}[1]{%
\node[draw=gray, semithick, fill=black, minimum width=\cellsize cm,
minimum height=\cellsize cm] at #1 {};%
}
\newcommand{\printcircle}[1]{%
\draw[gray, semithick, fill=gray] #1 circle (\circlesize cm/2);%
}
\newcommand{\printcell}[2]{%
\if#1w%
\printwhitecell{#2}%
\else
\if#1b%
\printblackcell{#2}%
\else
\if#1c%
\printwhitecell{#2}%
\printcircle{#2}%
\else
\if#1C%
\printblackcell{#2}%
\printcircle{#2}%
\else
\printwhitecell[#1]{#2}%
\fi
\fi
\fi
\fi
}
\newcommand{\printmatrix}[2]{%
\sbox0{$ $}% ensure the math font has been defined
\begin{tikzpicture}[baseline=\dimexpr\fontdimen22\textfont2-3\dimexpr\cellsize cm\relax]
\foreach \line[count=\i] in #2 {
\foreach \cell[count=\j] in \line {
\printcell{\cell}{(\j*\cellsize,-\i*\cellsize)}
}
\xdef\width{\j}
}
\node at ({0.5*(\width+1)*\cellsize}, {(-\i -1)*\cellsize}) {#1};
\end{tikzpicture}%
}
\begin{document}
\lipsum[2]
\begin{center}
\printmatrix{}{{
{b,w,w,w},
{w,b,w,w},
{w,w,b,w},
{w,w,w,b}% %
}}\largeto
\printmatrix{}{{
{w,b,w,w},
{w,w,b,w},
{w,w,w,b},
{w,w,w,w}% %
}}\largeto
\printmatrix{}{{
{w,w,b,w},
{w,w,w,b},
{w,w,w,w},
{w,w,w,w}% %
}}\largeto
\printmatrix{}{{
{w,w,w,b},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}\largeto
\printmatrix{}{{
{w,w,w,w},
{w,w,w,w},
{w,w,w,w},
{w,w,w,w}% %
}}
\end{center}
\lipsum[3]
\end{document}