# Automatically put PGFPlots xtick scale label in x axis label

I'm trying to put xtick scale label (defined by pgfkey /pgfplots/xtick scale label code/.code) in x axis label, between variable name and unit.

I've tried executing using the option xlabel = {$x$ (\pgfkeysvalueof{/pgfplots/xtick scale label code/.code} m)} to put in but this doesn't work.

I've also tried the following xtick scale label code/.code={\pgfkeyssetvalue{/pgfplots/xlabel}{#1}} to force PGFPlots to change my xlabel but it doesn't work as well.

I know I can do this by hand, but I'm interested in automatic approach. Is there a workaround or a solution to this?

My MWE is:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
scaled x ticks=true,
scaled y ticks = false,
y tick label style={/pgf/number format/fixed},
%xtick scale label code/.code={\pgfkeyssetvalue{/pgfplots/xlabel}{#1}},
xlabel = {$x$ (\pgfkeysvalueof{/pgfplots/xtick scale label code/.code} m)},
ylabel = {$y$},
]

\end{axis}
\end{tikzpicture}

\end{document}


Output from this code is:

• I was able to circumvent this with xtick scale label code/.code={$x$ $( 10^{#1}$ m)}, and removing xlabel key but this is more of a hack than a real solution. – Budo Zindovic Nov 5 '16 at 14:56

It seems that the best solution is to remove xlabel and use xscale label instead. My solution is posted below:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
scaled x ticks=true,
scaled y ticks = false,
y tick label style={/pgf/number format/fixed},
xtick scale label code/.code={$x$ $( 10^{#1}$ m)},
xlabel = {},
ylabel = {$y$},
]

\end{axis}
\end{tikzpicture}

\end{document}


Here I present a solution which came up mostly from Christian Feuersänger (the author of PGFPlots), but he allowed me to post it here.

(For the record: I added a Feature Request to the PGFPlots Tracker to implement a simpler interface for that.)

For more details on how the solution works, please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use this compat' level or higher to use the "advanced" placing of
% the axis labels
compat=1.3,
% create a new style to move the tick scale label' to the axis labels
tick scale labels in axis labels/.code={
\pgfkeysgetvalue{/pgfplots/xtick scale label code/.@cmd}\temp
% remember the original value of 'xtick scale label code':
\pgfkeyslet{/pgfplots/xtick scale label code orig/.@cmd}\temp
%
\pgfkeysalso{
% simply remember the value in some global macro:
xtick scale label code/.code={
\gdef\xTickScale{##1}
},
% now, _modify_ any user-specified value of 'xlabel' by
% appending the tick scale label.
% In order to evaluate this modification AFTER the user
% wrote "xlabel={$x$}", we add it to 'every axis':
every axis/.append style={
% because we don't need the "binop" in this context just
% set it to nothing
tick scale binop={},
(\pgfplotsset{xtick scale label code orig=\xTickScale}\,m)
},
},
}
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
tick scale labels in axis labels,
xlabel=$x$,
ylabel=$y$,
]

• Your solution is more than welcome but to me it seems a bit ambiguous: following the convention for other PGFPlots commands, I'd expect the option tick scale labels in axis labels to affect both axes (the one you presented affects only x-axis) and have separate xtick scale labels in axis labels and ytick scale labels in axis labels to affect x- or y-axis only. Your Feature Request for adding this option to PGFPlots package is more than welcome and I hope that the author would consider adding this to this magnificent package. – Budo Zindovic Feb 4 '17 at 22:53