3
  • I want to have an extra x tick.
  • In my "real" example, it is needed that the extra x tick label is moved in -y direction (using major tick length).
  • pgfplots cleverly also moves the xlabel in -y direction automatically.
  • This normally desriable behavior is a problem in my special case.
  • Question: Can I prevent that the position of the xlabel is affected by major tick length? I do not want to manually use xlabel shift = -20pt, because in my real example this still causes a minor difference.

\documentclass{article} 

\usepackage{pgfplots} 
\pgfplotsset{compat = newest}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        axis x line= bottom,
        axis y line= left,
        xlabel=$x$,
        ylabel={$f(x) = x^2 - x +4$},
        extra x ticks = {-5},
        extra x tick style = {
            major tick length = 20pt, % <-- Relevant
            xtick align = outside,    % <-- Relevant
            }
    ]
    % use TeX as calculator:
    \addplot {x^2 - x +4};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        axis x line= bottom,
        axis y line= left,  
        xlabel=$x$,
        ylabel={$f(x) = x^2 - x +4$},
        extra x ticks = {-5},
    ]
    % use TeX as calculator:
    \addplot {x^2 - x +4};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

1 Answer 1

1

This is, because you need to take into account the normal major tick length as well ...

To prove that this works, comment the two lines starting with xlabel style and extra x ticks and take a close look when the PDF updates. You shouldn't notice a jump in the xlabel position.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
        % store the extra tick length in a variable, so you don't need to repeat yourself
        \pgfmathsetlengthmacro{\ExtraMajorTickLength}{20pt}
    \begin{axis}[
        axis x line=bottom,
        axis y line=left,
        xlabel=$x$,
        ylabel={$f(x) = x^2 - x + 4$},
        % ---------------------------------------------------------------------
        % this gives you the "correct" xlabel positioning
        % (the `/2` is needed, because the normal ticks are centered and thus
        %  only half of the tick length is below the x-axis line)
        xlabel style={
            yshift={\ExtraMajorTickLength + \pgfkeysvalueof{/pgfplots/major tick length}/2},
        },
        % ---------------------------------------------------------------------
        extra x ticks={-5},
        extra x tick style={
            major tick length=\ExtraMajorTickLength,
            xtick align=outside,
        },
    ]
        \addplot {x^2 - x + 4};
    \end{axis}
        % (for debugging purposes only to not change the size of the `tikzpicture`
        %  when commenting lines as stated in the text)
        \fill [red] (0,-1.2) circle [radius=1pt];
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • 1
    Thanks! I saw in the manual that the default length is 1.5mm but I wasn't sure what to do with that information :). Your solution is very elegant - thx! Apr 9, 2021 at 20:56
  • As always: You are welcome! Apr 10, 2021 at 5:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .