3

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 $},
    ]

    \addplot[line width=3pt] coordinates {(0.0,0.0)(100,1)(400,2)(10000,0)};

    \end{axis}
    \end{tikzpicture}

\end{document}

Output from this code is: X-axis scale label should be in x axis label.

  • 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
3

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 $},
    ]

    \addplot[line width=3pt] coordinates {(0.0,0.0)(100,1)(400,2)(10000,0)};

    \end{axis}
    \end{tikzpicture}

\end{document}
2

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={},
                    xlabel/.add = {}{
                        (\pgfplotsset{xtick scale label code orig=\xTickScale}\,m)
                    },
                },
            }
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % activate/load the created style
            tick scale labels in axis labels,
            xlabel=$x$,
            ylabel=$y$,
        ]
            \addplot coordinates {
                (0.0,0.0)(100,1)(400,2)(10000,0)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • 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
  • 3
    It is true that the name could be a bit misleading, but I think it is much better than simply writing "bla". Hopefully from the following code it was clear that it is working for the x axis only. I also hope that Christian considers this to be worth to be implemented in one of the future versions. – Stefan Pinnow Feb 4 '17 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.