4

I'm trying to format all y tick labels in a pgfplots plot so that comma is the decimal separator. What I've tried is:

\documentclass{standalone}

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

\begin{document}
\begin{tikzpicture}
  \begin{axis}[axis lines = center, xlabel = {$x$}, ylabel = {$f(x)$},
     /pgf/number format/.cd, use comma, 
    extra y ticks = {8.25}, extra y tick labels = {$\theta = 8.25$}]
    \addplot [domain = 0:8] {x^2}; 
  \end{axis}
\end{tikzpicture}
\end{document}

which gives

MWE output PDF

I'd also like the decimal separators to be aligned properly (in case other y ticks use one) even with the \theta = part being present.

Using \pgfmathprintnumber{8.25} gives me the correct decimal separator (,) but I can't get the \theta = positioned properly.

1 Answer 1

2

If all the extra y ticks should have \theta = before the number, you can use

extra y tick label = {$\theta = \pgfmathprintnumber{\tick}$}

instead of the extra y tick labels. Or if it's just the one, you could modify your code to extra y tick labels = {$\theta = \pgfmathprintnumber{8.25}$}.

Commas will line up given that you've set the same precision for all the ticklabels, see second example.

\documentclass[border=5mm]{standalone}

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

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis lines = center,
    xlabel = {$x$},
    ylabel = {$f(x)$},
    /pgf/number format/.cd, use comma,
    extra y ticks = {8.25,28.45},
    extra y tick label = {$\theta = \pgfmathprintnumber{\tick}$},
    ]
    \addplot [domain = 0:8] {x^2}; 
  \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis}[
    axis lines = center,
    xlabel = {$x$},
    ylabel = {$f(x)$},
    /pgf/number format/.cd, use comma,
    extra y ticks = {8.25,28.4},
    extra y tick label = {$\theta = \pgfmathprintnumber{\tick}$},
    yticklabel style={/pgf/number format/.cd,
                      fixed,fixed zerofill,precision=2}
    ]
    \addplot [domain = 0:8] {x^2}; 
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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