多次元配列に対する Foreach の問題

多次元配列に対する Foreach の問題

私は の経験がないのでLaTeX/TikZ、何か愚かなことをしているかもしれません。

このコードのバージョンが 2 つあります。1 つは動作しますが、もう 1 つは動作しません。2 つ目のバージョンが動作しない理由を解明するのに途方に暮れています。

まず、動作するバージョン:

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

基本的に、これは六角形のグリッドを描画し、特定の六角形 (座標のリストとして渡される) を別の色で上書きします。

複数のグループを指定して、それぞれに異なる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}

具体的には、古いバージョンでは、 の\targetedHexes(target\x)または(#4_h\pt)\pt評価されます。新しいバージョンでは、 は または と評価さ0x1れます。0x2{0x1}{0x2}

私の質問は次のようになります:

ここで何が起こっているのでしょうか? これらはなぜ異なるのでしょうか? これらは多かれ少なかれ同等であるべきではないでしょうか?

2 番目のバージョンを修正するにはどうすればよいですか?

お時間をいただきありがとうございました!

答え1

最初のバージョンでは、通常のパラメータ置換によって、最初の引数として\targetedHexes呼び出されます。{0x1},{0x2},{1x0}

2 番目のバージョンでは、が最初の引数として\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}
}

関連情報