Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a command to (re)set the starting point of a cycle list?

What I am using now is the cycle list shift command but I end up doing some time consuming calculations and my code is oftenless robust as it use the number of elements of the cycle list which I might want to change afterwards.

Is there something like set cyle list at n?


Edit: to clarify the point raised by Jake, here is an example of the two possible mode as I undestand them.

Mode 1:

 \addplot    <- uses style 0
 \addplot    <- uses style 1
 \addplot    <- uses style 2

\pgfplotsset{cycle list set=5}
 \addplot    <- uses style 5
 \addplot    <- uses style 6
 \addplot    <- uses style 7
…

Mode 2:

 \addplot    <- uses style 0
 \addplot    <- uses style 1
 \addplot    <- uses style 2

\pgfplotsset{cycle list set just once=5}
 \addplot    <- uses style 5
 \addplot    <- uses style 4
 \addplot    <- uses style 5
…

What I had in mind when asking the question was mode 1.

Mode 2 could be interesting too, but maybe as a style:

 \addplot[set cycle list=5]    <- uses style 5

There could be also Mode 3:

 \addplot    <- uses style 0
 \addplot    <- uses style 1
 \addplot    <- uses style 2

\pgfplotsset{cycle list set=5}
 \addplot    <- uses style 5
 \addplot    <- uses style 5
 \addplot    <- uses style 6

but this is more or les equivalent to mode 2 (a shift of only one step is easily handled with cycle list shift).

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

There might be a more elegant way to do this, but one thing that works is to patch one of PGFPlots' internal functions to make a new key cycle list set available:

\begin{axis}[domain=0:10]
\addplot {x};
\addplot {2*x};
\addplot {3*x};
\pgfplotsset{cycle list set=0}
\addplot {4*x};
\addplot {5*x};
\addplot {6*x};
\end{axis}

\documentclass{article}

\usepackage{pgfplots}

\makeatletter
\def\pgfplots@getautoplotspec into#1{%
    \begingroup
    \let#1=\pgfutil@empty
    \pgfkeysgetvalue{/pgfplots/cycle multi list/@dim}\pgfplots@cycle@dim
    %
    \let\pgfplots@listindex=\pgfplots@numplots
    %%% Start new code
    \pgfkeysgetvalue{/pgfplots/cycle list set}\pgfplots@listindex@set
    \ifx\pgfplots@listindex@set\pgfutil@empty
    \else 
        \c@pgf@counta=\pgfplots@listindex
        \c@pgf@countb=\pgfplots@listindex@set
        \advance\c@pgf@countb by -\c@pgf@counta
        \globaldefs=1\relax
        \edef\setshift{%
            \noexpand\pgfkeys{
                /pgfplots/cycle list shift=\the\c@pgf@countb,
                /pgfplots/cycle list set=
            }
        }%
        \setshift%
    \fi
    %%% End new code    
    \pgfkeysgetvalue{/pgfplots/cycle list shift}\pgfplots@listindex@shift
    \ifx\pgfplots@listindex@shift\pgfutil@empty
    \else
        \c@pgf@counta=\pgfplots@listindex\relax
        \advance\c@pgf@counta by\pgfplots@listindex@shift\relax
        \ifnum\c@pgf@counta<0
            \c@pgf@counta=-\c@pgf@counta
        \fi
        \edef\pgfplots@listindex{\the\c@pgf@counta}%
    \fi
    \ifnum\pgfplots@cycle@dim>0
        % use the 'cycle multi list' feature.
        %
        % it employs a scalar -> multiindex map like
        % void fromScalar( size_t d, size_t scalar, size_t* Iout, const size_t* N )
        % {
        %   size_t ret=scalar;
        %   for( int i = d-1; i>=0; --i ) {
        %       Iout[i] = ret % N[i];
        %       ret /= N[i];
        %   }
        % }
        % to get the different indices into the cycle lists.
        %-------------------------------------------------- 
        \c@pgf@counta=\pgfplots@cycle@dim\relax
        \c@pgf@countb=\pgfplots@listindex\relax
        \advance\c@pgf@counta by-1
        \pgfplotsloop{%
            \ifnum\c@pgf@counta<0
                \pgfplotsloopcontinuefalse
            \else
                \pgfplotsloopcontinuetrue
            \fi
        }{%
            \pgfkeysgetvalue{/pgfplots/cycle multi list/@N\the\c@pgf@counta}\pgfplots@cycle@N
            % compute list index:
            \pgfplotsmathmodint{\c@pgf@countb}{\pgfplots@cycle@N}%
            \divide\c@pgf@countb by \pgfplots@cycle@N\relax
            %
            \expandafter\pgfplots@getautoplotspec@
                \csname pgfp@cyclist@/pgfplots/cycle multi list/@list\the\c@pgf@counta @\endcsname
                {\pgfplots@cycle@N}%
                {\pgfmathresult}%
            \t@pgfplots@toka=\expandafter{#1,}%
            \t@pgfplots@tokb=\expandafter{\pgfplotsretval}%
            \edef#1{\the\t@pgfplots@toka\the\t@pgfplots@tokb}%
            \advance\c@pgf@counta by-1
        }%
    \else
        % normal cycle list:
        \pgfplotslistsize\autoplotspeclist\to\c@pgf@countd
        \pgfplots@getautoplotspec@{\autoplotspeclist}{\c@pgf@countd}{\pgfplots@listindex}%
        \let#1=\pgfplotsretval
    \fi
    \pgfmath@smuggleone#1%
    \endgroup
}

\pgfplotsset{
    cycle list set/.initial=
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:10]
\addplot {x};
\addplot {2*x};
\addplot {3*x};
\pgfplotsset{cycle list set=0}
\addplot {4*x};
\addplot {5*x};
\addplot {6*x};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
The only issue with your code is that after using \pgfplotsset{cycle list set=*x*} it is stuck to x and does not get incremented. I must admit I am a bit out of touch here. I seize the occasion to thank you for your answers. It looks like it takes you minutes to figure out what would take me weeks. –  Alfred M. Sep 27 '12 at 9:04
    
@AlfredM.: Ah yes, you're right about that. You can reactivate the cycle by saying \pgfplotsset{cycle list set=}. That can, of course, be automated. There's a bit of a design question here, though: When you've set a plot to cycle list index x, should the next plot use x+1? Or should it use the cycle list index it would ordinarily have used? –  Jake Sep 27 '12 at 9:15
    
Both can make sense. What I had in mind was that it should use x+1, which is what is achieved with \pgfplotsset{cycle list set=}. I will wait a bit before accepting your anwser-- in case one comes up with a more compact solution-- but it works well. –  Alfred M. Sep 27 '12 at 9:17
    
@AlfredM.: Ah, there might be a misunderstanding. Say we're at plot number four now, but we use cycle list set=0 to set the style used for the first plot, do you want the next plot to use the style ordinarily used for plot number 2 or the one used for plot number 5? –  Jake Sep 27 '12 at 12:35
    
I added some examples to the question. I hope you can sort out what I mean with these modes. –  Alfred M. Sep 27 '12 at 12:57
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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