TikZ에서 "foreach" 옵션을 사용하여 테이블 만들기

TikZ에서 "foreach" 옵션을 사용하여 테이블 만들기

두 개의 주사위를 굴려 합이 7이 나올 확률이 1/6이라는 것을 보여주기 위해 .NET에서 "foreach" 옵션을 사용하여 테이블을 만들고 싶었습니다 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}

여기에 이미지 설명을 입력하세요

관련 정보