我想制作一个 26x26 的矩形表,其中第一行包含字母 A、B、...、Z,第二行包含字母 B、C、...、Z、A,第三行包含字母 C、D、...、Z、A、B 等。
一种(效率不高的)方法如下:
\begin{tikzpicture}
\draw[step=.5cm,gray,thick]
(-6,-6) grid (6,6);
\draw (-5.75,5.75) node{A} (-5.25,5.75) node{B} (-4.75,5.75) node{C} (-4.25,5.75) node{D} (-3.75,5.75) node{E} etc; % this is the first row
\draw (-5.75,5.25) node{B} (-5.25,5.25) node{C} (-4.75,5.25) node{D} (-4.25,5.25) node{E} (-3.75,5.25) node{F} etc; % this is the second row
% and so on for the rest rows
\end{tikzpicture}
有什么想法可以更有效地实现这一点吗?
另外,如果我想使用希腊字母 A、B、Γ、...、Ω 代替英文字母,该怎么办?
答案1
另一种方法:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,25} {
\foreach \j in {0,...,25} {
\edef\k{\ifnum\numexpr\i+\j\relax>25
\the\numexpr\i+\j-26\relax
\else
\the\numexpr\i+\j\relax
\fi}
\node[draw,minimum size=0.5cm,inner sep=0pt]
at (\i*0.5,-\j*0.5) {\strut\symbol{\numexpr`A+\k\relax}};
}
\node at (-0.5,-\i*0.5) {\strut\i};
\node at (\i*0.5,0.5) {\strut\i};
}
\end{tikzpicture}
\end{document}
它的工作方式与普通编程语言(如 C)相同,尽管 TeX 中的整数计算有点困难。
答案2
我以手册中的例子为例:
\documentclass[]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape]
\foreach \x [count=\xi] in {A,...,Z}
\foreach \y [count=\yi] in {\x,...,Z}
\node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
at (\xi,-\yi) {$\mathstrut\y$};
\end{tikzpicture}
\end{document}
Egreg 指出你还需要一个较低的部分:
\documentclass[border=10]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape]
\foreach \x [count=\xi] in {A,...,Z}
{%
\foreach \y [count=\yi] in {\x,...,Z}
{
\node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
at (\xi,-\yi) {$\mathstrut\y$};
\xdef\Ax{\yi}
}
\pgfmathsetmacro\Ax{\Ax+1}
\foreach \y [count=\yi from \Ax ] in {A,...,\x}
{
\ifnum\yi<27
\node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
at (\xi,-\yi) {$\mathstrut\y$};
\fi
}
}
\end{tikzpicture}
\end{document}
根据要求,他用希腊字母给出了解决方案:
\newcommand\greekletter[1]{%
\ifcase #1 \relax
\or
\alpha\or
\beta\or
\gamma\or
\delta\or
\epsilon\or
\zeta\or
\eta\or
\theta\or
\iota\or
\kappa\or
\lambda\or
\mu\or
\nu\or
\xi\or
\mathrm{o}\or
\pi\or
\rho\or
\sigma\or
\tau\or
\upsilon\or
\phi\or
\chi\or
\psi\or
\omega
\else A
\fi
}
以下是完整的 MWE:
\documentclass[border=10]{standalone}
\usepackage{tikz}
\newcommand\greekletter[1]{%
\ifcase #1 \relax
\or
\alpha\or
\beta\or
\gamma\or
\delta\or
\epsilon\or
\zeta\or
\eta\or
\theta\or
\iota\or
\kappa\or
\lambda\or
\mu\or
\nu\or
\xi\or
\mathrm{o}\or
\pi\or
\rho\or
\sigma\or
\tau\or
\upsilon\or
\phi\or
\chi\or
\psi\or
\omega
\else A
\fi
}
\begin{document}
\begin{tikzpicture}[scale=0.6,transform shape]
\foreach \xx [count=\xxi] in {1,...,24}
{%
\foreach \yy [count=\yyi] in {\xx,...,24}
{
\node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
at (\xxi,-\yyi) {$\mathstrut\expandafter\greekletter{\yy}$};
\xdef\Ax{\yyi}
}
\pgfmathsetmacro\Ax{\Ax+1}
\foreach \yy [count=\yyi from \Ax ] in {1,...,\xx}
{
\ifnum\yyi<27
\node [draw, top color=white, bottom color=blue!50, minimum size=0.666cm]
at (\xxi,-\yyi) {$\mathstrut\expandafter\greekletter{\yy}$};
\fi
}
}
\end{tikzpicture}
\end{document}
答案3
这是 Leo 代码的一个变体,可以处理任意字母。我用它fontspec
来轻松使用希腊字母。
% Compile with xelatex or lualatex.
\documentclass{article}
\usepackage{tikz}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\def\alphabet{Α,Β,Γ,Δ,Ε,Ζ,Η,Θ,Ι,Κ,Λ,Μ,Ν,Ξ,Ο,Π,Ρ,Σ,Τ,Υ,Φ,Χ,Ψ,Ω}
\def\alphabetlength{24}
\begin{document}
\begin{tikzpicture}
\foreach \row in {1,...,\alphabetlength} {
\foreach[count=\x] \letter in \alphabet {
\pgfmathsetmacro\column{Mod(-\row+\x,\alphabetlength)}
\node[draw,minimum size=0.5cm,inner sep=0pt]
at (\column*0.5,-\row*0.5) {\strut\letter};
}
\node at (-0.5,-\row*0.5) {\strut\row};
\node at ({\row*0.5-0.5},0) {\strut\row};
}
\end{tikzpicture}
\end{document}
答案4
\documentclass[border=10]{standalone}
\usepackage{tikz}
\begin{document}
\footnotesize
\begin{tikzpicture}[every node/.style={draw, circle, minimum size=16pt}]
\foreach \x [count=\xi] in {A,...,Z}
{\foreach \y [count=\yi] in {\x,...,Z}
{\node at (6*\xi ex,-6*\yi ex) {\y};}%
\ifnum\yi>1 \foreach \z [count=\zi from \yi ] in {A,...,\x}
{\node at (6*\xi ex +6 ex,-6*\zi ex) {\z};}\fi}
\end{tikzpicture}
\end{document}