[Tex/LaTex] pgfplots: xtick, xticklabels and extra x ticks

pgfplots

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        xtick = {0,1,5},
        xticklabels = {{zero},$\alpha$,$\varphi$},
        extra x ticks = {-4,-2},
        extra x tick style = {
                            red,
                            font=\bfseries
                            },
    ]
    \addplot {x^2 - x +4};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • I use xtick with custom labels (xticklabels).
  • In addition, I want some extra x ticks.
  • I expect that the extra x ticks are printed automatically but instead, the xticklabels are used.
  • I did not define extra x tick labels.

Best Answer

Ignore my comment

In pgfplots.code.tex line 6888-90

  \expandafter\ifx\csname pgfplots@extra@#1ticklabel\endcsname\pgfutil@empty 
      \pgfutil@namelet{pgfplots@extra@#1ticklabel}{pgfplots@#1ticklabel}% 
  \fi

This shows that this is intentionally designed to use xticklables as fallback of extra x tick labels.

The workaround is simple, assign extra x tick label or extra x tick labels.

The expected behavior

@Dr. Manuel Kuehner suggests that the expected behavior is to simply print the x-coordinate. This makes more sense than I thought. In that case, we should replace the three lines by the following

\expandafter\ifx\csname pgfplots@extra@#1ticklabel\endcsname\pgfutil@empty
    \expandafter\def\csname pgfplots@extra@#1ticklabel\endcsname{\axisdefaultticklabel}%
\fi

By replace, I mean to edit pgfplots.code.tex or to redefine \pgfplots@init@ticks@for#1.


By the way, in the same file, if we look at line 6881-90, we see

\expandafter\ifx\csname pgfplots@#1ticklabel\endcsname\pgfutil@empty
  \pgfplots@if{pgfplots@#1islinear}{%
      \expandafter\def\csname pgfplots@#1ticklabel\endcsname{\axisdefaultticklabel}%
  }{%
      \expandafter\def\csname pgfplots@#1ticklabel\endcsname{\axisdefaultticklabellog}%
  }% 
\fi
\expandafter\ifx\csname pgfplots@extra@#1ticklabel\endcsname\pgfutil@empty
  \pgfutil@namelet{pgfplots@extra@#1ticklabel}{pgfplots@#1ticklabel}%
\fi

translate to English

  • if the user does not assign how to print normal labels, simply print the x-coordinate
  • if the user does assign how to print normal labels, obey the user.
  • if the user does not assign how to print extra labels, treat extra labels as they are normal labels.

The logic behind is that, sometimes the user prefers 0cm, 1cm, and 5cm rather than 0, 1, and 5. So the user may assign

xticklabel={$\pgfmathprintnumber{\tick}$cm}

In this case, the user probably want -4cm and -2cm instead of -4 and -2, so PGFPlots will inherit the formatting and add cm to extra labels.


So the problem is, how to implement the following logic?

  • if the user does not assign how to print normal labels, simply print the x-coordinate
  • if the user does assign how to print normal labels, obey the user.
  • if the user does not assign how to print extra labels:
    • if the user assign xticklabel, treat extra labels as they are normal labels.
    • if the user assign xticklabels, simply print the x-coordinate.

You probably want to check if \pgfplots@xticklabel is empty. (Do you?)