我是 tikz 的新手,之前从未在 latex 中使用过 ifthenelse 语句,但我正在尝试创建一个网格,其中每个对角线上的顶点都有不同的颜色和形状。这是我现在所拥有的,基于我在https://graphtheoryinlatex.wordpress.com/2011/08/24/edges-on-a-grid/
\documentclass[11pt,letterpaper]{article}
\usepackage{tkz-berge}
\begin{document}
\begin{tikzpicture}
\grGrid[prefix=a,RA=1.5,RB=1.5]{8}{8}
\foreach \x in {0,1,...,6}{
\foreach \y in {0,1,...6}{
%something ifthenelse to change vertices
}
}
\end{tikzpicture}
\end{document}
答案1
对角线上的元素的索引和为奇数或偶数,因此您可以使用\ifodd
; 示意图:
\ifodd\numexpr\Row+\Column\relax
do domething for elements on diagonals with odd sum of indexes
\else
do domething for elements on diagonals with even sum of indexes
\fi
在问题中不清楚是否tkz-berge
真的需要所以我选择了“纯” TikZ 方法:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]
\def\Factor{1.4}
\foreach \Column in {1,...,8}
{
\foreach \Row in {1,...,8}
{
\ifodd\numexpr\Row+\Column\relax
\def\Shape{circle}
\def\Color{cyan!40}
\else
\def\Shape{rectangle}
\def\Color{red!40}
\fi
\node[
minimum size=1cm,
shape=\Shape,
fill=\Color,
font=\footnotesize,
]
(a\Column\Row)
at (\Factor*\Row,-\Factor*\Column)
{a\Column,\Row};
}
}
\begin{pgfonlayer}{background}
\draw (a11) grid[step=\Factor] (a88);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}