2

As in thread Get the auto generated tick distance of a pgfplot (DIN 461) already asked, i am searching for a way to get the units between last two ticks. The solution from the mentioned thread works for one axis. I tried to extend the code for two axes, but this only works for on axis. What am I doing wrong?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{siunitx}

\pgfplotsset{
    din xunit/.style={
        xticklabel style={
            name=xlabel\ticknum,
            append after command=\pgfextra{\xdef\lastxticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\lastxticknum-1)}
            \path (xlabel\lastxticknum.base) -- (xlabel\pgfmathresult.base) node [midway, anchor=base] {#1};
        }
    },
    din yunit/.style={
        yticklabel style={
            name=ylabel\ticknum,
            append after command=\pgfextra{\xdef\lastyticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\lastyticknum-1)}
            \path (ylabel\lastyticknum.base) -- (ylabel\pgfmathresult.base) node [midway, anchor=base] {#1};
        }
    }
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[din xunit=\si{\second}, din yunit=\si{\meter}]
      \addplot table {
        0 0
        1 3
        2 4
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

2 Answers2

1

This is because the second after end axis/.code overwrites the first one. Thus, changing .code to .append code solves the problem.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.15,
        din xunit/.style={
            xticklabel style={
                name=xlabel\ticknum,
                append after command=\pgfextra{\xdef\lastxticknum{\ticknum}}
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                \pgfmathparse{int(\lastxticknum-1)}
                \path (xlabel\lastxticknum.base) -- (xlabel\pgfmathresult.base) node [midway, anchor=base] {#1};
            },
        },
        din yunit/.style={
            yticklabel style={
                name=ylabel\ticknum,
                append after command=\pgfextra{\xdef\lastyticknum{\ticknum}}
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                \pgfmathparse{int(\lastyticknum-1)}
                \path (ylabel\lastyticknum.base) -- (ylabel\pgfmathresult.base) node [midway, anchor=base] {#1};
            },
        },
    }

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        din xunit=\si{\second},
        din yunit=\si{\meter},
    ]
        \addplot table {
            0 0
            1 3
            2 4
        };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
1

You might want to include the code to draw the axis all together:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{siunitx}

\pgfplotsset{
    din label/.style args={#1 and #2}{
        xticklabel style={
            name=xlabel\ticknum,
            append after command=\pgfextra{\xdef\xlastticknum{\ticknum}}
        },
        yticklabel style={
            name=ylabel\ticknum,
            append after command=\pgfextra{\xdef\ylastticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\xlastticknum-1)}
            \path (xlabel\xlastticknum.base) -- (xlabel\pgfmathresult.base) node [midway, anchor=base] {#1};
            \pgfmathparse{int(\ylastticknum-1)}
            \path (ylabel\ylastticknum.base) -- (ylabel\pgfmathresult.base) node [midway, anchor=base] {#2};
        }
    }
}


\begin{document}
  \begin{tikzpicture}
    \begin{axis}[din label=\si{\second} and \si{\meter}]
      \addplot table {
        0 0
        1 3
        2 4
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

The result: enter image description here