0

I wanted to make a macro \simplePlotWithFuncs creating plots both from .csv files and from given functions. I need it to be quite versatile, so I can set a lot of things. Because of limited number of parameters in the LaTeX macros I'm forced to use some of them for multiple arguments and parse those afterwards.

The problem is, this approach worked OK until I needed to give parameters to the addplot macro in axis environment. There I'm getting different errors and I don't know, how to solve this.

I have seen similar questions:

\foreach not behaving in axis environment

Using foreach loop variable as node label in pgfplots

Adding to a \foreach variable in pgfplots

Problems using \addlegendentry in a loop with pgfplots

But, unfortunately, I'm still not able to solve the problem.


My attempts

  1. I've tried to use \evaluate parameter in pgfplots \foreach cycle:

    \foreach [evaluate=\datacol(1) as \datacolI] \datafile in {#1} {
    \addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile};
    \addtocounter{counter}{-1}
    }
    
  2. I've also tried to use the construction \edef\variable{\noexpand}, but it doesn't work, too:

    \foreach \datafile in {#1} {
     \edef\temp{\noexpand\addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile}};
     \temp
     \addtocounter{counter}{-1}
    }
    

Error

! Incomplete \iffalse; all text was ignored after line 70.
<inserted text> 
                \fi 
<*> main.tex

? 
! Emergency stop.
<inserted text> 
                \fi 
<*> main.tex

!  ==> Fatal error occurred, no output PDF file produced!

Code with my attempt

\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{arrayjobx}

\pgfplotsset{compat=newest}

\newcounter{counter}

\newcommand{\simplePlotWithFuncs}[9]{
\newarray\labels
\readarray{labels}{#2}
\newarray\datacol
\readarray{datacol}{#3}
\edef\tmp{\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 [evaluate=\datacol(1) as \datacolI] \datafile in {#1} {
    \addplot+[only marks, mark=square*] table [x=\datacolI, y=time, 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.066181 + 0.548915/x}
\end{document}

CSV file

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
0

I managed to make it work using the \check command from arrarjobx package:

  • retrieve datacol(1) with the \checkdatacol command:
\newarray\datacol
\readarray{datacol}{#3}
\checkdatacol(1)
\edef\datacolI{\cachedata}
  • then use \datacolI in your loop:
\foreach \datafile in {#1} {
    \addplot+[only marks, mark=square*] table [x=\datacolI, y=time, col sep=comma] {\datafile};
    \addtocounter{counter}{-1}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.