`\foreach` 배열을 기반으로 길이 계산을 수행하는 방법

`\foreach` 배열을 기반으로 길이 계산을 수행하는 방법

\drawHoshisx 및 y 좌표에 대한 변수를 생성하고 괄호 안에 빼기를 사용한 계산(예: x * (y - 1)) 을 실행하여 매크로 의 가독성을 향상시키려고 합니다 . 왜 그런지는 모르겠지만 TeX을 사용할 수 없는 것 같습니다. 나는 그것이 TikZ/PGF의 배열과 관련된 것이라고 생각합니다 \foreach.

\foreach \sloc in {{3, 3}, {3, 7}, {7, 3}, {7, 7}, {5, 5}}{
  % This is what I'm tryig to improve:
  \filldraw (#3 * {\sloc}[0] - #3, #3 * {\sloc}[1] - #3)
    circle [radius=#3 / 10];
}

나는 이것을 다음과 같이 보이게 만들려고 노력하고 있습니다.

\newlength{\hoshiCoordX}
\newlength{\hoshiCoordY}
% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 3: step
% 
% Example: A 19x19 board with size 10cm x 10cm: `\drawHoshis{10}{19}{\step}'
\newcommand{\drawHoshis}[3]{
  \ifthenelse{#2 = 9} { 
    \foreach \sloc in {{3, 3}, {3, 7}, {7, 3}, {7, 7}, {5, 5}}{
      \setlength{\hoshiCoordX}{\dimexpr #3 * ({\sloc}[0] - 1) \relax}
      \setlength{\hoshiCoordY}{\dimexpr #3 * ({\sloc}[1] - 1) \relax}

      \filldraw (\hoshiCoordX, \hoshiCoordY)
        circle [radius=#3 / 10];
    }
  }{}
}

여기에 완전한 것이 있습니다일하고 있는예:

\documentclass{article}

\newlength{\step}
% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{
  \setlength{\step}{\dimexpr #1cm / (#2 - 1) \relax} % chktex 1

  \draw[step=\step] (0, 0) grid (#1, #1);
  
  \drawHoshis{#1}{#2}{\step}
}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 3: step
% 
% Example: A 19x19 board with size 10cm x 10cm: `\drawHoshis{10}{19}{\step}'
\newcommand{\drawHoshis}[3]{
  \ifthenelse{#2 = 9} { 
    \foreach \sloc in {{3, 3}, {3, 7}, {7, 3}, {7, 7}, {5, 5}}{
      % This is what I'm tryig to improve:
      \filldraw (#3 * {\sloc}[0] - #3, #3 * {\sloc}[1] - #3)
        circle [radius=#3 / 10];
    }
  }{}
}

\usepackage{tikz}

\begin{document}
  \begin{figure}[ht]
    \begin{center}
      \begin{tikzpicture}
        \goGrid{10}{9}
      \end{tikzpicture}
      \caption{Goban 1}\label{my_goban_1}
    \end{center}
  \end{figure}
\end{document}

나는 이것의 많은 변형을 시도했지만 지금까지 아무것도 작동하지 않았습니다(나는 없이도 시도했습니다 \dimexpr). 내가 놓친 부분을 발견할 수 있는 사람이 있나요?

답변1

코드에서 무엇을 생성하기를 원하는지 잘 모르겠지만 \pgfmathsetmacro대신 \setlength자동으로 계산을 수행하므로 사용하면 작업이 훨씬 더 쉬워질 것입니다. Ti의 기본 단위이므로 길이 단위를 추가할 필요가 없습니다.케이Z는 어쨌든 1cm입니다. 또한 \ifthenelse일부 패키지가 필요한 명령을 \ifnum.

괄호를 사용해야 하는 좌표 내에서 계산을 수행하려면 항목을 중괄호로 묶어야 합니다. 다음 두 줄은 모두 동일한 좌표를 나타냅니다.

(#3 * {\sloc}[0] - #3, #3 * {\sloc}[1] - #3) 
({#3 * ({\sloc}[0] - 1}, {#3 * ({\sloc}[1] - 1)}) 

어쨌든 귀하의 코드에서 배열의 항목을 선택하기 위한 인덱스가 적어도 표시된 코드 조각 중 하나에서 잘못된 것 같습니다. 이것이 올바른지 확인하십시오.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1
  \draw[step=\step] (0, 0) grid (#1, #1);
  \drawHoshis{#1}{#2}{\step}
}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 3: step
% 
% Example: A 19x19 board with size 10cm x 10cm: `\drawHoshis{10}{19}{\step}'
\newcommand{\drawHoshis}[3]{
  \ifnum#2=9\relax 
    \foreach \sloc in {{3, 3}, {3, 7}, {7, 3}, {7, 7}, {5, 5}}{
      % This is what I'm trying to improve:
      %\filldraw (#3 * {\sloc}[0] - #3, #3 * {\sloc}[1] - #3)
      \filldraw ({#3 * ({\sloc}[0] - 1}, {#3 * ({\sloc}[1] - 1)})
        circle[radius={#3 / 10}];
    }
  \fi
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{9}
  \end{tikzpicture}
\end{document}

또는:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 
% Example: A 19x19 board with size 10cm x 10cm: `\gogrid{10}{19}'
\newcommand{\goGrid}[2]{
  \pgfmathsetmacro{\step}{#1 / (#2 - 1)} % chktex 1
  \draw[step=\step] (0, 0) grid (#1, #1);
  \drawHoshis{#1}{#2}{\step}
}

% Parameters
%
% 1: dimension (in cm)
% 2: board size (square)
% 3: step
% 
% Example: A 19x19 board with size 10cm x 10cm: `\drawHoshis{10}{19}{\step}'
\newcommand{\drawHoshis}[3]{
  \ifnum#2=9\relax 
    \foreach \sloc in {{3, 3}, {3, 7}, {7, 3}, {7, 7}, {5, 5}}{
      \pgfmathsetmacro{\hoshiCoordX}{#3 * ({\sloc}[0] - 1)}
      \pgfmathsetmacro{\hoshiCoordY}{#3 * ({\sloc}[1] - 1)}
      \filldraw (\hoshiCoordX, \hoshiCoordY)
        circle[radius={#3 / 10}];
    }
  \fi
}

\begin{document}
  \begin{tikzpicture}
    \goGrid{10}{9}
  \end{tikzpicture}
\end{document}

두 코드 조각의 출력:

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

관련 정보