1

I have the following code that I don't like and generates an error:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
  \draw[red, thick] (0,0) --(6,0);
  \draw[blue, thick] (5,0.2) -- (9,0.2);
  \draw plot[smooth] coordinates {(0,0.6) (0.4,1) (1,0.4) (1.6,1.2) (2,1.4) (2.6,0.6) (3.4,1.8) (3.8,1.2) (4.4,1.2)(5,0.8) (5.4,0.4) (6,1) (6.6,2) (7,1.6) (7.4,1) (8.2,0.6) (9,0.4)};
\path[postaction={decorate}, decoration={markings, mark ={between positions 0.6 and 1 step 0.04 with {\fill[blue] circle[radius=0.08cm];}}}] plot[smooth] coordinates {(0,0.6) (0.4,1) (1,0.4) (1.6,1.2) (2,1.4) (2.6,0.6) (3.4,1.8) (3.8,1.2) (4.4,1.2)(5,0.8) (5.4,0.4) (6,1) (6.6,2) (7,1.6) (7.4,1) (8.2,0.6) (9,0.4)};
    \path[postaction={decorate}, decoration={markings, mark ={between positions 0 and 0.7 step 0.04 with {\fill[red] circle[radius=0.05cm];}}}] plot[smooth] coordinates {(0,0.6) (0.4,1) (1,0.4) (1.6,1.2) (2,1.4) (2.6,0.6) (3.4,1.8) (3.8,1.2) (4.4,1.2)(5,0.8) (5.4,0.4) (6,1) (6.6,2) (7,1.6) (7.4,1) (8.2,0.6) (9,0.4)};
\end{tikzpicture}
\end{document}

What I don't like is the repetition of the same commands, I am not sure how to streamline the code.

Second issue is that i get an error "Dimension too large" when compiling. I have googled the error but I could not really understand how the suggested solutions fit to my problem.

Any suggestion/pointer is very appreciated.

geguze
  • 470
  • 2
  • 11

2 Answers2

2

There are two questions. The answer to "how can I avoid repetition" is easy: you can add as many postactions to a path as you want as long as you set the braces right, i.e. instead of

`postaction={decorate}, decoration={...}`

you need

postaction={decorate, decoration={...}}`

AFAIK the second question "how can I avoid dimension too large errors" does not have a universal answer. The only information that I can offer here is that sometimes scaling the picture helps, and, as shown below, the smooth paths created with hobby are much less likely to have the issue. (In fact, so far I had never a dimension too large problem with hobby paths.) However, the smooth curve also looks somewhat different from what you get with a plain plot [smooth] coordinates.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{hobby,decorations.markings}
\begin{document}
\begin{tikzpicture}
  \draw[red, thick] (0,0) --(6,0);
  \draw[blue, thick] (5,0.2) -- (9,0.2);
  \draw[
    postaction={decorate, decoration={markings, mark ={between positions 0.6 and 1 step 0.04 with {\fill[blue] circle[radius=0.08cm];}}}},
    postaction={decorate, decoration={markings, mark ={between positions 0 and 0.7 step 0.04 with {\fill[red] circle[radius=0.05cm];}}}}
    ] plot[smooth,hobby] coordinates {(0,0.6) (0.4,1) (1,0.4) (1.6,1.2) (2,1.4) (2.6,0.6) (3.4,1.8) (3.8,1.2) (4.4,1.2)(5,0.8) (5.4,0.4) (6,1) (6.6,2) (7,1.6) (7.4,1) (8.2,0.6) (9,0.4)};
\end{tikzpicture}
\end{document}

enter image description here

2

Here are some news concerning the dimension too large issue. It turns out that a lot of the problems come from the built-in way pgf computes inverses. This issue came up here, and interestingly the same fix that solved the problem there also cures the problem of the decorations here. All it really does is to change the reciprocal to a fpu version. The corresponding pgf key use fpu reciprocal can be used locally.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.markings,fpu}
% https://tex.stackexchange.com/a/529159/194703
\makeatletter
\tikzset{use fpu reciprocal/.code={%
\def\pgfmathreciprocal@##1{%
    \begingroup
    \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}%
    \pgfmathparse{1/##1}%
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}}}%
\makeatother
\begin{document}
\begin{tikzpicture}[use fpu reciprocal]
\draw[postaction={decorate, decoration={markings, mark ={between positions 0 and 0.7
step 0.04 with {\fill[red] circle[radius=0.05cm];}}}},
postaction={decorate, decoration={markings, 
mark ={between positions 0.6 and 1 step 0.04 with 
{\fill[blue] circle[radius=0.08cm];}}}}] 
plot[smooth] coordinates {(0,0.6) (0.4,1) (1,0.4) (1.6,1.2) (2,1.4) (2.6,0.6) (3.4,1.8) (3.8,1.2) (4.4,1.2)(5,0.8) (5.4,0.4) (6,1) (6.6,2) (7,1.6) (7.4,1) (8.2,0.6) (9,0.4)};
\end{tikzpicture}
\end{document}

enter image description here