tikz作图,并排放置。问:代码有改进的方案吗?即tikz绘图有循环方法吗?图形排列有简单的方法吗?

2020-06-08 16:35发布

tikz## 编译环境 操作系统 * [ x] Windows 7/8/10 * [ ] macOS * [ ] Linux `若需勾选,请把[ ]改成[x]` Tex发行版 * [ x] TexL...

tikz## 编译环境 操作系统 * [ x] Windows 7/8/10 * [ ] macOS * [ ] Linux `若需勾选,请把[ ]改成[x]` Tex发行版 * [ x] TexLive `年份` * [ ] MikTeX `版本号` * [ ] CTeX `若需勾选,请把[ ]改成[x]` ## 我的问题 图形是人民教育出版社,七年级数学上册课本,第71页习题的图。下面给了一个实现的方法。问:代码有改进的方案吗?即tikz绘图有循环方法吗?图形排列有简单的方法吗? [![](https://wenda.latexstudio.net/data/attach/200608/Ahs83M4d.png)](https://wenda.latexstudio.net/data/attach/200608/Ahs83M4d.png) ```` \documentclass[UTF8]{ctexart} \usepackage{tikz} \usetikzlibrary {calc} \usepackage{graphicx} \usepackage{caption,subcaption} \begin{document} %要用的的包:tikz、calc、graphicx、caption。 %\end{minipage}%之后的“%”不能少,与一个图形不能有空行。 \begin{figure} \begin{minipage}[thtbp]{0.25\linewidth} \centering \begin{tikzpicture} \def\x{1cm} %利用参数定义长度。 \def\r{0.8mm} \coordinate (A) at (0,0); \coordinate (B) at (\x,0); \coordinate (C) at (60:\x); \fill[red] (A) circle (\r); \fill[red] (B) circle (\r); \fill[red] (C) circle (\r); \end{tikzpicture} \subcaption*{$n=2$} \end{minipage}% \begin{minipage}[thtbp]{0.25\linewidth} \centering \begin{tikzpicture} \def\x{1cm} %利用参数定义长度。 \def\r{0.8mm} \coordinate (A) at (0,0); \coordinate (B) at (\x,0); \coordinate (C) at (60:\x); \fill[red] (A) circle (\r); \fill[red] (B) circle (\r); \fill[red] (C) circle (\r); \fill[red] ($ (A)!.5!(B) $) circle (\r); \fill[red] ($ (B)!.5!(C) $) circle (\r); \fill[red] ($ (C)!.5!(A) $) circle (\r); \end{tikzpicture} \subcaption*{$n=3$} \end{minipage}% \begin{minipage}[thtbp]{0.25\linewidth} \centering \begin{tikzpicture} \def\x{1cm} %利用参数定义长度。 \def\r{0.8mm} \coordinate (A) at (0,0); \coordinate (B) at (\x,0); \coordinate (C) at (60:\x); \fill[red] (A) circle (\r); \fill[red] (B) circle (\r); \fill[red] (C) circle (\r); \fill[red] ($ (A)!0.333!(B) $) circle (\r); \fill[red] ($ (A)!0.666!(B) $) circle (\r); \fill[red] ($ (B)!0.333!(C) $) circle (\r); \fill[red] ($ (B)!0.666!(C) $) circle (\r); \fill[red] ($ (C)!0.333!(A) $) circle (\r); \fill[red] ($ (C)!0.666!(A) $) circle (\r); \end{tikzpicture} \subcaption*{$n=4$} \end{minipage}% \begin{minipage}[thtbp]{0.25\linewidth} \centering \begin{tikzpicture} \def\x{1.3cm} %利用参数定义长度。 \def\r{0.9mm} \coordinate (A) at (0,0); \coordinate (B) at (\x,0); \coordinate (C) at (60:\x); \fill[red] (A) circle (\r); \fill[red] (B) circle (\r); \fill[red] (C) circle (\r); \fill[red] ($(A)!0.25!(B)$) circle (\r); \fill[red] ($(B)!0.25!(C)$) circle (\r); \fill[red] ($(C)!0.25!(A)$) circle (\r); \fill[red] ($(A)!0.5!(B)$) circle (\r); \fill[red] ($(B)!0.5!(C)$) circle (\r); \fill[red] ($(C)!0.5!(A)$) circle (\r); \fill[red] ($(A)!0.75!(B)$) circle (\r); \fill[red] ($(B)!0.75!(C)$) circle (\r); \fill[red] ($(C)!0.75!(A)$) circle (\r); \end{tikzpicture} \subcaption*{$n=5$} \end{minipage}% \caption*{(第10题)} \end{figure} \end{document} ````
3条回答
U9430
2020-06-09 11:09
参考前面网友的回答,我找到了一个比较“中文友好”的方法,下面的代码看上去可能有点不太习惯,将就一下吧! texlive2020, texstudio, ubuntu, 我这里能正常编译下面的代码: ``` \documentclass{standalone} \usepackage{xcolor} \usepackage{tikz} \usetikzlibrary[calc] \usepgfmodule{parser} \usepackage{pgfplotstable} \begin{document} %保存几个名称 \def\空命令{} \def\点半径名{点半径} \def\点间距名{点间距} \def\点颜色名{点颜色} \def\一边上点的个数名{一边上点的个数} \def\边数名{边数} \def\指定边长名{指定边长} \def\临时保存{} %设置初始值 \def\点半径{2}%2pt \def\点间距{}% \def\点颜色{red} \def\一边上点的个数{} \def\边数{3} \def\边长{} %定义操作命令 \def\分析#1:#2\定界标志{% \def\临时变量{#1}% \ifx\临时变量\点半径名% \pgfmathsetmacro{\点半径}{#2}% \else% \ifx\临时变量\点间距名% \pgfmathsetmacro{\点间距}{#2}% \else% \ifx\临时变量\一边上点的个数名% \pgfmathsetmacro{\一边上点的个数}{int(#2)}% \else% \ifx\临时变量\边数名% \pgfmathsetmacro{\边数}{int(#2)}% \else% \ifx\临时变量\点颜色名% \edef\点颜色{#2}% \else% \ifx\临时变量\指定边长名% \pgfmathsetmacro{\边长}{#2}% \else% \pgferror{检查输入!}% \fi% \fi% \fi% \fi% \fi% \fi% }% \def\最终操作{% \ifx\一边上点的个数\空命令% \pgferror{“一边上点的个数”未知,请给出!}% \fi% \ifx\边长\空命令% \ifx\点间距\空命令% \pgferror{“边长、点间距”未知,无法计算,请检查!}% \else% \pgfmathsetmacro{\边长}{(\一边上点的个数-1)*\点间距}% \fi% \else% \ifx\点间距\空命令% \pgfmathsetmacro{\点间距}{\边长/(\一边上点的个数-1)}% \else% \pgfmathsetmacro{\临时点间距}{\边长/(\一边上点的个数-1)}% \pgfmathequal{\点间距}{\临时点间距}% \ifnum\pgfmathresult=0\relax% \pgferror{你输入的边长、点间距、一边上点的个数不协调,请检查!}% \fi% \fi% \fi% \pgfmathsetmacro{\边长之半}{0.5*\边长}% \pgfmathsetmacro{\圆心角}{360/\边数}% \pgfmathsetmacro{\圆心角之半}{0.5*\圆心角}% \pgfmathsetmacro{\底角}{90-\圆心角之半}% \pgfmathsetmacro{\外接圆半径}{\边长之半/cos(\底角)}% \def\保存的绘制命令{}% \pgfplotsforeachungrouped \i in {0,...,\一边上点的个数-2}% {% \edef\临时保存的绘制命令{% \noexpand\fill($(-90-\圆心角之半:\外接圆半径 pt)+(\i*\点间距 pt,0)$)circle[radius=\点半径 pt];% }% \expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\保存的绘制命令\expandafter\expandafter\expandafter{\expandafter\保存的绘制命令\临时保存的绘制命令}% }% \begin{tikzpicture} \pgfplotsforeachungrouped \j in {0,...,\边数-1}% {% \pgfmathsetmacro{\旋转角度}{\j*\圆心角}% \begin{scope}[rotate=-\旋转角度,fill=\点颜色]% \保存的绘制命令 \end{scope}% }% \node [below]at(current bounding box.south){$n=\一边上点的个数$};% \end{tikzpicture}% }% \pgfparserdef{我的点图}{initial};{% \expandafter\分析\临时保存\定界标志% \def\临时保存{}}% \pgfparserdefunknown{我的点图}{initial}{% \edef\临时保存{\临时保存\pgfparserletter}} \pgfparserdef{我的点图}{initial}。{\pgfparserswitch{final}}% \pgfparserdeffinal{我的点图}{\最终操作} \pgfparserset{silent=true} { \fbox{\pgfparserparse{我的点图}点半径:2pt;点间距:5mm;边数:4;点颜色:red;一边上点的个数:6;。} } { \fbox{\pgfparserparse{我的点图}点颜色:green;一边上点的个数:5;点间距:5mm;边数:5;。} } \end{document} ``` ![](https://wenda.latexstudio.net/data/attach/200609/d8zaHPED.png)

一周热门 更多>