다차원 배열 문제에 대한 Foreach

다차원 배열 문제에 대한 Foreach

저는 경험이 없어서 LaTeX/TikZ멍청한 짓을 하고 있을지도 모릅니다.

이 코드에는 두 가지 버전이 있습니다. 하나는 작동하고 하나는 작동하지 않습니다. 두 번째는 왜 작동하지 않는지 알아내려고 궁리 중입니다.

먼저 작동하는 버전은 다음과 같습니다.

\documentclass[parskip]{scrartcl}
\usepackage[left=1cm, right = 3cm, top = 1cm, bottom = 1cm]{geometry}
\usepackage{tikz}
\usepackage{pifont} 
\usepackage{xifthen}
\usepackage[utf8]{inputenc}
\usepackage{forloop}
\usepackage[nomessages]{fp}


\usetikzlibrary{shapes}
\usetikzlibrary{calc}


\newcommand\sz{1.5cm}

%usage: size guid
\newcommand\hexboard[2]{
\begin{tikzpicture} [hexa/.style= {shape=regular polygon,regular polygon sides=6,minimum size=\sz, draw,inner sep=0,anchor=center,fill=lightgray!85!blue,rotate=0}, remember picture]
    \newcommand\sep{x}
    \pgfmathsetmacro\minCoord{int(-1*#1)} 
    \foreach \j in {\minCoord,...,#1}{% 
        \foreach \i in {\minCoord,...,#1}{%
            \ifthenelse{\cnttest{ \minCoord }{<}{ \i*\j }}{\node[hexa] (#2_h\i\sep\j) at ({(\i*.75*\sz)},{\j*\sz*sqrt(3)/2 - \i*\sz*sqrt(3)/4}) {\i\sep\j};}{}  
        }
    } 
\end{tikzpicture}
}

%usage: {{x1,y1}...} color opacity guid
\newcommand\targetedHexes[4]{
\begin{tikzpicture} [hexa/.style= {fill opacity = #3, shape=regular polygon,regular polygon sides=6,minimum size=\sz, draw,inner sep=0,anchor=center,fill=lightgray!85!blue,rotate=0}, remember picture, overlay]
    \foreach [count=\x] \pt in {#1} {
            \node [hexa, fill = #2] (target\x) at (#4_h\pt) {};
        }
\end{tikzpicture}
}
%usage: guid
\newcommand\drawSelf[1]{
\begin{tikzpicture} [hexa/.style= {fill opacity = .5, shape=regular polygon,regular polygon sides=6,minimum size=\sz, draw,inner sep=0,anchor=center,fill=lightgray!85!blue,rotate=0}, remember picture, overlay]
    \node [hexa, color = blue] (self) at (#1_h0x0){};
\end{tikzpicture}
}

\newcommand\areaEffect[2]{
    \hexboard{#1}{hello}
        \targetedHexes{#2}{red}{0.5}{hello}
    \drawSelf{hello}
}

\begin{document}
\areaEffect{2}{{0x1},{0x2},{1x0}}
\end{document}

기본적으로 이는 16진수 그리드를 그린 다음 특정 16진수(좌표 목록으로 전달됨)를 다른 색상으로 덮어쓰는 것입니다.

color여러 그룹을 지정할 수 있고 각 그룹은 서로 다른 / 를 갖기를 원합니다 opacity. 그러나 마지막 10줄을 바꾸기 위해 위의 내용을 수정하면 작동하지 않습니다.

\newcommand\areaEffect[2]{
    \hexboard{#1}{hello}
    \foreach [count=\s] \section in {#2}{
            \targetedHexes{\section}{red}{0.15*\s}{hello}
    }
    \drawSelf{hello}
}

\begin{document}
\areaEffect{2}{{{0x1},{0x2}},{{1x0}}}
\end{document}

특히 이전 버전에서는 in \targetedHexes, in (target\x)at 은 (#4_h\pt)또는 로 \pt평가됩니다 . 새 버전에서는 또는 로 평가됩니다 .0x10x2{0x1}{0x2}

내 질문은 다음과 같습니다.

여기서 무슨 일이 일어나고 있는 걸까요? 이것들은 왜 다른가요? 그것들은 어느 정도 동등해야 하지 않나요?

두 번째 버전을 어떻게 수정하나요?

시간 내 주셔서 감사합니다!

답변1

첫 번째 버전에서는 일반적인 매개변수 대체를 통해 첫 번째 인수로 \targetedHexes호출됩니다 .{0x1},{0x2},{1x0}

두 번째 버전에서는 첫 번째 인수로 \targetedHexes호출됩니다 . \section이 시점에서는 확장이 발생하지 않습니다. 이제 \section시간에 따라 확장되는지 여부는 후속 코드에 따라 달라집니다 . 분명히 그렇지 않습니다.

\section전화하기 전에 확장하는 것이 targetedHexes트릭을 수행하는 것 같습니다.

\newcommand\areaEffect[2]{
    \hexboard{#1}{hello}
    \foreach [count=\s] \section in {#2}{
            \expandafter\targetedHexes\expandafter{\section}{red}{0.15*\s}{hello}
    }
    \drawSelf{hello}
}

관련 정보