2

Some time ago, I requested your help on creating axis labeling, that is compliant with the German standard DIN 461 (my older question). Thanks Jake for the good answer, that is working well in most cases!

Now I recently came across a problem, where the axis is getting "auto scaled" by the help of an axis scale. However, in that case, the above mentioned approach did not work well - better to say, it didn't work at all.

Here, the adpoted example code of the above mentioned solution:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{siunitx}

\usetikzlibrary{calc}
\pgfplotsset{
    din xlabel/.style={
        xticklabel style={
            name=label\ticknum,
            append after command=\pgfextra{\xdef\lastticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\lastticknum-1)}
            \path (label\lastticknum.base) -- (label\pgfmathresult.base) node [midway, anchor=base] {#1};
        }
    }
}

\begin{document}
  % === That one is working ===
  \begin{tikzpicture}
    \begin{axis}[din xlabel=\si{\celsius}]
      \addplot table {
      0 1
      2 1
      3 2
      4 3
      };
    \end{axis}
  \end{tikzpicture}
  % === This one not ===
  \begin{tikzpicture}
    \begin{axis}[din xlabel=\si{\celsius}, xtick = data]
      \addplot table {
      0.001 1
      0.002 1
      0.003 2
      0.004 3
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

The result:

The result

And the corresponding error: Undefined control sequence. label\ticknum

Obviously, the ticknum variable is not defined or not accessible. Unfortunately, I'm not that deep into how the axes and the ticks in particular, are generated. It would be great, if someone could shed a light on why axes with scale are treated differently than the "normal" ones. Solutions (or approaches) are appreciated as well. ;-)

Thanks in advance!

Stefan Pinnow
  • 29,535
Chris
  • 185

1 Answers1

0

Turns out that \ticknum is not defined for scaled ticks. So, unless one is ready to redefine macros contained in pgfplotsticks.code.tex, one may use a new counter.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{siunitx}
\newcounter{mytick}
\usetikzlibrary{calc}
\pgfplotsset{
    din xlabel/.style={
        execute at begin axis=\setcounter{mytick}{0},
        xticklabel style={
            /utils/exec=\stepcounter{mytick},
            name=label\number\value{mytick},
        },
        after end axis/.code={
            \path (label\the\numexpr\value{mytick}
            +\pgfkeysvalueof{/pgfplots/din xlabel shift}.base)
             -- (label\the\numexpr\value{mytick}
             +\pgfkeysvalueof{/pgfplots/din xlabel shift}-1.base) node [midway, anchor=base] {#1};
        }
    },din xlabel shift/.initial=0
}
\begin{document}
  % === That one is working ===
  \begin{tikzpicture}
    \begin{axis}[din xlabel=\si{\celsius}]
      \addplot table {
      0 1
      2 1
      3 2
      4 3
      };
    \end{axis}
  \end{tikzpicture}
  % === This one works now as well ===
  \begin{tikzpicture}
    \begin{axis}[din xlabel=\si{\celsius}, xtick = data,din xlabel shift=-1]
      \addplot table {
      0.001 1
      0.002 1
      0.003 2
      0.004 3
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

It also turns out that the scale factor increases the tick number by one. I added a shift to account for that. This allows you to move the position of the extra "tick" wherever you like, i.e. you can also move it between the first and second tick if you like.