如何基于 `\foreach` 数组进行长度计算

如何基于 `\foreach` 数组进行长度计算

我试图\drawHoshis通过为 x 和 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 是 1 厘米。我还将用 替换\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}

两个代码片段的输出:

在此处输入图片描述

相关内容