3

In answer of the question Origin and formation to the ERROR ‘Dimension too large’ is given only a hint, how the problem can be solved with package fp. Please could someone give a working solution, that demonstrate right way for drawing similar pictures. I constantly run into this problem in my tikz pictures.

Similar NMWE

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{calculator}
\usepackage{xcolor}

\begin{document}
  \def\nstep{0.1}
  \def\angle{45}
  \def\diameter{1}
  \def\n{200}
 \newcommand{\PI}{3.1415}

\begin{tikzpicture}[ y=0.01cm,x=0.2cm]
  \begin{scope}[ domain=-10:10]
    \draw[red](0 cm, 0 cm) circle (2mm);
    \draw[red](0 cm, 20cm) circle (2mm);
    \draw(0 cm,\PI*\diameter*\angle*\n*\nstep/180) circle (2mm);  % Dimension too large
    \draw[red](0 cm, 15.7 cm) circle (2mm);   % for n =200
    \draw[blue](0 cm, 7.85 cm) circle (2mm);  % for n =100
   \end{scope}
 \end{tikzpicture}
 \end{document} 

It made me so mad. I want to draw a simple picture, and I can not even calculate a simple y-coordinate.

JardaFait
  • 3,922

1 Answers1

4

You are seeing a classical overflow — the fastest way to fix it is with

\draw(0 cm,\PI/180*\diameter*\angle*\n*\nstep) circle (2mm);

(you'll learn this when programming microcontrollers — the difference is that at least LaTeX errors out instead of crashing your robot).

Anyway, as a general thing, I will share what I know about math calculations of big numbers in LaTeX. This thing is quite complex sometime — and you are mixing over there numbers and lengths, so I'll use another example.

\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}

\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff

\begin{document}
\def\ten{10}
\def\hundred{100}
\begin{tikzpicture}[ x=0.2cm, y=0.1cm]
    \filldraw [black] (0, 0) circle (1mm);    % origin    
    \draw[red](1 cm, 1 cm) circle (2cm);      % absolute shift
    \draw[black] (1, 1) circle (2cm);         % x-y shift
    % this will use x-y coords (going "on top" of the black one)
    \draw[cyan, dashed] (1, \fpeval{\ten*\hundred*\hundred/100000}) circle (2cm);
    % while this will errors out
    % \draw[cyan, dashed] (1, \ten*\hundred*\hundred/100000) circle (2cm);
    % this will use absolute coords (on top of the red one) 
    \draw[yellow, dashed] (1 cm, \fpeval{\ten*\hundred*\hundred/100000} cm) circle(2cm);
 \end{tikzpicture}
\end{document} 

will produce:

couple of circles

Without expl3, you can use the fpu PGF library, that is a bit convoluted... (if anyone reading this can correct me, please help). The code will be:

\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fpu}

\begin{document}
\def\ten{10}
\def\hundred{100}
\begin{tikzpicture}[ x=0.2cm, y=0.1cm]
    \filldraw [black] (0, 0) circle (1mm);    % origin    
    \draw[red](1 cm, 1 cm) circle (2cm);      % absolute shift
    \draw[black] (1, 1) circle (2cm);         % x-y shift

    \pgfkeys{/pgf/fpu=true, /pgf/fpu/output format=fixed}%
    \pgfmathsetmacro\mynum{\ten*\hundred*\hundred/100000}%
    \pgfkeys{/pgf/fpu=false}
    % this will use x-y coords (going "on top" of the black one)
    \draw[cyan, dashed] (1, \mynum) circle (2cm);
    % while this will errors out
    % \draw[cyan, dashed] (1, \ten*\hundred*\hundred/100000) circle (2cm);
    % this will use absolute coords (on top of the red one) 
    \draw[yellow, dashed] (1 cm, \mynum cm) circle(2cm);
 \end{tikzpicture}
\end{document}

and you'll have the same exact output.

Rmano
  • 40,848
  • 3
  • 64
  • 125