我正在尝试创建一组命令来自动绘制一些具有复杂数学关系的函数。但是,我在运行该函数时遇到此错误。我在命令中添加三角函数后遇到了这个问题。我该如何调试此错误?
%pdfLaTeX
\documentclass{article}
\usepackage{tikz}
\newcommand{\test}[1]{
\begin{tikzpicture}
%
\def\dh{{#1}[0]}
\def\db{{#1}[1]}
\def\dt{{#1}[2]}
\def\dr{{#1}[3]}
\def\dd{{#1}[4]}
\def\ag{{#1}[5]}
%
\def\lw{0.15mm}
%
\coordinate (1) at (0,{(\dr+\dt)*(1-cos(\ag))+\dd*sin(\ag)});
\coordinate (3) at ({\dd*cos(\ag)},{(\dr+\dt)*(1-cos(\ag))});
\coordinate (5) at ({\dd*cos(\ag)+(\dr+\dt)*sin(\ag)},0);
\coordinate (7) at ({\dd*cos(\ag)+\db-\dt-\dr},0);
\coordinate (9) at ({\dd*cos(\ag)+\db},{\dt+\dr});
\coordinate (11) at ({\dd*cos(\ag)+\db},{\dh-\dt-\dr});
\coordinate (13) at ({\dd*cos(\ag)+\db+\dr},{\dh-\dt});
\coordinate (15) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*sin(\ag)},{\dh-\dt});
\coordinate (17) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*cos(\ag)},{\dh-\dt-\dr*(1-cos(\ag))});
\coordinate (19) at ({2*(\dd*cos(\ag)+\db)-\dt},{\dh-\dt-\dr*(1-cos(\ag))-\dd*sin(\ag)});
%
\draw [line width=\lw] (1) -- (3) (5) -- (7) (9) -- (11) (13) -- (15) (17) -- (19);
\end{tikzpicture}
}
\begin{document}
\centering
\test{16,6,0.3,0.8,2.91,60}
\end{document}
答案1
使用pgfmath
命令来pgfmath
做事。顺便说一句,你的身材太大了。
\documentclass{article}
\usepackage{tikz}
\newcommand{\test}[1]{
\begin{tikzpicture}
%
\pgfmathsetmacro\dh{{#1}[0]}
\pgfmathsetmacro\db{{#1}[1]}
\pgfmathsetmacro\dt{{#1}[2]}
\pgfmathsetmacro\dr{{#1}[3]}
\pgfmathsetmacro\dd{{#1}[4]}
\pgfmathsetmacro\ag{{#1}[5]}
%
\def\lw{0.15mm}
%
\coordinate (1) at (0,{(\dr+\dt)*(1-cos(\ag))+\dd*sin(\ag)});
\coordinate (3) at ({\dd*cos(\ag)},{(\dr+\dt)*(1-cos(\ag))});
\coordinate (5) at ({\dd*cos(\ag)+(\dr+\dt)*sin(\ag)},0);
\coordinate (7) at ({\dd*cos(\ag)+\db-\dt-\dr},0);
\coordinate (9) at ({\dd*cos(\ag)+\db},{\dt+\dr});
\coordinate (11) at ({\dd*cos(\ag)+\db},{\dh-\dt-\dr});
\coordinate (13) at ({\dd*cos(\ag)+\db+\dr},{\dh-\dt});
\coordinate (15) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*sin(\ag)},{\dh-\dt});
\coordinate (17) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*cos(\ag)},{\dh-\dt-\dr*(1-cos(\ag))});
\coordinate (19) at ({2*(\dd*cos(\ag)+\db)-\dt},{\dh-\dt-\dr*(1-cos(\ag))-\dd*sin(\ag)});
%
\draw [line width=\lw] (1) -- (3) (5) -- (7) (9) -- (11) (13) -- (15) (17) -- (19);
\end{tikzpicture}
}
\begin{document}
\centering
\test{16,6,0.3,0.8,2.91,60}
\end{document}
笔记
我认为这是一个 XY 问题。您可能想根据一些参数绘制图形。在这种情况下,宏不是正确的方法。您应该改用
pic
。\documentclass{article} \usepackage{tikz} \tikzset{ yourpicname/.pic={ \pgfmathsetmacro\dh{{#1}[0]} \pgfmathsetmacro\db{{#1}[1]} \pgfmathsetmacro\dt{{#1}[2]} \pgfmathsetmacro\dr{{#1}[3]} \pgfmathsetmacro\dd{{#1}[4]} \pgfmathsetmacro\ag{{#1}[5]} \coordinate (1) at (0,{(\dr+\dt)*(1-cos(\ag))+\dd*sin(\ag)}); \coordinate (3) at ({\dd*cos(\ag)},{(\dr+\dt)*(1-cos(\ag))}); \coordinate (5) at ({\dd*cos(\ag)+(\dr+\dt)*sin(\ag)},0); \coordinate (7) at ({\dd*cos(\ag)+\db-\dt-\dr},0); \coordinate (9) at ({\dd*cos(\ag)+\db},{\dt+\dr}); \coordinate (11) at ({\dd*cos(\ag)+\db},{\dh-\dt-\dr}); \coordinate (13) at ({\dd*cos(\ag)+\db+\dr},{\dh-\dt}); \coordinate (15) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*sin(\ag)},{\dh-\dt}); \coordinate (17) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*cos(\ag)},{\dh-\dt-\dr*(1-cos(\ag))}); \coordinate (19) at ({2*(\dd*cos(\ag)+\db)-\dt},{\dh-\dt-\dr*(1-cos(\ag))-\dd*sin(\ag)}); \draw [pic actions] (1) -- (3) (5) -- (7) (9) -- (11) (13) -- (15) (17) -- (19); } } \begin{document} \centering \tikz\pic[line width=0.15mm,red]{yourpicname={16,6,0.3,0.8,2.91,60}}; \end{document}
如果您想坚持使用宏,我推荐这个版本,您可以在其中更改
line width
并删除一个宏。\documentclass{article} \usepackage{tikz} \newcommand{\test}[2][0.15mm]{ \begin{tikzpicture} % \pgfmathsetmacro\dh{{#2}[0]} \pgfmathsetmacro\db{{#2}[1]} \pgfmathsetmacro\dt{{#2}[2]} \pgfmathsetmacro\dr{{#2}[3]} \pgfmathsetmacro\dd{{#2}[4]} \pgfmathsetmacro\ag{{#2}[5]} % \coordinate (1) at (0,{(\dr+\dt)*(1-cos(\ag))+\dd*sin(\ag)}); \coordinate (3) at ({\dd*cos(\ag)},{(\dr+\dt)*(1-cos(\ag))}); \coordinate (5) at ({\dd*cos(\ag)+(\dr+\dt)*sin(\ag)},0); \coordinate (7) at ({\dd*cos(\ag)+\db-\dt-\dr},0); \coordinate (9) at ({\dd*cos(\ag)+\db},{\dt+\dr}); \coordinate (11) at ({\dd*cos(\ag)+\db},{\dh-\dt-\dr}); \coordinate (13) at ({\dd*cos(\ag)+\db+\dr},{\dh-\dt}); \coordinate (15) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*sin(\ag)},{\dh-\dt}); \coordinate (17) at ({2*(\dd*cos(\ag)+\db)-\dt-\dr*cos(\ag)},{\dh-\dt-\dr*(1-cos(\ag))}); \coordinate (19) at ({2*(\dd*cos(\ag)+\db)-\dt},{\dh-\dt-\dr*(1-cos(\ag))-\dd*sin(\ag)}); % \draw [line width=#1] (1) -- (3) (5) -- (7) (9) -- (11) (13) -- (15) (17) -- (19); \end{tikzpicture} } \begin{document} \centering \test{16,6,0.3,0.8,2.91,60} \test[0.3mm]{1,2,3,4,5,6} \end{document}