私は、横に並んだ長方形の 3 つの「グラフ」を描こうとしています。各グラフには、配置する 4 つのポイントがあります。これらの 4 つのポイントが「スコア」です。
foreach
すべてのレイアウトとラベルが同じなので、グラフではループを使用するのが自然です。
私が理解できないのは、データのリストを各反復に渡す方法です。他の言語では、ネストされたもののようなものを使いforeach
、プロットするデータを指す名前を持つ間接変数を使用して、データ ポイントを配置します。tikz で変数の間接参照が見つからないようです。
現時点では、2 番目の \foreach は 4 つの項目のリストではなく、1 つの値しか認識していないようです。間違った方法を使用している可能性があります。したがって、他の戦略を歓迎します。
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
完全に展開せず、1 レベルだけ深く展開します。したがって、\labelsM
値ではなく、値のみを参照します。\values
使用する前に少なくとも 1 回展開します。
\expandafter\let\expandafter\values\values
\expandafter
は、次の 2 番目のコマンドを展開します。したがって、最初のコマンド\expandafter
は 2 番目のコマンドを展開し\expandafter
、2 番目のコマンドは に展開され\values
、\labelsX
その後 TeX は\let
コマンドを続行します。最後に が得られます\let\values\labelsX
。)