1

I have a pgfplot and the labels in the x axis are not centered as I expected and this is because one of them is higher than the other because of the superscript.

\documentclass[]{article}  % Use the "Thesis" style, based on the ECS Thesis style by Steve Gunn

\usepackage{pgfplots}

\begin{document}

    \begin{tikzpicture}
        \begin{axis}[
            ytick={\empty},
            ytick={},
            xticklabels={$u_{n+1}^{}$,$u_{n+1}^{0}$},
            xtick={0,4},
            yticklabels={$r(u_{n+1}^{}) = 0$,$r(u_{n+1}^{0})>0$},
            ytick={-2,2},
            domain=-2:6,
            samples=55,
            scaled ticks=false,
            ymax=10,
            ymin=-5,
            xtick distance={60},
            ]
            %\addplot+[no marks,thick] {-5*(x-1)*(x)*(x-2)};
            \addplot[smooth,thick]
                coordinates{
                    (-1.9,9.9) (-1,0) (0,-2) 
                    (1,0)(3,4)(4,2)
                    (5,4)(6,10)
                };
            \draw[dashed] (axis cs:0,-5) -- (axis cs:0,-2);
            \draw[dashed] (axis cs:4,-5) -- (axis cs:4,2);
            \draw[dashed] (axis cs:-3,-2) -- (axis cs:0,-2);
            \draw[dashed] (axis cs:-3,2) -- (axis cs:4,2);
            \node[circle,draw=black,fill=black,scale=0.8] at (axis cs:0,-2) {};
            \node[circle,draw=black,fill=black,scale=0.8] at (axis cs:4,2) {};
        \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here

And a zoom of the axis:

enter image description here

How can I do to center the letter u to the same distance ?

2
  • Just add xticklabel style={text height=2ex},. – user121799 Mar 17 '19 at 10:50
  • It has not worked :( I tried also with 20ex and I see no difference. – gagiuntoli Mar 17 '19 at 10:55
1

On my machine adding xticklabel style={text height=2ex} works.

\documentclass[]{article}  % Use the "Thesis" style, based on the ECS Thesis style by Steve Gunn

\usepackage{pgfplots}

\begin{document}

    \begin{tikzpicture}
        \begin{axis}[
            ytick={\empty},
            ytick={},
            xticklabels={$u_{n+1}^{}$,$u_{n+1}^{0}$},
            xtick={0,4},
            yticklabels={$r(u_{n+1}^{}) = 0$,$r(u_{n+1}^{0})>0$},
            xticklabel style={text height=2ex},
            ytick={-2,2},
            domain=-2:6,
            samples=55,
            scaled ticks=false,
            ymax=10,
            ymin=-5,
            xtick distance={60},
            ]
            %\addplot+[no marks,thick] {-5*(x-1)*(x)*(x-2)};
            \addplot[smooth,thick]
                coordinates{
                    (-1.9,9.9) (-1,0) (0,-2) 
                    (1,0)(3,4)(4,2)
                    (5,4)(6,10)
                };
            \draw[dashed] (axis cs:0,-5) -- (axis cs:0,-2);
            \draw[dashed] (axis cs:4,-5) -- (axis cs:4,2);
            \draw[dashed] (axis cs:-3,-2) -- (axis cs:0,-2);
            \draw[dashed] (axis cs:-3,2) -- (axis cs:4,2);
            \node[circle,draw=black,fill=black,scale=0.8] at (axis cs:0,-2) {};
            \node[circle,draw=black,fill=black,scale=0.8] at (axis cs:4,2) {};
        \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here

1
  • 1
    yes, sorry I was doing something wrong, now it works :) – gagiuntoli Mar 17 '19 at 11:11
0

To overcome this there exists the option typeset ticklabels with strut. Simply add it to the axis options and you receive the expected result.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick={0,4},
        xticklabels={$u_{n+1}^{}$,$u_{n+1}^{0}$},
        ymin=-5,
        ymax=10,
        ytick={-2,2},
        yticklabels={$r(u_{n+1}^{}) = 0$,$r(u_{n+1}^{0})>0$},
        typeset ticklabels with strut,      % <-- added
    ]
        \addplot [smooth,thick] coordinates {
            (-1.9,9.9) (-1,0) (0,-2)
            (1,0)(3,4)(4,2)
            (5,4)(6,10)
        };
        \draw [dashed] (axis cs:0,-5) -- (axis cs:0,-2);
        \draw [dashed] (axis cs:4,-5) -- (axis cs:4,2);
        \draw [dashed] (axis cs:-3,-2) -- (axis cs:0,-2);
        \draw [dashed] (axis cs:-3,2) -- (axis cs:4,2);
        \node [circle,draw=black,fill=black,scale=0.8] at (axis cs:0,-2) {};
        \node [circle,draw=black,fill=black,scale=0.8] at (axis cs:4,2) {};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.