axis 环境的某些可选参数令 \foreach 命令不工作

2020-06-06 11:27发布

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

## 编译环境 操作系统 * [x] Windows 10 * [ ] macOS * [ ] Linux `若需勾选,请把[ ]改成[x]` Tex发行版 * [x] TexLive 2020 * [ ] MikTeX `版本号` * [ ] CTeX `若需勾选,请把[ ]改成[x]` ## 我的问题 `axis` 环境的某些可选参数令 `\foreach` 命令不工作。 ```tex \documentclass[tikz]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.15} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=0, xmax=5, ymin=0, ymax=5, xtick={1,2,3,4}, ytick={1,2,3,4}, ] \foreach \i in {1,...,4} { \foreach \j in {1,...,4} { \fill (\i,\j) circle (1pt); }; }; \end{axis} \end{tikzpicture} \end{document} ```
1条回答
`axis` 环境中的循环结构会在 `\end{axis}` 之后求值,所以它的值会丢掉。所以要用一些其他手段: ``` \documentclass[tikz]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.15} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=0, xmax=5, ymin=0, ymax=5, xtick={1,2,3,4}, ytick={1,2,3,4}, ] \foreach \i in {1,...,4} { \foreach \j in {1,...,4} { \edef\temp{\noexpand\fill (axis cs:\i,\j) circle (1pt);} \temp } } \end{axis} \end{tikzpicture} \end{document} ``` 参考来源: > `PGFPLOTS` 手册 8.1 Utility Commands > [\foreach not behaving in axis environment](https://tex.stackexchange.com/questions/170664/foreach-not-behaving-in-axis-environment "\foreach not behaving in axis environment")

一周热门 更多>