这是 TikZ 代码。如何改进它?
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale = 0.7]
\def \n {7}
\foreach \x in {0,...,\n}
\foreach \y in {-\n,...,0}
{
\draw (\x,\y) +(-.5,-.5) rectangle +(.5 ,.5) ;
}
%% ligne 0
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {$a$,$b$,$c$,$d$,$e$,$f$}
{\ifnum \ce = \cx
\node at (\x, 0) {\e} ;
\fi
};
%% ligne 1
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {8,9,1,2,0,4,5,6,7}
{\ifnum \ce = \cx
\node at (\x, -1) {\e} ;
\fi
};
%% ligne 2
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,2,0,4,$\alpha$,6,7}
{\ifnum \ce = \cx
\node at (\x, -2) {\e} ;
\fi
};
%% ligne 3
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,1,1,1}
{\ifnum \ce = \cx
\node at (\x, -3) {\e} ;
\fi
};
%% ligne 4
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {7,8,9,1,2,0,4,5}
{\ifnum \ce = \cx
\node at (\x, -4) {\e} ;
\fi
};
%% ligne 5
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,2,7,8,9,0,4,5}
{\ifnum \ce = \cx
\node at (\x, -5) {\e} ;
\fi
};
%% ligne 6
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,2,0,4,5,$\beta$,7,8}
{\ifnum \ce = \cx
\node at (\x, -6) {\e} ;
\fi
};
%% ligne 7
\foreach \x [count=\cx] in {0,...,\n}
\foreach \e [count=\ce] in {1,$x$,0,4,$y$,6,$z$,8}
{\ifnum \ce = \cx
\node at (\x, -7) {\e} ;
\fi
};
\end{tikzpicture}
\end{document}
答案1
正如我在评论中所建议的那样,使用该matrix
库的代码可以重现您的图片,非常简洁明了:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes, nodes in empty cells,
nodes = {draw, text width=1em, text height=0.75em, text depth=0.25ex,
align=center},
column sep=-\pgflinewidth, row sep=-\pgflinewidth
]
{
a & b & c & d & e & f & & \\
8 & 9 & 1 & 2 & 0 & 4 & 5 & 6 \\
1 & 2 & 0 & 4 &\alpha
& 6 & 7 & \\
1 & 1 & 1 & 1 & & & & \\
7 & 8 & 9 & 1 & 2 & 0 & 4 & 5 \\
1 & 2 & 7 & 8 & 9 & 0 & 4 & 5 \\
1 & 2 & 0 & 4 & 5 & \beta
& 7 & 8 \\
1 & x & 0 & 4 & y & 6 & z & 8 \\
};
\end{tikzpicture}
\end{document}
答案2
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (mamatrice) [matrix of math nodes,
nodes in empty cells,
nodes = {draw, text width=1em, text height=0.75em, text depth=0.25ex, align=center},
column sep=-\pgflinewidth,
row sep=-\pgflinewidth
]
{
2 & 4 & 2 & 3 & 2 & 5 & 2 & 4 \\
3 & 2 & 5 & 2 & 5 & 2 & 4 & 2 \\
2 & 4 & 2 & 7 & 2 & 4 & 2 & 2 \\
4 & 2 & 3 & 2 & 6 & 2 & 5 & 2 \\
2 & 3 & 2 & 5 & 2 & 4 & 2 & 6 \\
5 & 2 & 5 & 2 & 3 & 2 & 2 & 2 \\
2 & 4 & 2 & 3 & 2 & 4 & 2 & 2 \\
2 & 2 & 2 & 2 & 4 & 2 & 4 & 2 \\
};
\foreach \i [count = \ci] in {0,...,7} %% indexation python
\foreach \j [count = \cj] in {3,5,2,1,5} %% indexation python
\pgfmathsetmacro \myj {int(\j+1)}
\pgfmathsetmacro \myi {int(\i+1)}
{\ifnum \ci = \cj
\draw[red, thick] (mamatrice-\myi-\myj) circle (2mm);
\fi
};
\end{tikzpicture}
\end{document}