I want to make macro for creating plots from source files.
I have a following code:
\newcommand{\simplePlotWithFuncs}[9]{
\newarray\labels
\readarray{labels}{#2}
\newarray\datacol
\readarray{datacol}{#3}
\datacol(1)
\begin{figure}[hbtp!]
\centering
\begin{tikzpicture}[domain=0:25, scale=0.75, transform shape]
\begin{axis}[
xlabel=\labels(1),
ylabel=\labels(2),
legend pos={#4},
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
width=\textwidth*0.8,
height=\textwidth*0.5,
cycle list name = color list,
each nth point = 1,
filter discard warning=false
]
\setcounter{counter}{0}
\foreach \datafile in {#1} {
\addplot+[only marks, mark=square*] table [x=\datacol(1), y=\datacol(2), col sep=comma] {\datafile};
\addtocounter{counter}{-1}
}
\pgfplotsset{cycle list shift=\value{counter}-1}
\foreach \func in {#9} {
\addplot+[mark=none, dashed]{\func};
}
\legend{#5}
\end{axis}
\end{tikzpicture}
\caption{#6}
\label{#7}
\smallskip
{\small #8}
\end{figure}
\medskip
}
And I use it like this:
\simplePlotWithFuncs
{data/ratioThProcs-size10.csv,
data/ratioThProcs-size20.csv,
data/ratioThProcs-size90.csv,
data/ratioThProcs-size100.csv}
{Number of processes & Time [s]}
{nprocs & time}
{north east}
{size 10x10x10,
size 10x10x20,
size 10x10x90,
size 10x10x100}
{Stiffness matrix assembling speed dependence on decomposition style}
{fig:threadsProcsDecompDifference}
{Fixed size of problem. Changing style of the decomposition - ratio of MPI processes and threads. Number of threads per process is computed by the formula $\frac{24}{nprocs}$. Points are measured values, dashed line is a hyperbola got by inverse regression.}
{0.0550340156 + 0.64833795/x,
0.095864 + 1.076260/x,
0.156679 + 5.088291/x,
0.154080159 + 5.86336629/x}
The problem is, that it causes following error during compilation with pdflatex:
! Missing \endcsname inserted.
<to be read again>
\temp@@@count
l.152 0.154080159 + 5.86336629/x}
It's caused by reading from datacol array like this \datacol(1). The problem is, I have no idea why, when I'm using labels array in the same way and without any problem.
Do you know, what's the problem?
EDIT
Minimal working example:
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{subfig} % makra pro "podobrazky" a "podtabulky"
\usepackage{tikz} % makra pro kresleni
\usepackage{underscore}
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{arrayjob}
\pgfplotsset{compat=newest}
\newcounter{counter}
\newcommand{\simplePlotWithFuncs}[9]{
\newarray\labels
\readarray{labels}{#2}
\newarray\datacol
\readarray{datacol}{#3}
\begin{figure}[hbtp!]
\centering
\begin{tikzpicture}[domain=0:25, scale=0.75, transform shape]
\begin{axis}[
xlabel=\labels(1),
ylabel=\labels(2),
legend pos=#4,
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
width=\textwidth*0.8,
height=\textwidth*0.5,
cycle list name = color list,
each nth point = 1,
filter discard warning=false
]
\setcounter{counter}{0}
\foreach \datafile in {#1} {
\addplot+[only marks, mark=square*] table [x=\datacol(1), y=\datacol(2), col sep=comma] {\datafile};
\addtocounter{counter}{-1}
}
\pgfplotsset{cycle list shift=\value{counter}-1}
\foreach \func in {#9} {
\addplot+[mark=none, dashed]{\func};
}
\legend{#5}
\end{axis}
\end{tikzpicture}
\caption{#6}
\label{#7}
\smallskip
{\small #8}
\end{figure}
\medskip
}
\begin{document}
\simplePlotWithFuncs
{ratioThProcs-size10.csv}
{Number of processes & Time [s]}
{nprocs & time}
{north east}
{size 10x10x10}
{Stiffness matrix assembling speed dependence on decomposition style}
{fig:threadsProcsDecompDifference}
{Fixed size of problem. Changing style of the decomposition - ratio of MPI processes and threads. Number of threads per process is computed by the formula $\frac{24}{nprocs}$. Points are measured values, dashed line is a hyperbola got by inverse regression.}
{0.0550340156 + 0.64833795/x}
\end{document}
ratioThProcs-size10.csv
nprocs,nthreads,size,time
1,24,10,0.656395
2,12,10,0.256914
3,8,10,0.271941
4,6,10,0.174338
6,4,10,0.130503
8,3,10,0.123015
12,2,10,0.173938
24,1,10,0.114695
EDIT 2
I think the problem can be caused by the native behavior of axis environment, where, as far as I understand, variables aren't expanded until the end of the loop as described here for example.
So I guess I should use something like \edef, but I have no idea, how...
Substantially reduced MWE
\documentclass{article}
\usepackage{pgfplots}
\usepackage{arrayjob}
\begin{filecontents}{ratioThProcs-size10.csv}
nprocs,nthreads,size,time
1,24,10,0.656395
2,12,10,0.256914
3,8,10,0.271941
4,6,10,0.174338
\end{filecontents}
\newcommand{\simplePlotWithFuncs}[2]{
\newarray\datacol
\readarray{datacol}{#2}
\begin{tikzpicture}
\begin{axis}
\foreach \datafile in {#1} {
\addplot table [x=\datacol(1),y=\datacol(2),col sep=comma] {\datafile};
}
\end{axis}
\end{tikzpicture}
}
\begin{document}
\simplePlotWithFuncs{ratioThProcs-size10.csv}{nprocs & time}
\end{document}
\documentclassto\end{document}. And usearrayjobx, notarrayjobalong with LaTeX. But there are far better packages for that kind of business. – egreg Dec 17 '16 at 16:48