5

I have adapted an example from TEXample.net (Original author: Andreas Menge, http://www.texample.net/tikz/examples/3d-graph-model/) for creating parallelepipeds with a helper grid to give perspective. This was working fine up until a certain point when the drawing of some of the edges resulted in a Dimension too large error.

\documentclass[10pt]{article}
\usepackage{tikz}

\newcommand{\myGlobalTransformation}[3]
{
    \pgftransformcm{1}{0}{0.4}{0.5}{\pgfpoint{#1cm}{#2cm}}
}

\newcommand{\gridThreeD}[3]
{
\begin{scope}
    \myGlobalTransformation{#1}{#2};
    \draw [#3,step=1cm] grid (3,6);
\end{scope}
}

\newcommand{\drawLinewithBG}[2]
{
\draw[black,very thick] (#1) -- (#2);
}

\begin{document}
\begin{tikzpicture}

\gridThreeD{0}{0}{black!50};
\gridThreeD{0}{4}{black!50};

\begin{scope}
    \myGlobalTransformation{0}{0};

    \foreach \x in {1,2} {
        \foreach \y in {1,2} {
            \node (thisNode) at (\x,\y) [scale=0.01] {};
            {
               \pgftransformreset
                \draw[black,very thick] (thisNode) -- ++(0,4,-1.97); %----- anything less than z= -1.96 gives the error
           }
        }
    }

\end{scope}

\end{tikzpicture}    
\end{document}

(I have stripped down the code here to highlight the issue). The desired result would be the lines drawn up to a square with corners (1,4)(1,5)(2,4)(2,5) on the top helper grid. It seems there is no error if lines are drawn such that they are coming out of the page (i.e. when the z coordinate is +1.97 in the code, effectively a rotation of the shape by 180 degrees), so that may provide a possible solution.

There have been a lot of previous questions about this error, often answered with the suggestion of using fpu. I have tried this, but to no avail. Also at a glance the numbers involved here don't seem to be beyond the ~16384 limit.

Any suggestions would be much appreciated, thanks.

4
  • 3
    Instead of node, use \coordinate (thisNode) at (\x,\y); inside the foreach loops. And global transformation macro is redundant. Use \pgftransformshift instead (note that it expects three arguments as is but uses 2)
    – percusse
    Jul 7 '14 at 17:14
  • Ah thank you, works perfectly now! I will give \pgftransformshift a try as well.
    – cuboid42
    Jul 7 '14 at 17:37
  • @percusse Would you consider writing an answer which explains what the problem is and why that's a solution? I get this error from time-to-time with tikz and never have any idea why or what to do about it.
    – cfr
    Jul 8 '14 at 1:34
  • @percusse care to answer? Apr 19 '15 at 1:19
2

When you get a Dimension too large error from TikZ, there are two typical (and lots of other nontrivial) reasons. Either something is too small, or something too short is used in a computation to arrive at divide-by-almost zero. TikZ catches divide-by-zeros most of the times.

Here, I am too lazy to dig deep but I think the following screenshot tells why this is happening if we add fill=red option to the node, make the line very thin and pump the zoom up.

enter image description here

Because of the 1/100 scaling the node shape is shrunk to its extreme small shape however it still has a shape and a shape border. So when you refer to a node for line drawing as \draw (a) -- (b); it still looks for the point on the shape border to start. Here for some numerical reason z value below -1.96 trips up the math engine.

To overcome this, you can use another node shape namely coordinate which has nothing but a center anchor and no background/foreground paths associated with it. So it is possible to see it as a named position. Then TikZ skips the shape border calculation and we are back in business.

I've cleaned up the code a little bit with TikZ styles.

\documentclass[10pt]{article}
\usepackage{tikz}

\tikzset{my global trafo/.style={cm ={1,0,0.4,0.5,#1}}}

\newcommand{\gridThreeD}[3]{
\begin{scope}[my global trafo={(#1,#2)}]
    \draw [#3,step=1cm] grid (3,6);
\end{scope}}

\newcommand{\drawLinewithBG}[2]{\draw[black,very thick] (#1) -- (#2);}

\begin{document}
\begin{tikzpicture}

\gridThreeD{0}{0}{black!50};
\gridThreeD{0}{4}{black!50};
\begin{scope}[my global trafo={(0,0)}]
    \foreach \x in {1,2} {
        \foreach \y in {1,2} {
            \coordinate (thisNode) at (\x,\y);
            \begin{scope}[reset cm]
                \draw[very thick] (thisNode) -- ++(0,4,-1.97);
            \end{scope}
        }
    }
\end{scope}

\end{tikzpicture}    
\end{document}

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.