[Tex/LaTex] Pgfplots: tick labels with zerofill except for 0 tick

pgfplotsticks

The standard pgfplots notation for tick labels is without final zeros when no needed but the standard one for scientific papers is to have all ticks with the same number of digits after the dot a part for the 0 that is often print simply as 0.

I am able to set the same precision for all ticks but I would like to print the 0 simply as 0 and not for example 0.0, how can I do that?

The first image is the pgfplots default, the second one is what I am able to do and the third is what I desire to do.

enter image description here

Minimal code for the second image

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}    [ymin=0, ymax=2,
            xmin=0.2, xmax=2,
            y tick label style={
                /pgf/number format/fixed,
                /pgf/number format/fixed zerofill,
                /pgf/number format/precision=1},
            ]
    \addplot[only marks] {x};
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

This is a pretty unusual requirement (I haven't been able to find any published examples using this format), and I don't think there's a predefined format for this. It looks like you'll have to do this using a conditional:

yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi}

will print 0 if the tick value is 0, and feed the number through the number formatter in all other cases:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}    [ymin=0, ymax=2,
            xmin=0.2, xmax=2,
            y tick label style={
                /pgf/number format/fixed,
                /pgf/number format/fixed zerofill,
                /pgf/number format/precision=1
            },
            yticklabel={\ifdim\tick pt=0pt 0 \else\pgfmathprintnumber{\tick}\fi}
            ]
    \addplot[only marks] {x};
\end{axis}
\end{tikzpicture}
\end{document}