使用 TikZ 中的「foreach」選項製作表格

使用 TikZ 中的「foreach」選項製作表格

為了表明滾動兩個骰子並獲得總和為 7 的機率是 1/6,我想使用TikZ.以下代碼為我提供了所有可能的擲骰子的表格。

相鄰對的矩形重疊。這會導致外觀不雅觀。如何新增水平空間來分隔矩形?

使用這段程式碼,我可以用圓形或橢圓形替換包含總共 7 的六對數字的矩形嗎?

如何讓第一個數字為藍色,第二個數字為綠色?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,shapes,positioning,intersections,quotes,decorations.markings}


\begin{document}


\begin{tikzpicture}
\node foreach \x in {1,...,6} foreach \y in{1,...,6} [draw] at (\x-6,6-\y)
{(\x, \, \y)};
\end{tikzpicture}


\end{document}

答案1

% arara: pdflatex

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}    
    \begin{tikzpicture}[x=1.5cm,y=1.5cm]
    \node foreach \x in {1,...,6} foreach \y in{1,...,6} [draw, circle] at (\x-6,6-\y)
    {(\textcolor{blue}{\x}, \, \textcolor{green}{\y})};
    \end{tikzpicture}   
\end{document}

在此輸入影像描述


% arara: pdflatex

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}    
\begin{tikzpicture}[x=1.8cm,y=1cm]
    \foreach \x in {1,...,6} {%
        \foreach \y in {1,...,6} {%
            \draw (\x-6,6-\y) ellipse (.8cm and .3cm) node {$(\textcolor{blue}{\x},\textcolor{green}{\y})$};            
        }
    }
\end{tikzpicture}
\end{document}

在此輸入影像描述


編輯:

抱歉,我一開始就誤會你了。給你:

% arara: pdflatex

\documentclass{amsart}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{xifthen}

\begin{document}    
\begin{tikzpicture}[x=1.8cm,y=1cm]
    \foreach \x in {1,...,6} {%
        \foreach \y in {1,...,6} {%
        \pgfmathtruncatemacro{\test}{\x+\y};
            \ifthenelse{\test=7}
            {\draw (\x-6,6-\y) ellipse (.8cm and .3cm) node {$(\textcolor{blue}{\x},\textcolor{green}{\y})$};}
            {\draw (\x-6,6-\y) node [draw] {$(\textcolor{blue}{\x},\textcolor{green}{\y})$};}                        
        }
    }
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案2

您也可以使用矩陣和空單元指令

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,shapes.geometric}
% shorthand for less typing
\def\mcr{\pgfmatrixcurrentrow}\def\mcc{\pgfmatrixcurrentcolumn}
\begin{document}
\begin{tikzpicture}
\matrix (a) [execute at empty cell={\ifnum7=\numexpr\mcc+\mcr\relax
  \node[ellipse,draw,inner sep=1pt]
              {(\textcolor{blue}{\the\mcc},\textcolor{green}{\the\mcr})};
\else
  \node[draw,inner sep=1pt]{(\textcolor{blue}{\the\mcc},\textcolor{green}{\the\mcr})};
\fi}]{&&&&&\\&&&&&\\&&&&&\\&&&&&\\&&&&&\\&&&&&\\};
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

嘗試使用 MetaPost,盡可能保持清醒。使用 LuaLaTeX 進行處理。

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
  \mplibtextextlabel{enable}
\begin{document}
  \begin{mplibcode}
    u := 1.25cm; v := -cm; pair pos; path box; picture dices;
    beginfig(1);
      for i = 1 upto 6:
        for j = 1 upto 6:
          pos := ((i+1)*u, (j+1)*v);
          dices :=  thelabel(decimal i & ", " & decimal j, pos);
          box := bbox dices;
          if i+j <> 7: draw box;
          else:
            d1 := xpart(lrcorner box - llcorner box) + labeloffset;
            d2 := ypart(urcorner box - lrcorner box) + labeloffset;
            draw fullcircle xscaled d1 yscaled d2 shifted  pos;
          fi
          draw dices; 
        endfor
      endfor
    endfig;
  \end{mplibcode}
\end{document}

在此輸入影像描述

相關內容