foreach의 "간접" 변수

foreach의 "간접" 변수

나는 나란히 위치한 직사각형인 세 개의 "그래프"를 그리려고 합니다. 각각에는 4개의 점을 배치할 수 있습니다. 이 4개의 포인트가 "점수"입니다.

foreach모든 레이아웃과 레이블이 동일하므로 루프를 사용하는 것은 그래프에 자연스러운 현상입니다.

내가 알아낼 수 없는 것은 각 반복에 데이터 목록을 전달하는 방법입니다. 다른 언어에서는 foreach이름이 플롯할 데이터를 가리키는 간접 변수를 사용하여 데이터 포인트를 배치하는 중첩된 것과 같은 것을 사용합니다 . tikz에서 변수 간접 참조를 찾을 수 없는 것 같습니다.

현재 두 번째 \foreach는 4개 항목 목록이 아닌 하나의 값만 보는 것 같습니다. 제가 잘못된 방법을 사용하고 있을 수도 있습니다. 따라서 다른 전략을 환영합니다.

MWE:

\documentclass[]{article}
\usepackage{tikz}

\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\begin{document}

% Use A4 page shape. 
% Base size of one graph is A4 page, but with pt instead of mm.

\begin{tikzpicture}

\pgfmathsetmacro{\scale}{1.75}
\pgfmathsetmacro{\graphHeight}{297 pt/\scale}   
\pgfmathsetmacro{\graphWidth}{201 pt/\scale}

\def\labelsM{85, 10, 55, 75}
\def\labelsC{75, 20, 55, 65}
\def\labelsP{65, 30, 55, 55}

\def\graphInfo{ Graph one/{(-\graphWidth pt, 0pt)}/\labelsM,
                Graph two/{(0pt, 0pt)}/\labelsC,
                Graph three/{(\graphWidth pt, 0pt)}/\labelsP
                }



\foreach \name/\pos/\values in \graphInfo
        {
    % Draw box
    \node    [
                at = {\pos},
                draw, 
                rectangle, 
                line width = 2pt,
                minimum width = \graphWidth pt,
                minimum height = \graphHeight pt,
                fill = black!15,
                name=\name
                ] 
                {} ;

    % Name graph            
    \node   [
            font = \bfseries,
            below = 2pt of \name.south
            ]
            {\name} ;   

    % Vertical lines and labels (should be 4 equidistant vertical lines) 
    \foreach \s [count=\i] in \values {
        \coordinate  (top) at  ($(\name.north west)!\i/5!(\name.north east)$) ;
        \coordinate (bottom) at ($(\name.south west)!\i/5!(\name.south east)$) ;
        \draw [dashed] (top)  --  (bottom) ;
        % Data to be plotted when this works
        };

} % end foreach \name


\end{tikzpicture}

답변1

foreach는 완전히 확장되지 않고 \values한 수준 깊이로만 확장됩니다. 따라서 값만 볼 수 \labelsM있고 값은 볼 수 없습니다. \values사용하기 전에 한 번 이상 확장하세요 .

\expandafter\let\expandafter\values\values

\expandafter다음 명령 중 하나를 확장합니다. 따라서 첫 번째는 \expandafter두 번째를 확장 \expandafter하고 두 번째는 확장한 \values다음 \labelsXTeX는 \let명령을 계속합니다. 결국 당신은 얻습니다 \let\values\labelsX.)

관련 정보