6

I think the question says it all. I'd like to have two extra x tick labels with different colors. I thought of using a cycle command, but I couldn't figure out how it should work. I changed the color of both labels to red using

every extra x tick/.style={xticklabel style={color=red}},

and the xticklabel command is needed for a correct alignment of the labels (essentially with anchor=north).

Here comes the MWE (or MNWE):

\documentclass{scrreprt}
\usepackage{tikz,pgfplots}
\begin{document}
 \begin{tikzpicture}
  \begin{axis}[
    every extra x tick/.style={grid=none, xticklabel
      style={anchor=north, color=red}},
    extra x ticks ={0.5,1.5},
    extra x tick labels={$s_l$,$s_r$},
       ]
\addplot (x,x);  
\end{axis}
\end{tikzpicture}

\end{document}
1
  • Or is this simply impossible. I'd also appreciate such an answer – Quickbeam2k1 Sep 12 '14 at 16:36
12

A possibility would be write a style which depends on \ticknum. Inside of tick label typesetting, this macro evaluates to the current tick index (starting with 0). We could (locally) define color names which contain this index - and use these indexed colors inside of the ticklabel style:

enter image description here

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
 \begin{tikzpicture}
  \begin{axis}[
    every extra x tick/.style={
        grid=none, 
        tick0/.initial=red,
        tick1/.initial=green,
        tick2/.initial=orange,
        xticklabel style={
            anchor=north, 
            color=\pgfkeysvalueof{/pgfplots/tick\ticknum},
        },
    },
    extra x ticks ={0.5,1.5,3},
    extra x tick labels={$s_l$,$s_r$,qr},
       ]
\addplot (x,x);  
\end{axis}
\end{tikzpicture}

\end{document}

The code <key>/.initial=<value> instructs \pgfkeys to define a new (temporary) key with name <key> and assigns the value <value> to it. We can simply read that value with \pgfkeysvalueof{/pgfplots/<key>} . Macros are allowed inside of <key>; they will be expanded. Of course, \declarecolor{tick0}{...} combined with color=tick\ticknum would also work.

The code would complain if there are too few colors.

0

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.