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条回答
lichangkai - latex爱好者
2020-06-08 23:35 .采纳回答
画图并不难,有多种方式,麻烦的是高度可定制的样式,这里提供了如何使用`pgfkey`管理样式的例子供参考,简单实现了对三角形以下样式的定制: - 每条边上点的数量 - 相邻点之间的间距 - 边的宽度(会自动计算间距) - 每个重复点的样式 - 三角形下方的文本 更多的样式可以参照着设置。 ```tex \documentclass[tikz, border=1cm]{standalone} \usepackage{xeCJK} \usetikzlibrary{calc} \makeatletter \newlength\triangle@@sep \tikzset{ triangle count/.store in=\triangle@count, triangle count=2, triangle sep/.store in=\triangle@sep, triangle sep=5mm, triangle width/.store in=\triangle@width, triangle width=, triangle pattern/.store in=\triangle@pattern, triangle pattern = {\fill[red] (0, 0) circle (2pt);}, triangle text/.store in=\triangle@text, triangle text=, pics/triangle/.style={ /tikz/transform shape, code = { \pgfmathsetmacro{\triangle@count}{\triangle@count - 1} \ifx\triangle@width\pgfutil@empty \pgfmathsetlength{\triangle@@sep}{\triangle@sep} \else \pgfmathsetlength{\triangle@@sep}{\triangle@width/\triangle@count} \typeout{xyz \the\triangle@@sep} \fi \coordinate (triangle@shift) at (0, 0); \pgfmathsetmacro{\triangle@total}{3 * \triangle@count} \begin{scope}[local bounding box=triangle] \foreach \x [ evaluate=\x as \triangle@ang using {120 * floor((\x - 1) / \triangle@count)} ] in {1, ..., \triangle@total} { \coordinate (triangle@shift) at ($(triangle@shift) + (\triangle@ang:\triangle@@sep)$); \begin{scope}[shift={(triangle@shift)}] \triangle@pattern \end{scope} } \end{scope} \ifx\triangle@text\pgfutil@empty \else \path (0, 0 -| triangle.north) ++(0, -5mm) node {\triangle@text}; \fi } } } \makeatother \begin{document} \begin{tikzpicture} \pic at (0, 0) [triangle count=3, triangle text={test 1}] {triangle}; \pic at (3, 0) [triangle count=4, triangle width=3cm, triangle text={test 2}, triangle pattern={ \node[cyan] at (0, 0) {正}; } ] {triangle}; \pic at (8, 0) [triangle count=5, triangle width=3cm, triangle text={test 3}, triangle pattern={ \fill[teal] (-2pt, -2pt) rectangle (2pt, 2pt); } ] {triangle}; \foreach \x in {2, ..., 5} { \pic at ({(\x - 2)*2.5}, -3) [triangle count=\x, triangle text={$n=\x$}] {triangle}; } \foreach \x [evaluate=\x as \c using 100*(\x-2)/4] in {2, ..., 5} { \pic at ({(\x - 2)*3.5}, -6) [ triangle count=\x, triangle text={$n=\x$}, triangle width=2cm, triangle pattern={ \fill[red!\c!yellow] (-2pt, -2pt) rectangle (2pt, 2pt); } ] {triangle}; \end{tikzpicture} \end{document} ``` ![](https://wenda.latexstudio.net/data/attach/200608/ofRxqPvV.png)

一周热门 更多>