寻找大量数字代码的优雅与便捷

寻找大量数字代码的优雅与便捷

我正在设计tikz在环境中排列的数字图像tabular。每个数字都根据特定样式进行格式化(不同的颜色和形状)。我首先定义每个数字的样式,然后将它们排列在一个tabular环境中。

问题:有没有办法让我的代码更优雅和/或更系统化?

我本来希望能够以 的样式定义数字命令\81,但不知道该怎么做(除了个位数),所以我决定使用\eightyone。然而,现在我想写一个循环遍历数字列表,但“无数字”符号使其变得繁琐。我希望能够处理大量数字。有什么建议吗?

最小示例:编辑:在我的真实世界中,每个数字都有不同的风格,为了简单起见,我将它们全部圈起来并设为绿色。

\documentclass[margin=0pt]{article}%
\usepackage[svgnames,x11names]{xcolor}%
\usepackage{tikz}%
\usepackage{rotating}% provides sideways environment
\begin{document}%
\newcommand{\mycolor}{Brown4!30!DarkGreen}%
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{%
            \node[shape = circle, draw, inner sep = 1.8pt, fill = green!30!black!10] (char) {#1};}}%
\newcommand{\eightyone}{\color{\mycolor}{\circled{81}}}%
\newcommand{\eightytwo}{\color{\mycolor}{\circled{82}}}%
\newcommand{\eightythree}{\color{\mycolor}{\circled{83}}}%
\newcommand{\eightyfour}{\color{\mycolor}{\circled{84}}}%
\newcommand{\eightyfive}{\color{\mycolor}{\circled{85}}}%
\newcommand{\eightysix}{\color{\mycolor}{\circled{86}}}%
\newcommand{\eightyseven}{\color{\mycolor}{\circled{87}}}%
\newcommand{\eightyeight}{\color{\mycolor}{\circled{88}}}%
\newcommand{\eightynine}{\color{\mycolor}{\circled{89}}}%

\fontfamily{pzc}\selectfont%
\parindent=0pt%
    \begin{tikzpicture}%
        \node[anchor = base, inner sep = 0pt, outer sep = 0pt, scale = 6] {%
            \begin{sideways}%
            \begin{tabular}{ccc}%
               \eightyone    & \eightytwo    & \eightythree   \\%
               \eightyfour   & \eightyfive   &  \eightysix    \\%
               \eightyseven  & \eightyeight  & \eightynine    \\%
            \end{tabular}%
            \end{sideways}%
        };%
    \end{tikzpicture}%
\end{document}%

在此处输入图片描述

另外,顺便问一下,我想让数字稍微居中一些,因为它们目前倾斜得太靠右了。

编辑:如果有人对这些事情感兴趣的话,这里有一个后续内容: Tikz 节点位置和形状以及一般数量的节点

答案1

这是另一个使用 的最小解决方案\foreach。为了清楚起见,我决定将其添加为单独的答案。

\documentclass{article}%
\usepackage[svgnames,x11names]{xcolor}%
\usepackage{tikz}%
\usetikzlibrary{shapes.geometric}
\usepackage{rotating}% provides sideways environment
\tikzset{
  81/.style={shape = rectangle, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         },
  82/.style={shape = ellipse, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         },
  83/.style={shape = star, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         },
  84/.style={shape = diamond, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         }
}
\begin{document}%
  \begin{sideways}
    \begin{tikzpicture}
      \foreach \x/\y/\z in {0/0/81,6/0/82}
         \node[anchor = base, inner sep = 0pt, outer sep = 0pt, scale = 6,\z] at (\x,\y) {\z};%
      \foreach \x/\y/\z in {0/-5/83,6/-5/84}
         \node[anchor = base, inner sep = 0pt, outer sep = 0pt, scale = 6,\z] at (\x,\y) {\z};%
    \end{tikzpicture}%
  \end{sideways}
\end{document}%

在此处输入图片描述

这里你必须自己决定坐标和列数和行数。进一步的即兴发挥留作家庭作业 ;)

答案2

以下是使用 和 的一种matrix方法tikz

\PassOptionsToPackage{svgnames,x11names}{xcolor}
\documentclass{article}
\usepackage{rotating}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes.geometric}

\begin{document}

\tikzset{
table/.style={
  matrix of nodes,
  row sep=1ex,
  column sep=2ex,
  nodes={shape = circle, draw=Brown4!30!DarkGreen, inner sep = 1.8pt, 
         fill = green!30!black!10,text=Brown4!30!DarkGreen,
         font=\fontfamily{pzc}\selectfont,align=center},
  },
  81/.style={shape = rectangle, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         },
  82/.style={shape = ellipse, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         },
  83/.style={shape = star, rounded corners=.5em, draw=Brown4!30!DarkGreen, inner sep = 1.8pt,
         fill = green!30!black!10,text=Brown4!30!DarkRed,
         font=\fontfamily{pzc}\selectfont,align=center
         }         
}
\begin{sideways}
\begin{tikzpicture}
\matrix (mat) [table]
{
|[81]|81 & |[82]|82 & |[83]|83 \\
84 & 85 & 86 \\
86 & 87 & 88 \\
};
\end{tikzpicture}
\end{sideways}

\end{document}

在此处输入图片描述

相关内容