简化使用 Tikz 创建表的代码

简化使用 Tikz 创建表的代码

有没有办法简化以下代码?有点麻烦的是如何正确地将圆圈和叉号放置在表格中的数字上方。

\documentclass[border=1cm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,calc, automata}

\tikzset{cross/.style={cross out, draw=black, minimum size=2*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},
%default radius will be 1pt. 
cross/.default={10pt}}

\begin{document}

\begin{tikzpicture}

\matrix(m) at (-7,5) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
     {
 s,0 &  1,0 & 2,2 & 3,1 &   4,0 & 5,0    & 6,2 &  e,1   \\ 
 1,0 &  2,2 & 5,1 & 2,0  &   6,2 & 3,2    & e,0 &       \\ 
 2,0 &  4,2 & 6,5 & 6,3  &   e,0 & 4,1    &     &       \\ 
 3,0 &  5,3 &     &     &       & e,0    &     &       \\ 
    }; 

        
\draw[-] (m-1-1.south west) -- (m-1-8.south east);        

\foreach \k in {2-1,3-1,4-1, 3-2, 2-3, 2-4, 3-4, 2-5,3-5, 3-6,4-6} {       
    \draw[draw=blue] (m-\k) ellipse (4mm and 3mm) ;
     }   
        
\foreach \k in {2-2, 4-2, 3-3,2-6,2-7} {
    \draw (m-\k) node[cross=6pt] {};
} 

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

您可以简单地将样式说明放入单元格中。

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,calc, automata}

\tikzset{cross/.style={cross out, draw=black, minimum size=2*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},
%default radius will be 1pt. 
cross/.default={10pt}}

\begin{document}

\begin{tikzpicture}

\matrix(m) at (-7,5) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex},e/.style={ellipse,draw=blue,inner sep=0pt,outer sep=0pt,minimum height=6mm,minimum width=8mm},c/.style={cross=6pt}]
     {
 s,0 &  1,0 & 2,2 & 3,1 &   4,0 & 5,0    & 6,2 &  e,1   \\ 
 |[e]|1,0 &  |[c]|2,2 & |[e]|5,1 & |[e]|2,0  &   |[e]|6,2 & |[c]|3,2    & |[c]|e,0 &       \\ 
 |[e]|2,0 &  |[e]|4,2 & |[c]|6,5 & |[e]|6,3  &   |[e]|e,0 & |[e]|4,1    &     &       \\ 
 |[e]|3,0 &  |[c]|5,3 &     &     &       & |[e]|e,0    &     &       \\ 
    }; 

        
\draw[-] (m-1-1.south west) -- (m-1-8.south east);        
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容