# How to change only the last xticklabel?

I would like to set the last xticklabel to \infty. How can this be achieved without necessarily knowing the xtick value?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xticklabel={\tick >= 24 ? $\infty$ : $\pgfmathprintnumber{\tick}$}
]
\end{axis}
\end{tikzpicture}
\end{document}


(note: the above inline conditional doesn't work at all, it's just to illustrate what I am trying to do)

-
Welcome to TeX.SE. I would be best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This would also ensure that the proposed solution actually works for your case. – Peter Grill Feb 14 '12 at 18:37

You can assign names to the label nodes and access the last one through that. The node text can't be changed after it has been created, unfortunately, so you will have to paint over it with the changed label. Here's a new style called overwrite last x tick label that takes an optional argument to specify what the last label should be overwritten with. The style first draws a white rectangle over the old label and then puts a new node on top of that. That ensures that the old label is completely covered, even if the new label takes up less space.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
overwrite last x tick label/.style={
every x tick label/.append style={alias=lasttick},
extra description/.append code={
\fill [white] (lasttick.north west) ++(0pt,-\pgflinewidth) rectangle (lasttick.south east);
\node [anchor=base] at (lasttick.base) {#1};}
},
overwrite last x tick label/.default={$\infty$}
}

\begin{tikzpicture}
\begin{axis}[overwrite last x tick label]
0 1\\
2 1\\
3 2\\
4 3\\};
\end{axis}
\end{tikzpicture}
\end{document}

-
Thanks Jake! Works like a charm. I needed to add a yshift though: \node [anchor=base,yshift=.2ex] at (lasttick.base) {#1}; for the \infty to be displayed nicely. – Fabian Feb 15 '12 at 11:58

You can use the axis description coordinate system:

## Notes:

• Needed to add clip=false to the axis.
• The fill=whites is so that the real label is hidden below the \infty.
• draw=none can be omitted.
• yshift=1.5ex was necessary to get the label in the correct vertical position. Perhaps there is a better way to do this.
• If you explicitly specify xmax in the axis options you can access this value thru \pgfkeysvalueof{/pgfplots/xmin} as per How to access \xmin, \xmax, \ymin, \ymax from within PGFplots axis environment.

## Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[clip=false]

\node[draw=none,yshift=1.5ex,fill=white] at (xticklabel cs:1) {$\infty$};
\end{axis}
\end{tikzpicture}
\end{document}

-
You can also work with: \begin{axis}[ extra x ticks=6, extra x tick labels={$\infty$}, extra x tick style={tick label style={fill=white}}, ]  – Marco Daniel Feb 14 '12 at 19:26
@MarcoDaniel: That seems useful, but how can yo adapt that to work without knowing the 6 value specified in extra x ticks=6. – Peter Grill Feb 14 '12 at 19:33
I don't know. I used this code some time ago ;-) – Marco Daniel Feb 14 '12 at 19:49
Thanks guys! Don't you think that this inline conditional xticklabel={\tick >= 24 ? $\infty$ : $\pgfmathprintnumber{\tick}$} would be the cleanest solution? Maybe it just needs a tiny change to work. Then one could maybe combine it with \nexttick. – Fabian Feb 14 '12 at 22:25
@Fabian: I don't know how to make that syntax work. Even still, that would require that you know a maximum value for the tick label. – Peter Grill Feb 14 '12 at 23:07

You can use TeX (or LaTeX) conditionals based on \tick pt as long as the numbers are within the LaTeX number range -16384...16384. Here, the suffix pt allows you to apply fixed point arithmetics (because pt are represented as fixed point numbers in TeX):

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xticklabel={\ifdim\tick pt<6pt $\pgfmathprintnumber{\tick}$ \else $\infty$\fi}
]