2

I'm trying to improve the readability of my \drawHoshis macro by creating variables for the x and y coordinates, besides also running a calculation with a minus inside parentheses (e.g. x * (y - 1)). I don't know why but I just can't seem to make TeX work with it. I think it's something related to the array from the 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];
}

I'm trying to make it look more like this:

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

}{} }

Here's the complete working example:

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

I've tried many variations of this but none have worked so far (I did try without \dimexpr as well). Is anyone able to spot what I'm missing?

psygo
  • 438
  • Shouldn't it be \filldraw (#3 * {\sloc}[0] - #3, #3 * {\sloc}[1] - #3)? – Jasper Habicht Feb 06 '24 at 02:05
  • Unrelated: You can replace \setlength{\step}{\dimexpr #1cm / (#2 - 1) \relax} with \pgfmathsetmacro{\step}{#1 / (#2 - 1)} and delete \newlength{\step}. And I'd probably just use \ifnum#2=9\relax ... \fi instead of the \ifthenelse command which requires an additional package. – Jasper Habicht Feb 06 '24 at 02:07
  • Yes, it was supposed to be 0 and 1. That was a typo. The indices inside the desired example were correct though. – psygo Feb 06 '24 at 11:20

1 Answers1

3

I am unsure what you want the code to produce, but things will be much easier if you use \pgfmathsetmacro instead of \setlength as this will automatically do the calculations for you. There is no need to add a length unit, because the base unit in TikZ is 1 cm anyways. I'd also replace the \ifthenelse command that requires some package by \ifnum.

If you want to do calculations inside a coordinate that require the use of parenthesis, you need to wrap things in curly braces. The following two lines are both representing the same coordinate:

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

In any case, it seems that in your code the indices for selecting the items from the arrays are wrong, at least in one of the code snippets you show. Make sure that these are correct.

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

Alternatively:

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

Output of both code snippets:

enter image description here

  • Thanks, @Jasper Habicht! Do you know if there's something similar to this but for \newcounter? – psygo Feb 06 '24 at 11:50
  • @PhilippeFanaro It depends on what you want to do. There is \pgfmathtruncatemacro which is similar to \pgfmathsetmacro but it always outputs an integer, there is also \pgfmathsetcount, but you need to initialize the counter first using \newcounter for example. See the online manual for more information about this. – Jasper Habicht Feb 06 '24 at 12:05
  • Thank you for the tips again, @JasperHabicht! If you're interested, I've just now asked a follow-up question to this one, based on the tips you gave me. – psygo Feb 06 '24 at 12:10
  • In the future, I think I might need the variable \step as a global variable, for other macros. Do you know if there's a way of doing that through \pgfmathsetmacro? – psygo Feb 06 '24 at 12:26
  • 1
    @PhilippeFanaro You cannot directly say \global\pgfmathsetmacro as far as I know, but you can always store the result of some \pgfmath... calculation globally in another variable via other means. My point was that it is much easier to use PGF for the calculations. How you store the result is another thing. – Jasper Habicht Feb 06 '24 at 13:13