7

Could someone explain me why the following code compile perfectly :

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\pgfplotsset{/pgf/number format/.cd, 1000 sep={}, assume math mode=true}

\begin{document}

\begin{tikzpicture}

\begin{axis}[%
width=4cm,
height=4cm,
ymin=-0.5,
ymax=0,
]
\addplot [forget plot]
table[row sep=crcr]{%
    5   -0.000535\\
};
\end{axis}
\end{tikzpicture}
\end{document}

while if I want

ymin=-0.1,

it does not compile anymore. Error message :

Missing $ inserted.

Clues for a possible answer :

  • if the math mode is removed, the compilation is possible in both cases. But I dont want to remove it.

  • if I have ymin=-0.1 and ymax=0.5, it compiles too ...

Well ... I dont understand.

  • 1
    Why do you need assume math mode ? – BambOo Aug 8 '18 at 10:04
  • im using it elsewhere on my document ;-) maybe i can remove it and adapt the document, but this is not the point here :-( – Viv Aug 8 '18 at 10:27
  • See my answer below – BambOo Aug 8 '18 at 10:32
  • 1
    Adding yticklabels={} to the axis options seems to remove the error. This seems related to the automatic computation of ticks or ticklabels in this situation. – BambOo Aug 8 '18 at 11:17
3

Your attempt fails, because PGFPlots changes the way the yticks/yticklabels are drawn when you change ymin to -0.1 (see left plot). And this of course cannot be drawn in text mode, because in text mode neither \cdot nor ^ are known/can be interpreted.

To circumvent this problem you can change the number format of the yticklabels "back" to fixed (see right plot). (Optionally you can move the yticklabel style part to the preamble as well.)

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        /pgf/number format/.cd,
            1000 sep={},
            assume math mode=true,
    }
\begin{document}
% dummy plot to show where the problem is coming from
\begin{tikzpicture}
    \begin{axis}[
        width=4cm,
        height=4cm,
        ymin=-0.1,
        ymax=0,
        % don't assume math mode
        /pgf/number format/assume math mode=false,
    ]
        \addplot table {
            5   -0.000535
        };
    \end{axis}
\end{tikzpicture}
% plot showing how to circumvent the problem
\begin{tikzpicture}
    \begin{axis}[
        width=4cm,
        height=4cm,
        ymin=-0.1,
        ymax=0,
        % change number format to `fixed'
        yticklabel style={
            /pgf/number format/fixed,
%            % (optionally change the `precision' to your needs)
%            /pgf/number format/precision=2,
        },
    ]
        \addplot table {
            5   -0.000535
        };
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Exactly what i was waiting for as explanation ! Thank you very much – Viv Aug 14 '18 at 8:24
2

If you do not need assume math mode=true for this specific tikzpicture, you can pass /pgf/number format/assume math mode=false to the axis environment options or to the tikzpicture environment options to avoid the error.

  • another good solution in deed ! But i fact i spent one our to understand that the problem was coming from the math mode, i dont really understrand why it behaves like that ... – Viv Aug 8 '18 at 10:35
  • Maybe more advanded users as @marmot could answer that more precisely, but try to plot something almost empty may be the reason... – BambOo Aug 8 '18 at 10:39
  • I dont think this is the reason because the problem appeared with a huge figure ;-) – Viv Aug 8 '18 at 11:05

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.