0

In the following MWE, the tikz picture is outside of the paper. When I replace 10 by a bigger number, the pictures goes even further to the right. Any idea ?

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfmathdeclarefunction{myfunction}{1}{
    \let\x\pgfmathresult
    \pgfmathsetmacro\ret{0}
    \foreach \i in {1,...,10}{
        \pgfmathsetmacro\ret{\ret + \x^\i}
        \xdef\ret{\ret}
    }
    \pgfmathparse{\ret}
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[axis lines=middle, domain=-1:1]
        \addplot[domain=-1:1, blue, samples=10] {myfunction(x)};
    \end{axis}
\end{tikzpicture}

\end{document}
1

1 Answer 1

1

As mentioned in the comment, add percent signs. (one of the percent sign can be omitted in this case with no harm because of TeX rules, but in this particular case it's okay to have all of them as well.)

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfmathdeclarefunction{myfunction}{1}{%
    \let\x\pgfmathresult
    \pgfmathsetmacro\ret{0}%
    \foreach \i in {1,...,10}{%
        \pgfmathsetmacro\ret{\ret + \x^\i}%
        \xdef\ret{\ret}%
    }%
    \pgfmathparse{\ret}%
}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[axis lines=middle, domain=-1:1]
        \addplot[domain=-1:1, blue, samples=10] {myfunction(x)};
    \end{axis}
\end{tikzpicture}

\end{document}

Alternatively use expl3 syntax, but this ignores all spaces and does some other changes, so make sure you know what you do.

\ExplSyntaxOn
\pgfmathdeclarefunction{myfunction}{1}{
    \let\x\pgfmathresult
    \pgfmathsetmacro\ret{0}
    \foreach \i in {1,...,10}{
        \pgfmathsetmacro\ret{\ret + \x^\i}
        \xdef\ret{\ret}
    }
    \pgfmathparse{\ret}
}
\ExplSyntaxOff

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.