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}

基本上,它的作用是繪製一個六角形網格,然後用不同的顏色覆蓋某些六角形(作為座標列表傳入)。

我希望能夠指定多個組,每個組都有不同的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}or {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}
}

相關內容