
에 대한 후속 질문입니다.범례에 여러 pgfplotstable 열을 사용하는 방법?
거기에서 최소한의 작업 예제를 시도하고 열 이름 중 하나를 다음으로 바꿨습니다.
{$R_0 = \SI{10}{\kilo\ohm}$}
이와 같이:
\documentclass[]{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepgfplotslibrary{groupplots}
\pgfplotsset{% global config
compat=newest,
every minor tick={very thin, gray},
minor tick num=4,
enlargelimits=0.02,
group style={
columns=2,
xlabels at=edge bottom,
ylabels at=edge left},
every axis legend/.append style={
legend cell align=left,
legend columns=4
}
}
\pgfplotstableread{
Time {Model A} {Model G} {$R_0 = \SI{10}{\kilo\ohm}$}
0 1 1 1
1 1 2 2
2 2 3 3
3 3 4 4
4 5 5 8
5 8 6 10
6 13 7 16
}\modeltable
\pgfplotstableread{
Time {Data 1} {Data 2}
0 1.3 2.3
1 2.6 3.6
2 3.8 4.8
3 7.9 5.9
4 6.3 7.3
5 7.5 6.5
6 9.0 8.9
}\datatable
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
xlabel={Time},
ylabel={Amplitude},
every axis title shift=0,
legend to name=grouplegend,
legend style={/tikz/every even column/.append style={column sep=0.5cm}}
]
\pgfplotsinvokeforeach{1,2}{%
\nextgroupplot[title=Data $#1$]
% Models
\foreach \y in {1, 2, 3} {
\addplot [smooth, dashed, color=DarkRed]
table[x index=0, y index=\y] {\modeltable};
\pgfplotstablegetcolumnnamebyindex{\y}\of{\modeltable}\to{\colname}
\addlegendentryexpanded{\colname}
}
% Data
\addplot [only marks, mark=o]
table[x index=0, y index=#1] {\datatable};
\addlegendentry {Data}
}
\end{groupplot}
\node (dummytitle) at ($(group c1r1.north)!0.5!(group c2r1.north)$)
[above]{};
\node (title) at (dummytitle.north)
[above, yshift=\pgfkeysvalueof{/pgfplots/every axis title shift}]
{Experimental Data};
\node (legend) at ($(group c1r1.south)!0.5!(group c2r1.south)$)
[below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
{\ref{grouplegend}};
\end{tikzpicture}
\end{document}
그러나 이것은 컴파일되지 않습니다.
로그의 첫 번째 오류는 다음과 같습니다.
)) ! 정의되지 않은 제어 순서. $R_0 = \SI {10}{\kilo \ohm }$ l.33 }\modeltable
몇 가지 애프터 이펙트가 있습니다. pgfplottable에 수학 문자열을 넣을 때만 작동하지 않습니다. 문서의 다른 곳에서는 예상대로 작동합니다.
그러면 pgfplots 파서가 라텍스 명령 및/또는 수학 모드가 포함된 열 이름을 처리할 수 없다고 생각합니까?
답변1
그러면 매크로가 생성되고 \namesub
미리 정의된 대체 항목(존재하는 경우)으로 \createsub
간단히 대체됩니다 .\colname
\documentclass[]{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepgfplotslibrary{groupplots}
\pgfplotsset{% global config
compat=newest,
every minor tick={very thin, gray},
minor tick num=4,
enlargelimits=0.02,
group style={
columns=2,
xlabels at=edge bottom,
ylabels at=edge left},
every axis legend/.append style={
legend cell align=left,
legend columns=4
}
}
\pgfplotstableread{
Time {Model A} {Model G} R0
0 1 1 1
1 1 2 2
2 2 3 3
3 3 4 4
4 5 5 8
5 8 6 10
6 13 7 16
}\modeltable
\pgfplotstableread{
Time {Data 1} {Data 2}
0 1.3 2.3
1 2.6 3.6
2 3.8 4.8
3 7.9 5.9
4 6.3 7.3
5 7.5 6.5
6 9.0 8.9
}\datatable
\makeatletter
\newcommand{\namesub}[1]% #1 = column name
{\@ifundefined{name.#1}{#1}{\csname name.#1\endcsname}}
\newcommand{\createsub}[2]% #1 = column name, #2 = substituted name
{\@ifundefined{name.#1}{\expandafter\def\csname name.#1\endcsname{#2}}%
{\errmessage{Name #1 already in use}}}
\makeatother
\createsub{R0}{$R_0 = \SI{10}{\kilo\ohm}$}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
xlabel={Time},
ylabel={Amplitude},
every axis title shift=0,
legend to name=grouplegend,
legend style={/tikz/every even column/.append style={column sep=0.5cm}}
]
\pgfplotsinvokeforeach{1,2}{%
\nextgroupplot[title=Data $#1$]
% Models
\foreach \y in {1, 2, 3} {
\addplot [smooth, dashed, color=DarkRed]
table[x index=0, y index=\y] {\modeltable};
\pgfplotstablegetcolumnnamebyindex{\y}\of{\modeltable}\to{\colname}
\addlegendentryexpanded{\namesub\colname}
}
% Data
\addplot [only marks, mark=o]
table[x index=0, y index=#1] {\datatable};
\addlegendentry {Data}
}
\end{groupplot}
\node (dummytitle) at ($(group c1r1.north)!0.5!(group c2r1.north)$)
[above]{};
\node (title) at (dummytitle.north)
[above, yshift=\pgfkeysvalueof{/pgfplots/every axis title shift}]
{Experimental Data};
\node (legend) at ($(group c1r1.south)!0.5!(group c2r1.south)$)
[below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
{\ref{grouplegend}};
\end{tikzpicture}
\end{document}