使其更紧凑并解决“尺寸太大”的问题

使其更紧凑并解决“尺寸太大”的问题

我有以下我不喜欢并会产生错误的代码:

\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}

我不喜欢重复相同的命令,我不知道如何简化代码。

第二个问题是我在编译时收到错误“维度太大”。我在 Google 上搜索了错误,但我真的不明白建议的解决方案如何解决我的问题。

非常感谢任何建议/指示。

答案1

有两个问题。“如何避免重复”的答案很简单:只要正确设置括号,就可以向路径添加任意数量的后续操作,即

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

你需要

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

据我所知,第二个问题“我如何避免dimension too large错误”没有统一的答案。我在这里能提供的唯一信息是,有时缩放图片会有所帮助,并且如下所示,使用创建的平滑路径hobby不太可能出现问题。(事实上,到目前为止,我从未遇到过路径dimension too large问题hobby。)但是,平滑曲线看起来也与使用普通的 得到的曲线略有不同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}

在此处输入图片描述

答案2

以下是有关该dimension too large问题的一些新闻。事实证明,很多问题都来自 pgf 计算逆的内置方式。这个问题出现了这里,有趣的是,解决那里问题的相同修复也解决了这里的装饰问题。它实际上所做的只是将倒数更改为版本fpu。相应的 pgf 密钥use fpu reciprocal可以在本地使用。

\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}

在此处输入图片描述

相关内容