[Tex/LaTex] Line break in date ticks label

pgfplotsticks

I want to plot some data depending on dates, but as the date range is quite large (4-5 years) I would like to have ticks in the form dd.mm\\yyyy, is it possible ? It would be even better not to repeat the year for each tick, by displaying only year changes, but it is not a necessity 😉

Here is a working example :

\begin{tikzpicture}
    \begin{axis}[
    axis on top,
    enlarge x limits = false,
    date coordinates in=x,
    xticklabel=\day.\month.\year,
    date ZERO = 2013-08-13
]
\addplot[blue] coordinates {
    (2013-08-13, 5)
    (2013-08-18, 10)
    (2013-08-21, 4)
    (2013-08-26, 7)
    (2013-09-01, 1)
    (2013-09-02, 0)
    (2013-09-06, 4)
    (2013-09-11, 5.5)
    (2013-09-15, 3)
    (2013-11-13, 5)
    (2013-11-18, 10)
    (2013-11-21, 4)
    (2013-12-26, 7)
    (2014-01-01, 1)
    (2014-01-02, 0)
    (2014-01-06, 4)
    (2014-02-11, 5.5)
    (2014-02-15, 3)
};
\end{axis}
\end{tikzpicture}

I tried replacing xticklabel=\day.\month.\year by xticklabel={\day.\month\\ \year} without success.

Best Answer

Similar to axis labels and TikZ nodes in general, one way to introduce line breaks is to explicitly specify the text alignment by setting

xticklabel style={align=center}

In order to only print the year when it changes, you can store the previous label's year value in a macro and compare whether the current year label is equal to that:

xticklabel={
    \day.\month.\\\ifx\year\prevyear\else\year\fi%
    \xdef\prevyear{\year}%
}

Complete code

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}

\begin{tikzpicture}
\edef\prevyear{}
    \begin{axis}[
    axis on top,
    enlarge x limits = false,
    date coordinates in=x,
    xticklabel={
        \day.\month.\\\ifx\year\prevyear\else\year\fi%
        \xdef\prevyear{\year}%
    },
    xticklabel style={align=center},
    date ZERO = 2013-08-13
]
\addplot[black] coordinates {
    (2013-08-13, 5)
    (2013-08-18, 10)
    (2013-08-21, 4)
    (2013-08-26, 7)
    (2013-09-01, 1)
    (2013-09-02, 0)
    (2013-09-06, 4)
    (2013-09-11, 5.5)
    (2013-09-15, 3)
    (2013-11-13, 5)
    (2013-11-18, 10)
    (2013-11-21, 4)
    (2013-12-26, 7)
    (2014-01-01, 1)
    (2014-01-02, 0)
    (2014-01-06, 4)
    (2014-02-11, 5.5)
    (2014-02-15, 3)
};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question