TikZ の "foreach" オプションを使用してテーブルを作成する

TikZ の "foreach" オプションを使用してテーブルを作成する

2 つのサイコロを振って合計が 7 になる確率が 1/6 であることを示すために、 の "foreach" オプションを使用して表を作成したいと考えましたTikZ。次のコードは、すべての可能なロールの表を提供します。

隣接するペアの長方形が重なっています。見た目が悪くなります。長方形を区切るために水平方向のスペースを追加するにはどうすればよいでしょうか。

このコードを使用して、合計が 7 になる 6 組の数字を含む長方形を円または楕円に置き換えることはできますか?

最初の数字を青、2 番目の数字を緑にするにはどうすればよいでしょうか?

\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}

ここに画像の説明を入力してください

関連情報