我希望编写一个宏,这样在选项中,我可以更改水平长度和垂直长度以自动生成图像。我只使用 \hordisplay,因为有些长度不能很好地缩放。如果有人能建议更好的方法,那也很好。
我想画一个三角形并能够要求输入 \hor (水平长度)并要求输入 \vertical 垂直长度。然后让 tikz 为我绘制一个漂亮的比例图。
\documentclass[12pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{trees,quotes,angles}
\begin{document}
\begin{tikzpicture}[scale=.8]
\newcommand{\hor}{3.24}
\newcommand{\vertical}{1.68}
\pgfmathsetmacro{\hordisplay}{\hor*10}
\pgfmathsetmacro{\vertdisplay}{\vertical*10}
\coordinate (A) at (0,0);
\coordinate (B) at (\hor,0);
\coordinate (C) at (0,\vertical);
\draw(A)--node[midway, below]{$\hordisplay$ m}(B)--(C)--node[midway,left]{$\vertdisplay$ m}cycle;
\draw[|-|,blue] ([xshift=1mm,yshift=2.5mm]B)--node[black,pos=0.5,fill=white,yshift=1mm]{$x$}([xshift=1mm,yshift=2.5mm]C);
\tkzMarkRightAngle[draw=blue,size=.2](B,A,C);
\end{tikzpicture}
\end{document}
答案1
这是使用 pgf 键的提议。您可以将其用作\MyTrianle{h=3.24,v=1.68}
,指定参数的顺序无关紧要。可选参数可以使用键(例如thick
或缩放等)来输入。
\documentclass[12pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tkz-euclide}
%\usetkzobj{all}%<- if you need this line, it is time to update your TeX installation
\newcommand{\MyTrianle}[2][]{\begin{tikzpicture}[scale=.8,#1,
triangle/.cd,h/.initial=1,v/.initial]
\tikzset{triangle/.cd,#2}
\newcommand{\hor}{\pgfkeysvalueof{/tikz/triangle/h}}
\newcommand{\vertical}{\pgfkeysvalueof{/tikz/triangle/v}}
\pgfmathsetmacro{\hordisplay}{\hor*10}
\pgfmathsetmacro{\vertdisplay}{\vertical*10}
\coordinate (A) at (0,0);
\coordinate (B) at (\hor,0);
\coordinate (C) at (0,\vertical);
\draw(A)--
node[midway, below]{$\pgfmathprintnumber\hordisplay\,$m}(B)--(C)--
node[midway,left]{$\pgfmathprintnumber\vertdisplay\,$m}cycle;
\draw[|-|,blue] ([xshift=1mm,yshift=2.5mm]B)--node[black,pos=0.5,fill=white,yshift=1mm]{$x$}([xshift=1mm,yshift=2.5mm]C);
\tkzMarkRightAngle[draw=blue,size=.2](B,A,C);
\end{tikzpicture}}
\begin{document}
\MyTrianle{h=3.24,v=1.68}
\MyTrianle{h=4.5,v=2.7}
\MyTrianle{h=pi,v=e}
\end{document}
您可以随时通过添加更多键来升级宏。
\documentclass[12pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tkz-euclide}
%\usetkzobj{all}%<- if you need this line, it is time to update your TeX installation
\newcommand{\MyTrianle}[2][]{\begin{tikzpicture}[scale=.8,#1,
triangle/.cd,h/.initial=1,v/.initial,unit/.initial=m,factor/.initial=10]
\tikzset{triangle/.cd,#2}
\newcommand{\hor}{\pgfkeysvalueof{/tikz/triangle/h}}
\newcommand{\vertical}{\pgfkeysvalueof{/tikz/triangle/v}}
\pgfmathsetmacro{\hordisplay}{\hor*\pgfkeysvalueof{/tikz/triangle/factor}}
\pgfmathsetmacro{\vertdisplay}{\vertical*\pgfkeysvalueof{/tikz/triangle/factor}}
\coordinate (A) at (0,0);
\coordinate (B) at (\hor,0);
\coordinate (C) at (0,\vertical);
\draw(A)--
node[midway,below]{$\pgfmathprintnumber\hordisplay\,$\pgfkeysvalueof{/tikz/triangle/unit}}(B)--(C)--
node[midway,left]{$\pgfmathprintnumber\vertdisplay\,$\pgfkeysvalueof{/tikz/triangle/unit}}cycle;
\draw[|-|,blue] ([xshift=1mm,yshift=2.5mm]B)--node[black,pos=0.5,fill=white,yshift=1mm]{$x$}([xshift=1mm,yshift=2.5mm]C);
\tkzMarkRightAngle[draw=blue,size=.2](B,A,C);
\end{tikzpicture}}
\begin{document}
\MyTrianle{h=3.24,v=1.68}
\MyTrianle{h=4.5,v=2.7,factor=100}
\MyTrianle{h=pi,v=e,unit=cm}
\end{document}