4

A scale factor factor for xticks does not work with ldots mask .... Does the problem have a solution?

\documentclass[tikz]{standalone}
\usepackage{pgfplots}


%===============================================================================
\begin{document}
\noindent
\begin{tikzpicture}[
    declare function ={
        factor=2;
    }
    ]
    \begin{axis}[
        %xtick = {-2*factor,2*factor}, % This one work
        %xtick = {-2,...,2}, % This one work
        xtick = {-2*factor,...,2*factor}, % This one doesn't work
        ]
        \addplot+[blue, no marks, domain={-6:6}] {x^2};
    \end{axis}
\end{tikzpicture}


\end{document}
4

You need to set /pgf/foreach/parse=true, see p. 989 of pgfmanual v3.1.4

enter image description here

This works (at least in in this case).

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

%===============================================================================
\begin{document}
\noindent
\begin{tikzpicture}[/pgf/foreach/parse=true,%<-added
    declare function ={
        factor=2;
    }
    ]
    \begin{axis}[
        %xtick = {-2*factor,2*factor}, % This one work
        %xtick = {-2,...,2}, % This one work
        xtick={-2*factor,...,2*factor}, % This one works now
        ]
        \addplot+[blue, no marks, domain={-6:6}] {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Let me mention that I recommend not to overdo it with parse=true, there are cases with integers in which I had some difficulties.

Just in case you encounter problems with this proposal because some other, perhaps internal, loop causes problems: you could also use xtickmin and xtickmax, whose values get automatically parsed.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

%===============================================================================
\begin{document}
\noindent
\begin{tikzpicture}[
    declare function ={
        factor=2;
    }
    ]
    \begin{axis}[
        xtickmin=-2*factor,
        xtickmax=2*factor,
        ]
        \addplot+[blue, no marks, domain={-6:6}] {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |

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.